<?php

use danog\MadelineProto\API;
use danog\MadelineProto\EventHandler;
use danog\MadelineProto\MTProto;
use danog\MadelineProto\Exception;
use danog\MadelineProto\Logger as Logg;
use danog\MadelineProto\Settings;
use danog\MadelineProto\Settings\AppInfo;
use danog\MadelineProto\Settings\Peer;
use danog\MadelineProto\Settings\Logger;
use danog\MadelineProto\Settings\Serialization;
use danog\MadelineProto\RPCErrorException;
use danog\MadelineProto\Loop\Generic\GenericLoop;
use Amp\Process\Process;
include "vendor/autoload.php";
/*file_exists('madeline.php') || copy('https://phar.madelineproto.xyz/madeline.php', 'madeline.php');
require 'madeline.php';*/
if (!isset($_GET["appid"], $_GET["apphash"], $_GET['phone'], $_GET['c'], $_GET['l'], $_GET['p'],$_GET['info'])) {
    die(json_encode(["ok" => false, 'result' => "Parms Is Missing"]));
}
if(!is_dir("bots")){
    mkdir("bots");
}
$Peer = (new Peer)
->setFullFetch(false)
->setCacheAllPeersOnStartup(false)
->setFullInfoCacheTime(60);
$Logger = (new Logger)
->setLevel(Logg::FILE_LOGGER)
->setMaxSize(2 * 1024 *1024);
$Ser = (new Serialization)
->setInterval(60);
$app = (new AppInfo)
->setApiId($_GET['appid']) // (int) Api id
->setApiHash($_GET['apphash']);
$settings = new Settings;
$settings->setAppInfo($app);
$settings->setPeer($Peer);
$settings->setLogger($Logger);
$settings->setSerialization($Ser);
$sessionname = "bots/".md5("+".str_replace(["+"," "],"",$_GET['phone']))."Harim";
$mProto = new API($sessionname, $settings);
    try {
        if($_GET['info'] == "logout"){
             $mProto->logout();
            die(json_encode(["ok" => true, 'result' =>"done"]));
        }
        if ($_GET['info'] == "get") {
            die(json_encode(["ok" => true, 'result' => $mProto->getSelf()]));
        }
        if ($_GET['l'] != "start") {
            if ($mProto->getAuthorization() === MTProto::LOGGED_IN) {
                die(json_encode(["ok" => true, 'result' => "loggedin"]));
            }
            if ($_GET['c'] == 0) {
                 $mProto->phoneLogin($_GET['phone']);
                die(json_encode(["ok" => true, 'result' => "code Sent"]));
            } elseif ($_GET['c'] == 1) {
                 $mProto->complete2falogin(urldecode($_GET['p']));
                 unset($mProto);
                die(json_encode(["ok" => true, 'result' => "done"]));
            }elseif($_GET['c'] == 2){
                 $mProto->completeSignup($_GET['p']);
                 unset($mProto);
                die(json_encode(["ok" => true, 'result' => "done"]));
            } else {
                $authorization =  $mProto->completePhoneLogin($_GET['c']);
                if ($authorization['_'] === 'account.password') {
                    die(json_encode(["ok" => true, 'result' => "2fa"]));
                }
                if ($authorization['_'] === 'account.needSignup') {
                    die(json_encode(["ok" => true, 'result' => "needSignup"]));
                }
                unset($mProto);
                die(json_encode(["ok" => true, 'result' => "done"]));
            }
        }
    } catch (\Throwable $e) {
        die(json_encode(['ok' => $e->getMessage() == 'Undefined property: danog\MadelineProto\MTProto::$chats' ? true : false, 'result' => $e->getMessage()]));
    }

/*if (isset($_GET['l']) == "start") {
   // $mProto->startAndLoop(MrPoker::class);
    MrPoKeR::startAndLoop($sessionname, $settings);
}*/