<?php

use Database\{Settings, Profile, blocked, Users, Playerlist, Games, Question};
use inc\functions;
use inc\fileStorage;

if (preg_match('/^([Ss]tartGameByFrinds_)(.*)/', $data)) {
    $dta = str_replace(['StartGameByFrinds_'], '', $data);

    $typelockbot = Settings::getSettings('type');
    $countLockCoin = Settings::getSettings('countLockCoin');
    $countinvite = Settings::getSettings('countinvite');
    $UserProfile = getStore('getUserProfile');
    $coinUser = $UserProfile->coin;
    $inviteUser = $UserProfile->invite;
    if ($typelockbot == 'coin' and $coinUser < $countLockCoin) {
        $Telegram->sendMessage($chatid, str_replace(['#userCoin', '#botCoin'], [$coinUser, $countLockCoin], msg('lockCoin')), 'html', false);
        die();
    } elseif ($typelockbot == 'invite' and $inviteUser <= $countinvite) {
        $Telegram->sendMessage($chatid, str_replace(['#invite'], [$countinvite], msg('lockInvite')), 'html', false);
        die();
    }
    if ($UserProfile->ingame == true) {
        $Telegram->sendMessage($chatid, msg('alerGame2'), 'html', false);
        die();
    }
    if (Profile::getUser($dta)->ingame == true) {
        $Telegram->sendMessage($chatid, msg('alerGame3'), 'html', false);
        die();
    }
    if (blocked::hasBlocked($chatid, $dta)) {
        $Telegram->sendMessage($chatid, msg('YouBlockedIsUsernotSendMsg'), 'html', false);
        die();
    }
    if (blocked::hasBlocked($dta, $chatid)) {
        $Telegram->sendMessage($chatid, msg('UserIsBlockedYouNotSendMsg'), 'html', false);
        die();
    }
    $Telegram->deleteMessage($chatid, $MessageID);
    $Telegram->sendMessage($chatid, msg('sendRequestGame'), 'html', false, $button_start);
    $cod = $UserProfile->cod;
    $bot = Users::getUser($dta)->bot;
    $cod = '/Profile_' . $cod;
    $beforToken = $Telegram->bot_token;
    $Telegram->bot_token = \Database\Listbots::getBot($bot)->token;
    $button = [
        'inline_keyboard' => [
            [
                ["text" => msg('accept'), "callback_data" => "YesStartGame_$chatid"],
                ["text" => msg('reject'), "callback_data" => "NoStartGame_$chatid"]
            ],
        ],
        'resize_keyboard' => true
    ];
    $Telegram->sendMessage($dta, str_replace(['#user'], [$cod], msg('getRequestGame')), 'html', false, $button);
    $Telegram->bot_token = $beforToken;
} elseif (preg_match('/^([Nn]oStartGame_)(.*)/', $data)) {
    $dta = str_replace(['NoStartGame_'], '', $data);
    $player = $dta;
    $Telegram->deleteMessage($chatid, $MessageID);
    $cod = getStore('getUserProfile')->cod;
    $bot = Users::getUser($player)->bot;
    $cod = '/Profile_' . $cod;
    $beforToken = $Telegram->bot_token;
    $Telegram->bot_token = \Database\Listbots::getBot($bot)->token;
    $Telegram->sendMessage($player, str_replace(['#user'], [$cod], msg('rejectRequestGame')), 'html', false, $button_start);
    $Telegram->bot_token = $beforToken;
} elseif (preg_match('/^([Yy]esStartGame_)(.*)/', $data)) {
    $player = str_replace(['YesStartGame_'], '', $data);
    //region check user In Game

    if (getStore('inGame') == true) {
        $Telegram->sendMessage($chatid, msg('alerGame2'), 'html', false);
        die();
    }
    if (Profile::getUser($player)->ingame == true) {
        $Telegram->sendMessage($chatid, msg('alerGame3'), 'html', false);
        die();
    }
    //endregion check user In Game

    //region check User In PlayerList And Remove

    if (Playerlist::hasPlayer($chatid) == true) {
        Playerlist::removePlayer($chatid);
    }
    if (Playerlist::hasPlayer($player) == true) {
        Playerlist::removePlayer($player);
    }
    //endregion check User In PlayerList And Remove

    $random_id = $func->RandomText(20);
    //region Update User Data
    Users::updateCommand($chatid, 'unGame');
    Users::updateCommand($player, 'unGame');
    Users::updateData($chatid, $random_id);
    Users::updateData($player, $random_id);
    //endregion Update User Data
    //region Get Players Cod
    $GetUserCod = '/Profile_' . getStore('getUserProfile')->cod;
    $GetPlayerCod = '/Profile_' . Profile::getUser($player)->cod;
    //endregion Get Players Cod

    //region Msg Connect
    $Telegram->sendMessage($chatid, str_replace(['#user'], [$GetPlayerCod], msg('connect')), 'html', false, $button_stop);
    $bot = Users::getUser($player)->bot;
    $beforToken = $Telegram->bot_token;
    $Telegram->bot_token = \Database\Listbots::getBot($bot)->token;
    $Telegram->sendMessage($player, str_replace(['#user'], [$GetUserCod], msg('acseptRequestGame')), 'html', false, $button_stop);
    $Telegram->bot_token = $beforToken;
    //endregion Msg Connect
    //region Msg Waring
    $Telegram->sendMessage($chatid, msg('wringgame'), 'html', false, $button_ingame);
    $bot = Users::getUser($player)->bot;
    $beforToken = $Telegram->bot_token;
    $Telegram->bot_token = \Database\Listbots::getBot($bot)->token;
    $Telegram->sendMessage($player, msg('wringgame'), 'html', false, $button_ingame);
    $Telegram->bot_token = $beforToken;
    //endregion Msg Waring

    //region Create Game
    $players = [$player, $chatid];
    $filenamelog = time() . $chatid;


    Games::createGame($random_id, $UsernamebotCur, '', 'friends', $chatid, implode('|', $players));
    functions::createFolderGame($random_id);
    functions::createPlayerList($random_id, $players);
    functions::updateCurrentPlayer($random_id, $player);
    functions::createInfoFile($random_id, [
        'type' => 'friends',
        'count' => 2,
        'sender' => $chatid
    ]);
    if (!fileStorage::checkStore('question')) {
        $arrQuestion = [
            'truth' => array_merge(array_column(Question::getBygenderAndType(1, 'all'), 'question')),
            'truth_male' => array_column(Question::getBygenderAndType(1, 'male'), 'question'),
            'truth_female' => array_column(Question::getBygenderAndType(1, 'female'), 'question'),
            'courage' => array_merge(array_column(Question::getBygender(2, 'all'), 'question')),
            'courage_male' => array_column(Question::getBygenderAndType(2, 'male'), 'question'),
            'courage_female' => array_column(Question::getBygenderAndType(2, 'female'), 'question'),
            'truthPlus' => array_merge(array_column(Question::getBygenderAndType(3, 'all'), 'question')),
            'truth_malePlus' => array_column(Question::getBygenderAndType(3, 'male'), 'question'),
            'truth_femalePlus' => array_column(Question::getBygenderAndType(3, 'female'), 'question'),
            'couragePlus' => array_merge(array_column(Question::getBygender(4, 'all'), 'question')),
            'courage_malePlus' => array_column(Question::getBygenderAndType(4, 'male'), 'question'),
            'courage_femalePlus' => array_column(Question::getBygenderAndType(4, 'female'), 'question'),
        ];
        fileStorage::saveTofile('question', $arrQuestion);
    } else {
        $arrQuestion = fileStorage::readFile('question');
    }

    functions::createQuestion($random_id, '1', array_slice($arrQuestion['truth'], 0, 50));
    functions::createQuestion($random_id, '2', array_slice($arrQuestion['courage'], 0, 50));
    functions::createQuestion($random_id, '1_male', array_slice($arrQuestion['truth_male'], 0, 50));
    functions::createQuestion($random_id, '2_male', array_slice($arrQuestion['courage_male'], 0, 50));
    functions::createQuestion($random_id, '1_female', array_slice($arrQuestion['truth_female'], 0, 50));
    functions::createQuestion($random_id, '2_female', array_slice($arrQuestion['courage_female'], 0, 50));
    functions::createQuestion($random_id, '3', array_slice($arrQuestion['truthPlus'], 0, 50));
    functions::createQuestion($random_id, '4', array_slice($arrQuestion['couragePlus'], 0, 50));
    functions::createQuestion($random_id, '3_male', array_slice($arrQuestion['truth_malePlus'], 0, 50));
    functions::createQuestion($random_id, '4_male', array_slice($arrQuestion['courage_malePlus'], 0, 50));
    functions::createQuestion($random_id, '3_female', array_slice($arrQuestion['truth_femalePlus'], 0, 50));
    functions::createQuestion($random_id, '4_female', array_slice($arrQuestion['courage_femalePlus'], 0, 50));

    Games::updateStatus($random_id, 'start');
    Games::updateType($random_id, 'Un');
    Profile::updateIngame($chatid, true);
    Profile::updateIngame($player, true);

    //endregion Create Game

    $bot = Users::getUser($player)->bot;
    $beforToken = $Telegram->bot_token;
    $Telegram->bot_token = \Database\Listbots::getBot($bot)->token;
    $Telegram->sendMessage($player, msg('qus'), 'html', false, $button_un);
    $Telegram->bot_token = $beforToken;
    $Telegram->sendMessage($chatid, msg('WaitSelect2'), 'html', false);

}