<?php

namespace Twitter;
//use Illuminate\Database\Eloquent\Model as DB;


class ChangeAvatar
{
    // Active twitter object for quering api
    private static $twitter;

    public static function getAuthorizationUrl()
    {
        $key         = \Config::get('twitter_api_key');
        $secret      = \Config::get('twitter_api_secret');
        $callbackUrl = \Config::get('twitter_callback_url');

        $auth = new \TwitterOAuth($key,$secret);

        $token = $auth->getRequestToken($callbackUrl);

        $_SESSION['request_token'] = $token;

        $registerLink = $auth->getAuthorizeURL($token);

        return $registerLink;
    }


    public static function getAvatar()
    {
        $key         = \Config::get('twitter_api_key');
        $secret      = \Config::get('twitter_api_secret');

        $token = $_SESSION['tw_token'];

        $twitter = new \TwitterOAuth($key,$secret,$token['oauth_token'],$token['oauth_token_secret']);
        $_SESSION['at'] = $twitter->getAccessToken($_SESSION['oauth_verifier']);

        $data = self::$twitter->get('account/verify_credentials');

        $avatarUrl = str_replace("_normal", "", $data->profile_image_url);

        $avatar = file_get_contents($avatarUrl);

        $filename = \Config::get('base_path').'avatars/'.$data->screen_name.'.jpg';
        file_put_contents($filename, $avatar);

        $_SESSION['screen_name'] = $data->screen_name;

        self::makeImg($filename, $data->screen_name);

        //return $data;
    }

    public static function postAvatar()
    {

        $key         = \Config::get('twitter_api_key');
        $secret      = \Config::get('twitter_api_secret');

        $token = $_SESSION['at'];

        $twitter = new \TwitterOAuth($key,$secret,$token['oauth_token'],$token['oauth_token_secret']);

        $screenName = $_SESSION['screen_name'];

        $path = \Config::get('base_path').'avatars/'.$screenName.'.jpg';
        //$type = pathinfo($path, PATHINFO_EXTENSION);
        $data = file_get_contents($path);

        //$img_base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
        $img_base64 = base64_encode($data);

        $twitter->post('account/update_profile_image', array('image' => $img_base64));
        $twitter->post('statuses/update', array('status' => 'test test test'));

        return $token;
    }

    public static function makeImg($filename,$username)
    {
        $path = \Config::get('base_path').'avatars/';
        $top_file = $filename;
        $bottom_file = $path.'overlay.png';

        list($bottom_width, $top_bottom) = getimagesize($bottom_file);

        $top = self::_resizeImage($top_file, 200, 200);
        $bottom = imagecreatefrompng($bottom_file);

        $new = imagecreatetruecolor(200,200);
        imagecopyresampled($new, $top, 0, 0, 0, 0, 200, 200, 200, 200);

        imagecopyresampled($new, $bottom, 45, 5, 0, 0, 155, 61, 155, 61);

        imagepng($new, $path.$username.'.png');
    }

    private static function _resizeImage($file, $w, $h) {
        list($width, $height, $src_image_type) = getimagesize($file);

        switch ($src_image_type)
        {
            case IMAGETYPE_GIF:
                $src = imagecreatefromgif($file);
                break;
            case IMAGETYPE_JPEG:
                $src = imagecreatefromjpeg($file);
                break;
            case IMAGETYPE_PNG:
                $src = imagecreatefrompng($file);
                break;
        }

        $dst = imagecreatetruecolor($w, $h);
        imagecopyresampled($dst, $src, 0, 0, 0, 0, $w, $h, $width, $height);

        return $dst;
    }

    public function saveOauthData()
    {

    }
}