<?php

namespace App\Http\Controllers;

use App\Models\Bot_info;
use App\Models\Campains_user;
use App\Models\User;
use GuzzleHttp\Client;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;


class TelegramController extends Controller
{
    public $request;

    public $bot_key;

    public function __construct()
    {
        $this->bot_key = config('TelegramSettings.bot_api');
        if(empty($this->bot_key)){
            echo 'your bot api key is empty';die;
        }
        $this->request = new Client(array( 'base_uri' => 'https://tapi.bale.ai/bot'.$this->bot_key.'/' ));
    }


    public function index()
    {
        //$response = json_decode(file_get_contents("php://input"));

        // return $response;
    }

    public function setWebhook()
    {
        $response =  Http::get('https://tapi.bale.ai/bot'.$this->bot_key.'/setWebhook',[
            'url'    =>  config('app.url').'/api/salam_dell'
        ]);
        return $response;
    }

    public function deleteWebhook()
    {
        $response =  Http::get('https://tapi.bale.ai/bot'.$this->bot_key.'/deleteWebhook');
        return $response;
    }

    public function getUpdates()
    {
        $response =  Http::get('https://tapi.bale.ai/bot'.$this->bot_key.'/getUpdates');
        return $response->json();
    }

    public function getWebhookInfo()
    {
        $response =  Http::get('https://tapi.bale.ai/bot'.$this->bot_key.'/getWebhookInfo');
        return $response;
    }

    // public function getUserProfile($id = 1313323820)
    // {
    //     $response =  Http::get('https://tapi.bale.ai/bot'.$this->bot_key.'/getUserProfilePhotos',[
    //         'user_id'   => 1313323820
    //     ]);
    //     if($response['result']['photos'][0][2]['file_id'] != null) {
    //         $file_id =  $response['result']['photos'][0][2]['file_id'];
    //         $respon =  Http::get('https://tapi.bale.ai/bot'.$this->bot_key.'/getFile',[
    //             'file_id'   => $file_id
    //         ]);
    //         //  return $respon;die;
    //         if($respon['ok'] == true && $respon['result']['file_path'] != null){
    //             $image = Http::get('https://api.telegram.org/file/bot'.$this->bot_key.'/'.$respon['result']['file_path']);

    //             Storage::put('public/user_profile/'.$id.'.jpeg', $image );
    //             $path = 'storage/public/user_profile/'.$id.'.jpeg' ;

    //             $user = new User();
    //             $user->name = 'salam';
    //             $user->user_telegram_id = $id;
    //             $user->photo = $path;
    //             $user->save();
    //             $campain_user = new Campains_user();
    //             $campain_user->campain_id = 1;
    //             $campain_user->user_id = $user->id;
    //             $campain_user->save();
    //             $main_filename = public_path('image\main_image.jpg');
    //             //return $main_filename;

    //             $user_image = public_path($path);
    //             // The file
    //             $filename = $main_filename;
    //             $percent = 0.4;
    //             // Content type
    //             header('Content-Type: image/jpeg');
    //             // Get new dimensons
    //             list($width, $height) = getimagesize($user_image);
    //             $new_width = $width * $percent;
    //             $new_height = $height * $percent;
    //             // Resampl
    //             $image_p = imagecreatefromjpeg($user_image);
    //             $image   = imagecreatefromjpeg($filename);
    //             imagecopyresampled($image,$image_p , 510, 50, 0, 0, $new_width, $new_height, $width, $height);
    //             // Output
    //             imagejpeg($image, public_path('image/1.jpeg'), 60);
    //             return $path;
    //         }
    //     }
    //     echo '<pre>';
    //     var_dump($response);
    //     echo '</pre>';die;
    // }
}
