<?php
date_default_timezone_set('Asia/Tehran');

define('DIR_BOT', __DIR__ . '/');
include 'vendor/autoload.php';

include 'btn.php';

use Database\{
    Users,
    Games,
    Sendmsg,
    Listbots,
    Playerlist
};
use inc\functions;


$listbots = Listbots::getBotDef();
define('API_KEY', $listbots->token);
$UsernamebotCur = $listbots->bot;
$Telegram = new \telegramBotApiPhp\telegram(API_KEY);

function sendMessage()
{
    global $Telegram;
    if (!Sendmsg::hasMsgs()) {
        return;
    }
    $Allmsg = Sendmsg::getMsgs();
    foreach ($Allmsg as $msg) {
        $msgSendId = $msg->id;

        $msgid = $msg->msgid;
        $chatidSender = $msg->chatid;
        $last_id = $msg->last_id ?? 1;
        $count = $msg->count;
        $pause = $msg->pause;
        if ($pause != false) {
            continue;
        }

        $users = $last_id == 1 ? Users::getAllStatusActiveUser2(true, 1, 120) : Users::getAllStatusActiveUser2(true, $last_id, 120);

        if ($users->count() == 0) {
            Sendmsg::removeMsg($msgSendId);
            continue;
        }
        foreach ($users as $user) {

            $chatid = $user->chatid;
            $chatsid = $user->id;

            $bot = $user->bot;
            $beforToken = $Telegram->bot_token;
            $Telegram->bot_token = ListBots::getBot($bot)->token;
             $a = $Telegram->copyMessageNonCaption($chatid, $chatidSender, $msgid);
//            $Telegram->SendMessage(82267967,$chatid .' - '.$chatsid, 'html', false);
//
//            $Telegram->SendMessage(82267967,$last_id , 'html', false);
//
            if ($a->getOk() == false) {
                Users::updateStatus($chatid, false);
            }
            $Telegram->bot_token = $beforToken;
            try {
                $count = Sendmsg::getMsg($msgSendId)->count;
            } catch (Exception $e) {
                error_log($e);
                Sendmsg::updateLastId($msgSendId, $last_id + 1);
                break;
            }
            Sendmsg::updaeCount($msgSendId, $count + 1);
        }


        Sendmsg::updateLastId($msgSendId, $last_id + 1);
    }

}

function ifHasGame()
{
    return Games::hasGameByType('via') or Games::hasGameByType('NORMAL') or Games::hasGameByType('PLUS');
}

function closeGame()
{
    if (!ifHasGame()) {
        return;
    }
    global $Telegram;
    $game = Games::getGameByhoursViaType(1);
    if (count($game) == 0) {
        return;
    }
    foreach ($game as $item) {
        $id = $item->rand_id;
        if (!Games::hasGame($id)) {
            continue;
        }
        if (functions::checkGameFolder($id)) {
            functions::deleteFolderAndSub($id);
        }
        Games::removeGame($id);
        if ($item->msg_id == null) {
            continue;
        }
        $beforToken = $Telegram->bot_token;
        $Telegram->bot_token = ListBots::getBot($item->bot)->token;
        $Telegram->EditMessageText($item->chatid, $item->msg_id, msg('close1hoursBot'), 'html', '', true);
        $Telegram->bot_token = $beforToken;
    }
}

function leaveUserOnList()
{
    if (Playerlist::hasPlayers() != true) {
        return;
    }
    global $Telegram;
    $gt = Playerlist::getPlayerByMin(11);
    foreach ($gt as $user) {
        $chid = $user->chatid;
        $bot = Users::getUser($chid)->bot;
        $beforToken = $Telegram->bot_token;
        $Telegram->bot_token = ListBots::getBot($bot)->token;
        Playerlist::removePlayer($chid);
        $Telegram->SendMessage($chid, msg('limitwaitingList'), 'html', false, $button_start);
        $Telegram->bot_token = $beforToken;

    }
}

function giveWeek()
{
    echo 'in';
    global $Telegram, $button_start;
    $give = \Database\Settings::getSettings('giveWeek');
    $users = Users::getAllWeek();
    if ($give == 0) {
        echo 'give';
        return;
    }
    if (count($users) == 0) {
        echo 'user';
        return;
    }
    foreach ($users as $user) {
        $chatid = $user->chatid;
        \Database\Profile::addCoin($chatid, $give);
        Users::updateGiveWeek($chatid);
        $getChat = $Telegram->getChat($chatid);
        if ($getChat->getOk()) {
            $msg = str_replace(
                ['#coin', '#name'],
                [$give, $getChat->getResult()->getFirstName()],
                msg('giveWeek')
            );
            $Telegram->SendMessage($chatid, $msg, 'html', false, $button_start);
        } else {
            Users::updateStatus($chatid, 0);
        }

    }

}

//send message
sendMessage();
//close game
closeGame();
//leave User On List
leaveUserOnList();
giveWeek();
