<?php

use Database\{Channels, Games, Playerlist, Profile, Question, Sendmsg, Settings, Users};
use inc\fileStorage;

if (preg_match('/^\/([Pp]anel)/', $text)) {
    $Telegram->sendMessage($chatid, msg('def'), 'html', false, $button_admin);
} // Close Btn Admin
elseif ($text == patternAdmin('closebtn')) {
    $Telegram->sendMessage($chatid, msg('close'), 'html', false, $button_remove);
} // Amar bot
elseif ($text == patternAdmin('amar')) {
    $Allusers = Users::getCountAll();
    $Allusers = 2000000 + (int)$Allusers;
    $users24 = Users::getCountTodayingUp();
    $week = Users::getCountWeekSingUp();
    $Month = Users::getCountMonthSingUp();
    $notactive = Users::getCountNotActive();
    $online = Profile::getOnlineUsersCount();
    $Game_active = Games::getActiveCount();
    $Game_Un = Games::getGameUn();
    $Game_Normal = Games::getGameNormal();
    $Game_Plus = Games::getGamePlus();
    $Get_GameFriends = Games::getGameFriends();
    $playerlist = Playerlist::getCountPlayers();
    $Question_1 = Question::countQuestion(1);
    $Question_2 = Question::countQuestion(2);
    $Question_3 = Question::countQuestion(3);
    $Question_4 = Question::countQuestion(4);
    $txt = str_replace(
        ['#users', '#last', '#week', '#Month', '#game', '#un', '#match18', '#match1', '#qu1', '#qu2', '#qu3', '#qu4', '#fr', '#online', '#playerlist', '#stop'], [$Allusers, $users24, $week, $Month, $Game_active, $Game_Un, $Game_Plus, $Game_Normal, $Question_1, $Question_2, $Question_3, $Question_4, $Get_GameFriends, $online, $playerlist, $notactive], msg('amar'));
    $Telegram->sendMessage($chatid, $txt, 'html', false);
} // Manage User
elseif ($text == patternAdmin('users')) {
    $Telegram->sendMessage($chatid, msg('def'), 'html', false, $button_users);
} // Back
elseif ($text == patternAdmin('back')) {
    Users::updateCommand($chatid, '');
    Users::updateData($chatid, null);
    $Telegram->sendMessage($chatid, msg('def'), 'html', false, $button_admin);
} // Block
elseif ($text == patternAdmin('block')) {
    Users::updateCommand($chatid, 'block');
    $Telegram->sendMessage($chatid, msg('sendud'), 'html', false, $button_backhome);
} elseif ($command == 'block' && patternAdmin('back')) {
    if (!is_numeric($text)) {
        $Telegram->sendMessage($chatid, msg('justint'), 'html', false, $button_backhome);
        die();
    }
    Users::updateCommand($chatid, null);
    Users::updateActive($text, 0);
    $Telegram->sendMessage($chatid, msg('blockeds'), 'html', false, $button_admin);

} //Unblock
elseif ($text == patternAdmin('unblock')) {
    Users::updateCommand($chatid, 'unblock');
    $Telegram->sendMessage($chatid, msg('sendud'), 'html', false, $button_backhome);
} elseif ($command == 'unblock' && patternAdmin('back')) {
    if (!is_numeric($text)) {
        $Telegram->sendMessage($chatid, msg('justint'), 'html', false, $button_backhome);
        die();
    }
    Users::updateCommand($chatid, null);
    Users::updateActive($text, true);
    $Telegram->sendMessage($chatid, msg('unblocks'), 'html', false, $button_admin);

} //question
elseif ($text == patternAdmin('question')) {
    $Telegram->sendMessage($chatid, msg('def'), 'html', false, $button_question);
} elseif ($text == patternAdmin('question_1')) {
    Users::updateCommand($chatid, 'questionControl');
    Users::updateData($chatid, '1');
    $Telegram->sendMessage($chatid, msg('def'), 'html', false, $button_questionControl);
} elseif ($text == patternAdmin('question_2')) {
    Users::updateCommand($chatid, 'questionControl');
    Users::updateData($chatid, '2');
    $Telegram->sendMessage($chatid, msg('def'), 'html', false, $button_questionControl);
} elseif ($text == patternAdmin('question_3')) {
    Users::updateCommand($chatid, 'questionControl');
    Users::updateData($chatid, '3');
    $Telegram->sendMessage($chatid, msg('def'), 'html', false, $button_questionControl);
} elseif ($text == patternAdmin('question_4')) {
    Users::updateCommand($chatid, 'questionControl');
    Users::updateData($chatid, '4');
    $Telegram->sendMessage($chatid, msg('def'), 'html', false, $button_questionControl);
} // Control question
elseif ($command == 'questionControl' && patternAdmin('back')) {
    // list question
    if ($text == patternAdmin('list_question')) {
        $ses = random_int(1, 9999);
        Settings::updateSettings('sesionWeb', $ses);
        $urlQus = URLBOT . 'webPanel/viewqus.php?session=' . $ses . '&type=' . $user_data;

        $Telegram->sendMessage($chatid, str_replace(['#all', '#male', '#fm'], [$urlQus . '&sex=all', $urlQus . '&sex=male', $urlQus . '&sex=female'], msg('txtListQus')), 'html', false);

    } elseif ($text == patternAdmin('add_question')) {
        $button = [
            'inline_keyboard' => [
                [
                    ["text" => msg('allsex'), "callback_data" => "sendAdminQus_all_" . $user_data],
                ],
                [
                    ["text" => msg('male'), "callback_data" => "sendAdminQus_male_" . $user_data],
                    ["text" => msg('female'), "callback_data" => "sendAdminQus_female_" . $user_data]
                ],
            ],
            'resize_keyboard' => true
        ];
        $Telegram->sendMessage($chatid, msg('selecQusGen'), 'html', false, $button);
        Users::updateCommand($chatid, 'addquestion');

    } elseif ($text == patternAdmin('remove_question')) {
        Users::updateCommand($chatid, 'removequestion');
        $Telegram->sendMessage($chatid, msg('sendquestion'), 'html', false, $button_backhome);
    }
} elseif (preg_match('/^(sendAdminQus_)(.*)/', $data)) {
    $dta = str_replace(['sendAdminQus_'], '', $data);
    Users::updateCommand($chatid, 'SendAdminNewQus');
    Users::updateData($chatid, $dta);
    $Telegram->sendMessage($chatid, msg('sendquestion'), 'html', false, $button_cancels);
} // Add question
elseif ($command == 'SendAdminNewQus' && patternAdmin('back')) {
    $ex = explode('_', $user_data);
    if (Question::hasQuestion($ex[1], $text, $ex[0]) == false) {
        Question::addQuestion($ex[1], $text, $ex[0]);
        $Telegram->sendMessage($chatid, msg('done'), 'html', false, $button_backhome);
        fileStorage::removeFile('question');
    } else {
        $Telegram->sendMessage($chatid, msg('Alreadyquestion'), 'html', false, $button_backhome);
    }
} // Remove question
elseif ($command == 'removequestion' && patternAdmin('back')) {
    if (Question::hasQuestion($user_data, $text) == false) {
        $Telegram->sendMessage($chatid, msg('NotFoundquestion'), 'html', false, $button_backhome);
        die();
    }

    Question::removeQuestion($user_data, $text);
    fileStorage::removeFile('question');
    Users::updateCommand($chatid, null);
    Users::updateData($chatid, null);
    $Telegram->sendMessage($chatid, msg('done'), 'html', false, $button_question);

}  //Channels
elseif ($text == patternAdmin('channels')) {
    $lock = Settings::getSettings('lock');
    $button_channel = $button_channelon;
    if ($lock == 0) {
        $button_channel = $button_channeloff;
    }
    $Telegram->sendMessage($chatid, msg('def'), 'html', false, $button_channel);
} //on Lock
elseif ($text == patternAdmin('onLock')) {
    Settings::updateSettings('lock', 1);
    fileStorage::removeFile('bot');
    $Telegram->sendMessage($chatid, msg('done'), 'html', false, $button_channelon);
}//off Lock
elseif ($text == patternAdmin('offLock')) {
    Settings::updateSettings('lock', '0');
    fileStorage::removeFile('bot');
    $Telegram->sendMessage($chatid, msg('done'), 'html', false, $button_channeloff);
} //Add Channel
elseif ($text == patternAdmin('AddChannel')) {
    Users::updateCommand($chatid, 'channelAdd');
    $Telegram->sendMessage($chatid, msg('setChannel'), 'html', false, $button_backhome);
} elseif ($command == 'channelAdd' && $text != patternAdmin('back')) {
    if (!$Telegram->message()->issetForwardFromChat()) {
        $Telegram->sendMessage($chatid, msg('plsForward'), 'html', false, $button_backhome);
        die();
    }
    $chchatid = (string)$Telegram->message()->getForwardFromChat()->getId();
    if (Channels::hasChannel($chchatid) == false and $Telegram->message()->getForwardFromChat()->getType() == 'channel') {
        $addchh = Channels::addChannel($chchatid);
    }
    if ($addchh) {
        Channels::updateLinks($addchh, $Telegram->createChatInviteLink($Telegram->message()->getForwardFromChat()->getId())->getResult()->getInviteLink());
    }
    fileStorage::removeFile('bot');
    Users::updateCommand($chatid, null);

    $lock = Settings::getSettings('lock');
    $button_channel = $button_channelon;
    if ($lock == 0) {
        $button_channel = $button_channeloff;
    }
    $Telegram->sendMessage($chatid, msg('done'), 'html', false, $button_channel);
} //Remove Channel
elseif (preg_match('/^\/([Rr]emoveChannel_)(.*)/', $text)) {
    $dta = str_replace(['/removeChannel_'], '', $text);
    $ch = Channels::getById($dta);

    $btn = [["text" => msg('yes'), "callback_data" => "yesRemoveChannel_{$dta}"], ["text" => msg('no'), "callback_data" => "noRemoveChannel_{$dta}"]];
    $getch = $Telegram->getChat($ch->chatid);
    if ($getch->getOk()) {
        $name = $getch->getResult()->getTitle();
    } else {
        $name = $ch->chatid;
    }
    $Telegram->sendMessage($chatid, str_replace('#ch', $name, msg('realyDelete')), 'html', false, [
        'inline_keyboard' =>
            [$btn]
        ,
        'resize_keyboard' => true
    ]);

} //Remove Channel
elseif (preg_match('/^([Nn]oRemoveChannel_)(.*)/', $data)) {
    $Telegram->editMessageText($chatid, $MessageID, msg('done'), 'html', '', true);
} elseif (preg_match('/^([Yy]esRemoveChannel_)(.*)/', $data)) {
    $dta = str_replace(['yesRemoveChannel_'], '', $data);
    Channels::removeChannelById($dta);
    fileStorage::removeFile('bot');
    $Telegram->editMessageText($chatid, $MessageID, msg('done'), 'html', '', true);
}//list Channel
elseif ($text == patternAdmin('ListChannel')) {
    $list = Channels::getAll();
    if (count($list) == 0) {
        $Telegram->sendMessage($chatid, msg('NotChannel'), 'html', false);
        die();
    }
    $btn[] = [["text" => msg('item'), "callback_data" => "none"]];
    foreach ($list as $channels) {
        $getch = $Telegram->getChat($channels->chatid);
        if ($getch->getOk()) {
            $name = $getch->getResult()->getTitle();
            $btn[] = [["text" => $name, "callback_data" => "GetChannel_" . $channels->chatid]];
        } else {
            $Telegram->sendMessage($chatid, $channels->chatid . ' ' . $getch->getDescription(), 'html');
            $btn[] = [["text" => $channels->chatid, "callback_data" => "GetChannel_" . $channels->chatid]];
        }
    }
    $Telegram->sendMessage($chatid, msg('def'), 'html', false, [
        'inline_keyboard' =>
            $btn
        ,
        'resize_keyboard' => true
    ]);
} elseif (preg_match('/^([Gg]etChannel_)(.*)/', $data)) {
    $dta = str_replace(['GetChannel_'], '', $data);
    $ch = Channels::getChannel($dta);
    $co = $ch->count;
    $link = $ch->link;
    $id = $ch->id;
    $Telegram->editMessageText($chatid, $MessageID, str_replace(['#user', '#ch', '#command'], [$co, $link, '/removeChannel_' . $id], msg('txtChannel')), 'html', '', true);
} // Send msg for users
elseif ($text == patternAdmin('sendmsg')) {
    $Telegram->sendMessage($chatid, msg('def'), 'html', false, $button_sendmsg);
} elseif ($text == patternAdmin('listmsg')) {
    if (Sendmsg::hasMsgs() == false) {
        $Telegram->sendMessage($chatid, msg('notfoundmsg'), 'html', false);
        die();
    }
    Users::updateCommand($chatid, 'SelectMsg');
    $btn = [];
    $btn[] = [['text' => patternAdmin('back')]];
    foreach (Sendmsg::getMsgs() as $item) {
        $btn[] = [['text' => $item->id]];
    }
    $Telegram->sendMessage($chatid, msg('def'), 'html', false, [
        'keyboard' => $btn,
        'resize_keyboard' => true
    ]);

} elseif ($text == patternAdmin('sendmsgs')) {
    Users::updateCommand($chatid, 'sendmsg');
    $Telegram->sendMessage($chatid, msg('sendForUser'), 'html', false, $button_backhome);
} elseif ($command == 'sendmsg' && patternAdmin('back')) {
    $msgCopy = $Telegram->copyMessageNonCaption(CHANNELFORWARD, $chatid, $MessageID);
    $Telegram->sendMessage(CHANNELFORWARD, $chatid);
    if ($msgCopy->getOk()) {
        Users::updateCommand($chatid, null);
        $idmsg = Sendmsg::addMsg(CHANNELFORWARD, $msgCopy->getResult()->getMessageId());
        $Telegram->sendMessage($chatid, str_replace(['#cod'], [$idmsg], msg('donesendmsg')), 'html', false, $button_sendmsg);
    } else {
        $Telegram->sendMessage($chatid, msg('un_error2'), 'html', false, $button_sendmsg);
    }


} elseif ($command == 'SelectMsg' && patternAdmin('back')) {
    $get = Sendmsg::getMsg($text);
    if (!empty($get)) {
        Users::updateCommand($chatid, null);
        $Telegram->copyMessageNonCaption($chatid, $get->chatid, $get->msgid);
        $Telegram->sendMessage($chatid, str_replace(['#up', '#date', '#count', '#command', '#other'], [
            $get->update_at,
            $get->create_at,
            $get->count,
            '/canselsendmsg_' . $text,
            $get->pause == false ? '/pauseMsg_' . $text : '/playMsg_' . $text
        ], msg('infomsg')), 'html', false, $button_sendmsg);

    }
} elseif (preg_match('/^\/([Cc]anselsendmsg_)(.*)/', $text)) {
    $ex = explode('_', str_replace('/canselsendmsg_', '', $text))[0];
    Sendmsg::removeMsg($ex);
    $Telegram->sendMessage($chatid, msg('doneCanselsendmsg'), 'html', false);
} elseif (preg_match('/^\/([Pp]auseMsg_)(.*)/', $text)) {
    $ex = explode('_', str_replace('/pauseMsg_', '', $text))[0];
    Sendmsg::updatePause($ex, true);
    $Telegram->sendMessage($chatid, msg('done'), 'html', false);
} elseif (preg_match('/^\/([Pp]layMsg_)(.*)/', $text)) {
    $ex = explode('_', str_replace('/playMsg_', '', $text))[0];
    Sendmsg::updatePause($ex, false);
    $Telegram->sendMessage($chatid, msg('done'), 'html', false);
}
