<?php

//region Select Cat
use Database\{Profile, Users, QuestionUsers, Groups, Listbots, Question};
use inc\{functions, fileStorage};

if ($data == 'sendqus') {

    if (getStore('inGame') == true) {
        $Telegram->EditMessageText($chatid, $MessageID, msg('alerGame2'), 'html', '', true, $button_start);
        die();
    }
    $dta = str_replace(['sendqus'], '', $data);
    $Telegram->EditMessageText($chatid, $MessageID, msg('SelectQusGame'), 'html', '', true, $button_SendQus);
}

//endregion Select Cat

//region Send Qus
elseif (preg_match('/^([Ss]endqusnew_)(.*)/', $data)) {
    $dta = str_replace(['Sendqusnew_'], '', $data);
    Users::updateData($chatid, $dta);
    $button = [
        'inline_keyboard' => [
            [
                ["text" => msg('allsex'), "callback_data" => "sendEndQus_all_" . $dta],
            ],
            [
                ["text" => msg('male'), "callback_data" => "sendEndQus_male_" . $dta],
                ["text" => msg('female'), "callback_data" => "sendEndQus_female_" . $dta]
            ],
        ],
        'resize_keyboard' => true
    ];
    $Telegram->sendMessage($chatid, msg('selecQusGen'), 'html', false, $button);
} elseif (preg_match('/^(sendEndQus_)(.*)/', $data)) {
    $dta = str_replace(['sendEndQus_'], '', $data);

    Users::updateCommand($chatid, 'SendNewQus');
    Users::updateData($chatid, $dta);

    $Telegram->sendMessage($chatid, msg('sendquestion'), 'html', false, $button_cancels);
} elseif ($command == 'SendNewQus' and $text != Getpattern('cancels') and $data != 'back') {
    if ($text) {
        Users::updateCommand($chatid, null);
        Users::updateData($chatid, null);
        $a = $Telegram->sendMessage($chatid, '.', 'html', false, $button_remove);
        if ($a->getOk()) {
            $Telegram->deleteMessage($chatid, $a->getResult()->getMessageId());
        }
        $Telegram->sendMessage($chatid, msg('done'), 'html', false, $button_start);
        $senderMen = functions::generationMentions($chatid, $chatid);
        $ex = explode('_', $user_data);
        $msg = str_replace(['#user', '#type', '#text', '#sex'], [$senderMen, msg('SendQus_' . $ex[1]), $text, $ex[0]], msg('SendNewQusGame'));

        $rowId = QuestionUsers::addQuestion($chatid, $text, $ex[1], $ex[0]);
        $button = [
            'inline_keyboard' => [
                [
                    ["text" => msg('accept'), "callback_data" => "QusGameaccept_" . $rowId],
                ],
                [
                    ["text" => msg('reject'), "callback_data" => "QusGamereject_" . $rowId],
                ],
            ],
            'resize_keyboard' => true
        ];
        $beforToken = $Telegram->bot_token;
        $Telegram->bot_token = getStore('botDefToken');
        if (Groups::hasGroups()) {
            foreach (Groups::getGroups() as $item) {
                $Telegram->sendMessage('-' . $item->chatid, $msg, 'html', false, $button);
            }

        } else {
            $Telegram->sendMessage(Users::getAdmins()[0]->chatid, $msg, 'html', false, $button);
        }
        $Telegram->bot_token = $beforToken;
    } else {
        $Telegram->sendMessage($chatid, msg('string'), 'html', false, $button_cancels);
    }
}
//endregion Send Qus
//region Accept Qus
elseif (preg_match('/^([Qq]usGameaccept_)(.*)/', $data)) {
    $dta = str_replace(['QusGameaccept_'], '', $data);
    if (QuestionUsers::hasQuestion($dta)) {
        $qus = QuestionUsers::getQuestion($dta);
        Question::addQuestion($qus->type, $qus->question, $qus->sex);
        fileStorage::removeFile('question');
        QuestionUsers::removeQuestion($dta);
        $Telegram->deleteMessage($chatid, $MessageID);
        $Telegram->answerCallbackQuery($Telegram->Callback_Id(), msg('DoneacceptQusGame'), false);
    }
}
//endregion Accept Qus
//region Reject Qus
if (preg_match('/^([Qq]usGamereject_)(.*)/', $data)) {
    $dta = str_replace(['QusGamereject_'], '', $data);
    if (QuestionUsers::hasQuestion($dta)) {
        QuestionUsers::removeQuestion($dta);
        $Telegram->deleteMessage($chatid, $MessageID);
        $Telegram->answerCallbackQuery($Telegram->Callback_Id(), msg('rejectQusGame'), false);
    }
}
//endregion Accept Qus

?>