<?php

use Database\{Users, Profile, Follow, blocked, Listbots};
use inc\functions;
use Carbon\Carbon;

if ($text == patternAdmin('addCoin')) {
    Users::updateCommand($chatid, 'addCoinForUser');
    $Telegram->SendMessage($chatid, msg('sendId'), 'html', false, $button_backhome);
} elseif ($command == 'addCoinForUser' && $text != patternAdmin('back')) {
    Users::updateCommand($chatid, 'addCoinForUser_step2');
    Users::updateData($chatid, $text);
    $Telegram->SendMessage($chatid, msg('snedCoin'), 'html', false, $button_backhome);
} elseif ($command == 'addCoinForUser_step2' && $text != patternAdmin('back')) {
    $coin = Profile::getUser($user_data)->coin;
    $newcoin = $coin < 0 ? $text : $coin + $text;
    Profile::updateCoin($user_data, $newcoin);
    Users::updateCommand($chatid, null);
    Users::updateData($chatid, null);
    $Telegram->SendMessage($chatid, msg('done'), 'html', false, $button_users);
    $Telegram->SendMessage($user_data, str_replace(['#newcoin', '#coin'], [$newcoin, $text], msg('alertAddCoin')), 'html', false);
} elseif ($text == patternAdmin('removeCoin')) {
    Users::updateCommand($chatid, 'removeCoinForUser');
    $Telegram->SendMessage($chatid, msg('sendId'), 'html', false, $button_backhome);
} elseif ($command == 'removeCoinForUser' && $text != patternAdmin('back')) {
    Users::updateCommand($chatid, 'removeCoinForUser_step2');
    Users::updateData($chatid, $text);
    $Telegram->SendMessage($chatid, msg('snedCoin'), 'html', false, $button_backhome);
} elseif ($command == 'removeCoinForUser_step2' && $text != patternAdmin('back')) {
    $coin = Profile::getUser($user_data)->coin;
    $coin = $coin < 0 ? 0 : $coin - $text;
    Profile::updateCoin($user_data, $coin);
    Users::updateCommand($chatid, null);
    Users::updateData($chatid, null);
    $Telegram->SendMessage($chatid, msg('done'), 'html', false, $button_users);
} elseif ($text == patternAdmin('searchUser')) {
    $Telegram->SendMessage($chatid, msg('def'), 'html', false, $button_searchUser);
} elseif ($text == patternAdmin('searchUserById')) {
    Users::updateCommand($chatid, 'searchUserById');
    $Telegram->SendMessage($chatid, msg('sendId'), 'html', false, $button_backhome);
} elseif ($text == patternAdmin('searchUserByCode')) {
    Users::updateCommand($chatid, 'searchUserByCode');
    $Telegram->SendMessage($chatid, msg('sendId'), 'html', false, $button_backhome);
} elseif (($command == 'searchUserById' or $command == 'searchUserByCode') && $text != patternAdmin('back')) {
    $text = str_replace(['/Profile_'], '', $text);

    if ($command == 'searchUserByCode') {
        if (Profile::checkUserByCode($text)) {
            $text = Profile::getUserByCode($text)->chatid;
        }
    }
    if (Users::check($text)) {
        Users::updateData($chatid, null);

        $getUser = Users::getUser($text);
        $bot = Users::getUser($text)->bot;
        $getProfile = Profile::getUser($text);
        $tokenBefore = $Telegram->bot_token;
        $Telegram->bot_token = Listbots::getBot($bot)->token;
        $getchat = $Telegram->getChat($text);
        $fname = 'none';
        $username = 'none';
        if ($getchat->getOk()) {
            $fname=str_replace(['<','>'], '', $getchat->getResult()->getFirstName());

            $fname = functions::generationMentions($text, $fname);
            $username = $getchat->getResult()->issetUsername() ? $getchat->getResult()->getUsername() : 'none';
        }
        $Telegram->bot_token = $tokenBefore;
        $getUserBlock = blocked::countBlock($text);
        $getUserBlocked = blocked::countBlocked($text);
        $countFollow = Follow::countFollow($text);
        $msg = str_replace([
            '#name',
            '#username',
            '#status',
            '#usercode',
            '#coin',
            '#game',
            '#invite',
            '#report',
            '#userblock',
            '#userBlocked',
            '#follow',
            '#lastonline',
            '#command',
            '#data',
            '#chatid',
        ],
            [
                $fname,
                $username,
                $getUser->active == true ? 'فعال' : 'غیرفعال',
                '/Profile_' . $getProfile->cod,
                $getProfile->coin,
                $getProfile->ingame == true ? 'درون بازی' : 'درون بازی نیست',
                $getProfile->invite,
                $getProfile->report,
                $getUserBlock,
                $getUserBlocked,
                $countFollow,
                Carbon::createFromTimestamp($getProfile->lastonline, 'Asia/Tehran')->diffForHumans(),
                $getUser->command,
                $getUser->data,
                $text

            ], msg('userSearched'));
        $Telegram->SendMessage($chatid, $msg, 'html', false, $button_searchUser);

    } else {
        $Telegram->SendMessage($chatid, msg('NotfoundUser'), 'html', false, $button_backhome);
    }
} elseif ($text == patternAdmin('giveCoin')) {
    Users::updateCommand($chatid, 'giveCoin');
    $Telegram->SendMessage($chatid, msg('snedCoin'), 'html', false, $button_backhome);
} elseif ($command == 'giveCoin' && $text != patternAdmin('back')) {
    Profile::giveCoin($text);
    Users::updateCommand($chatid, null);
    $Telegram->SendMessage($chatid, msg('done'), 'html', false, $button_users);
}

