<?php

use Database\{Channels, Games, Listbots, Profile, Settings, Users};
use inc\functions;

if (Users::check($Userid) == false) {
    $Telegram->answerInlineQuery($Telegram->inline_query()->getId(), [], 1, null, null, msg('FirstJoinBot'), 'new');
    die();
}

if ($user_rank != 'admin') {
    if (Settings::getSettings('offline')) {
        $txt = Settings::getSettings('textOffline');

        $Telegram->answerInlineQuery($Telegram->inline_query()->getId(), [], 1, null, null, $txt == null ? msg('offline') : $txt, 'new');
        die();
    }

    if (Settings::getSettings('lock') == 1 && Channels::channelCount() > 0) {

        foreach (Channels::getAll() as $channel) {
            $beforToken = $Telegram->bot_token;
            $Telegram->bot_token = Listbots::getBotDef()->token;
            $get = $Telegram->getChatMember($channel->chatid, $Userid);
            if ($get->getOk() && !in_array($get->getResult()->getStatus(), [
                    'creator',
                    'member',
                    'administrator'
                ])) {
                $Telegram->answerInlineQuery($Telegram->inline_query()->getId(), [], 1, null, null, msg('FirstJoinChannel'), 'new');
            }
            $Telegram->bot_token = $beforToken;
            break;
        }
    }
}


if (preg_match('/^([Rr]esend_)(.*)/', $text)) {

    $random_id = str_replace('resend_', '', $text);
    if (Games::hasGame($random_id) == false) {
        $Telegram->answerInlineQuery($Telegram->inline_query()->getId(), [], 2, null, null, msg('NotFoundGame'), 'new');
        die();
    }
    $GetGameInfo = $func->GetGameInfo($random_id);
    $type = $GetGameInfo['type'];
    $cur = $func->GetCurrentPlayer($random_id)[0];
    $info_cur = $Telegram->getChat($cur)->getResult()->getFirstName();
    $txt = str_replace(['#name'], [functions::generationMentions($cur, $info_cur)], msg('nextgame'));
    $Telegram->answerInlineQuery($Telegram->inline_query()->getId(), [
        [
            'type' => 'article',
            'id' => base64_encode(1),
            'title' => msg('GameTransfer'),
            'input_message_content' => [
                'message_text' => $txt,
                "parse_mode" => "html",
                "disable_web_page_preview" => true
            ],
            'thumb_url' => PHOTO_1,
            "reply_markup" => $func->generation_btn($random_id, $type),

        ],
    ], 1);
} else {
    $first_name = $Telegram->FirstName();
    Profile::updateRealname($Userid, $first_name);
    $start_text = '1 - ' . $first_name;
    $url = functions::generationMentions($Userid, $first_name);
    $random_id = $func->RandomText(20);

    if (Games::hasGame($random_id) == false) {
        Games::createGame($random_id, $UsernamebotCur, $Userid, 'via', '', $Userid, 'close');
    }
    $msginline = msg('startgame');
    if (Settings::getSettings('lock')  == 1 and Channels::channelCount() > 0) {
        $chanel_id = '';
        $i = 1;
        $beforToken = $Telegram->bot_token;
        $Telegram->bot_token = Listbots::getBotDef()->token;
        foreach (Channels::getAll() as $channel) {
            $getchat = $Telegram->getChat($channel->chatid);

            if ($getchat->getOk()) {
                $titles = $getchat->getResult()->getTitle();
                $urlChannel = '<a href="' . $channel->link . '">' . $titles . '</a>';
                $chanel_id .= $i . '- ' . $urlChannel . "\n\n";
                $i++;
            }
        }
        $Telegram->bot_token = $beforToken;

        $msginline = str_replace(['#channel'], [
            $chanel_id
        ], msg('startgameLock'));
    }
    $Telegram->answerInlineQuery($Telegram->inline_query()->getId(), [
        [
            'type' => 'article',
            'id' => base64_encode(1),
            'title' => msg('game_1'),
            'input_message_content' => [
                'message_text' => str_replace(['#name', '#users', '#type'], [
                    $url,
                    $start_text,
                    msg('gametitle_1')
                ], $msginline),
                "parse_mode" => "html",
                "disable_web_page_preview" => true
            ],
            'thumb_url' => PHOTO_1,
            "reply_markup" => $func->generation_BtnStart($random_id, 1),

        ],
        [
            'type' => 'article',
            'id' => base64_encode(2),
            'title' => msg('game_2'),
            'input_message_content' => [
                'message_text' => str_replace(['#name', '#users', '#type'], [
                    $url,
                    $start_text,
                    msg('gametitle_2')
                ], $msginline),
                "parse_mode" => "html",
                "disable_web_page_preview" => true
            ],
            'thumb_url' => PHOTO_2,
            "reply_markup" => $func->generation_BtnStart($random_id, 2)
        ],
        [
            'type' => 'article',
            'id' => base64_encode(3),
            'title' => msg('game_3'),
            'input_message_content' => [
                'message_text' => str_replace(['#name', '#users', '#type'], [
                    $url,
                    $start_text,
                    msg('gametitle_3')
                ], $msginline),
                "parse_mode" => "html",
                "disable_web_page_preview" => true
            ],
            'thumb_url' => PHOTO_3,
            "reply_markup" => $func->generation_BtnStart($random_id, 3)

        ],
        [
            'type' => 'article',
            'id' => base64_encode(4),
            'title' => msg('game_4'),
            'input_message_content' => [
                'message_text' => str_replace(['#name', '#users', '#type'], [
                    $url,
                    $start_text,
                    msg('gametitle_4')
                ], $msginline),
                "parse_mode" => "html",
                "disable_web_page_preview" => true
            ],
            'thumb_url' => PHOTO_4,
            "reply_markup" => $func->generation_BtnStart($random_id, 4)

        ]
    ], 1);
}
