<?php

use Database\{Listbots, Users};

if ($text == patternAdmin('bots')) {
    $Telegram->SendMessage($chatid, msg('def'), 'html', false, $button_bots);
} elseif ($text == patternAdmin('listBot')) {
    $btn = [];
    $btn[] = [["text" => msg('username'), "callback_data" => "none"], ["text" => msg('defualt'), "callback_data" => "none"]];
    foreach (Listbots::getBots() as $bot) {
        $btn[] = [["text" => $bot->bot, "callback_data" => "setdefbot_" . $bot->bot], ["text" => $bot->def == 1 ? '*' : ' ', "callback_data" => "setdefbot_" . $bot->bot]];
    }
    $Telegram->SendMessage($chatid, msg('txtListBots'), 'html', false, [
        'inline_keyboard' =>
            $btn
        ,
        'resize_keyboard' => true
    ]);
} elseif (preg_match('/^([Ss]etdefbot_)(.*)/', $data)) {
    $dta = str_replace(['setdefbot_'], '', $data);
    Listbots::removeDefults();
    Listbots::updateDef($dta, 1);
    $btn = [];
    $btn[] = [["text" => msg('username'), "callback_data" => "none"], ["text" => msg('defualt'), "callback_data" => "none"]];
    foreach (Listbots::getBots() as $bot) {
        $btn[] = [["text" => $bot->bot, "callback_data" => "setdefbot_" . $bot->bot], ["text" => $bot->def == 1 ? '*' : ' ', "callback_data" => "setdefbot_" . $bot->bot]];
    }
    $Telegram->editMessageReplyMarkup($chatid, $MessageID, msg('txtListBots'), [
        'inline_keyboard' =>
            $btn
        ,
        'resize_keyboard' => true
    ]);
} elseif ($text == patternAdmin('addBot')) {
    Users::updateCommand($chatid, 'addbot');
    $Telegram->SendMessage($chatid, msg('sendToken'), 'html', false, $button_backhome);
} elseif ($command == 'addbot' && $text != patternAdmin('back')) {
    $bot = Users::getUser($chatid)->bot;
    $beforToken = $Telegram->bot_token;
    $Telegram->bot_token = $text;
    $getme = $Telegram->getMe();
    $botusername = $getme->getResult()->getUsername();
    $Telegram->bot_token = $beforToken;
    if ($getme->getOk() == false) {
        $Telegram->SendMessage($chatid, msg('notToken'), 'html', false);
        die();
    }
    if (Listbots::hasBot($botusername)) {
        $Telegram->SendMessage($chatid, msg('alreadyBot'), 'html', false);
        die();
    }
    Listbots::addBot($botusername, $text);
    Users::updateCommand($chatid, null);
    $Telegram->bot_token = $text;
    $Telegram->setWebhook(URLBOT . 'index.php?usernamebot=' . $botusername);
    $Telegram->bot_token = $beforToken;
    $Telegram->SendMessage($chatid, msg('done'), 'html', false, $button_bots);

} elseif ($text == patternAdmin('removeBot')) {
    Users::updateCommand($chatid, 'removebot');
    $Telegram->SendMessage($chatid, msg('sendusernamebot'), 'html', false, $button_backhome);
} elseif ($command == 'removebot' && $text != patternAdmin('back')) {
    if (Listbots::hasBot($text) == false) {
        $Telegram->SendMessage($chatid, msg('notalreadyBot'), 'html', false);
        die();
    }
    $getbot = Listbots::getBot($text);
    $beforToken = $Telegram->bot_token;
    $Telegram->bot_token = $getbot->token;
    $Telegram->deleteWebhook();
    $Telegram->bot_token = $beforToken;
    Listbots::removeBot($text);
    Users::updateCommand($chatid, null);
    $Telegram->SendMessage($chatid, msg('done'), 'html', false, $button_bots);
}

