<?php
date_default_timezone_set('Asia/Tehran');
ob_start();
define('DIR_BOT', __DIR__ . '/');

if (!file_exists('Games')) {
    error_log('Create Folder Games');
    mkdir('Games');
}


//endregion use Class

require_once 'vendor/autoload.php';

use Carbon\Carbon;

Carbon::setLocale('fa');

//region use Class
use Database\{Groups, Listbots, Profile, Users, Botads, Settings, Channels, Shop, Payment};
use inc\{fileStorage, functions};

$func = new functions();
//region Set Token And UsernameBot
$UsernamebotCur = $_GET['usernamebot'] ?? null;

if (Listbots::hasBot($UsernamebotCur) and isset($UsernamebotCur)) {
    define('API_KEY', Listbots::getBot($UsernamebotCur)->token);
} else {
    $listbots = Listbots::getBotDef();
    define('API_KEY', $listbots->token);
    $UsernamebotCur = $listbots->bot;
}

define('USERNAME_BOT', $UsernamebotCur);
//endregion Set Token And UsernameBot

//region Db connect
$Telegram = new \telegramBotApiPhp\telegram(API_KEY,'http://91.107.247.58:8081');
//endregion Db connect

//region Init Method
$chatid = $Telegram->chatId() ?? 'none';
$Userid = $Telegram->fromId();
$chat_type = $Telegram->chatType();
$text = $Telegram->text();
$data = $Telegram->isCallbackQuery() ? $data = $Telegram->callback_query()->getData() : null;
$MessageID = $Telegram->messageID();
//endregion Init Method
//new \inc\UpdateIdChecker($Telegram->update_id());

//region Get & Check And Insert User
$user_lang = DF_LANG;
if ($Telegram->isPrivate() and !$Telegram->isInlineQuery()) {
    Users::checkAndInsert($Userid);
    Profile::checkAndInsertProfile($Userid);
}
$user_rank = 'user';
$infouser = null;
$botdef = Listbots::getBotDef();

if (!fileStorage::checkStore('bot')) {
    $arrBot = [
        'botDefToken' => $botdef->token,
        'botDefUserName' => $botdef->bot,
        'adminChatid' => Users::getAdmins(),
    ];
    fileStorage::saveTofile('bot', $arrBot);
} else {
    $arrBot = fileStorage::readFile('bot');
}
setsStore($arrBot);
if ($Telegram->isPrivate() && Users::check($chatid)) {
    setStore('getUser', Users::getUser($Userid));
    setStore('getUserProfile', Profile::getUser($Userid));
    setStore('inGame', getStore('getUserProfile')->ingame);
    $infouser = getStore('getUser');
    $user_active = $infouser->active;
    $user_rank = $infouser->rank;
    $user_data = $infouser->data;
    @$command = $infouser->command;
    $user_lang = $infouser->lang;
} elseif ($Telegram->fromId()) {
    if (Users::check($Userid)) {
        if (is_null(getStore('getUser'))) {
            setStore('getUser', Users::getUser($Userid));
            setStore('getUserProfile', Profile::getUser($Userid));
        }
        $infouser = getStore('getUser');
        $user_active = $infouser->active;
        $user_rank = $infouser->rank;
        $user_data = $infouser->data;
        @$command = $infouser->command;
        $user_lang = $infouser->lang;
    }
}
Users::updateBot($Userid, $UsernamebotCur);

//endregion Get & Check And Insert User
$signature = Settings::getSettings('signature');

//region Array Db
$ListState = ['آذربایجان شرقی', 'آذربایجان غربی', 'اردبیل', 'اصفهان', 'البرز', 'ایلام', 'بوشهر', 'تهران', 'چهارمحال و بختیاری', 'خراسان جنوبی', 'خراسان رضوی', 'خراسان شمالی', 'خوزستان', 'زنجان', 'سمنان', 'سیستان و بلوچستان', 'فارس', 'قزوین', 'قم', 'کردستان', 'کرمان', 'کرمانشاه', 'کهگیلویه و بویراحمد', 'گلستان', 'گیلان', 'لرستان', 'مازندران', 'مرکزی', 'هرمزگان', 'همدان', 'یزد',];
$ListGender = ['آقا', 'خانم'];
//endregion Array Db
$meliNwt = '<a href="http://t.me/Ping_Servers">' . msg('meli') . '</a>';

//region Include File
include 'btn.php';

include 'plugin.php';
//endregion Include File

if ($Telegram->isPrivate()) {
    Users::updateStatus($chatid, true);
    if ($func->regex_search($text, pattern()) == false && ($command == 'none' || $command == null) && !isset($data)) {
        if ($func->regex_search($text, pattern_admin()) == true && ($command == 'none' || $command == null) && !isset($data)) {
            die();
        }
        $Telegram->sendMessage($chatid, msg('notfoundCommand'));

    }
}
//endregion NotFound
