<?php

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

if ($data == 'profile') {
    $profileUser = Profile::getUser($chatid);
    if (getStore('inGame') == true) {
        $Telegram->editMessageText($chatid, $MessageID, msg('alerGame2'), 'html', '', true, $button_start);
        die();
    }
    $photo = NOPHOTO;


    if (!is_null($profileUser->photo)) {
        $photo = $profileUser->photo;
    }
    $name = is_null($profileUser->name) ? '?' : $profileUser->name;
    $age = is_null($profileUser->age) ? '?' : $profileUser->age;
    $state = is_null($profileUser->state) ? '?' : $profileUser->state;
    $gender = is_null($profileUser->gender) ? '?' : $profileUser->gender;
    $city = is_null($profileUser->city) ? '?' : $profileUser->city;
    $point = $profileUser->point;
    $usercod = '/Profile_' . $profileUser->cod;
    $coin = $profileUser->coin;
    $invite = $profileUser->invite;
    $getUserBlock = blocked::countBlock($chatid);
    $countFollow = Follow::countFollow($chatid);
    $ProfText = str_replace(['#name', '#state', '#ciity', '#age', '#gender', '#point', '#usercod', '#coin', '#invite', '#block', '#friend'], [$name, $state, $city, $age, str_replace(['female', 'male'], ['خانم', 'آقا'], $gender), $point, $usercod, $coin, $invite, $getUserBlock, $countFollow], msg('YouProfile'));
    if ($photo == NOPHOTO) {
        $Telegram->sendPhoto($chatid, $photo, $ProfText, 'html', false, $button_profile, $Telegram->MessageID());
    } else {
        $Telegram->copyMessage($chatid, CHANNELFORWARD, $photo, $ProfText, 'html', $button_profile);
    }
}
//endregion Profile Data

//region Cancel
elseif ($text == Getpattern('cancels')) {
    Users::updateData($chatid, null);
    Users::updateCommand($chatid, null);
    $a = $Telegram->sendMessage($chatid, '.', 'html', false, $button_remove);
    if ($a->getOk()) {
        $Telegram->deleteMessage($chatid, $a->getResult()->getMessageId());
    }
    $Telegram->sendMessage($chatid, msg('def'), 'html', false, $button_start);
}
//endregion Cancel

//region Set New Name
elseif ($data == 'editename') {
    Users::updateCommand($chatid, 'setname');
    $Telegram->sendMessage($chatid, msg('sendname'), 'html', false, $button_cancels);
} elseif ($command == 'setname' and $data != 'back') {
    if ($text) {
        Users::updateCommand($chatid, null);
        Profile::updateName($chatid, $text);
        $Telegram->sendMessage($chatid, msg('done'), 'html', false, $button_remove);
        $Telegram->sendMessage($chatid, msg('def'), 'html', false, $button_profile);
    } else {
        $Telegram->sendMessage($chatid, msg('string'), 'html', false);
    }
}
//endregion Set New Name

//region Set New Age
elseif ($data == 'editeage') {
    Users::updateCommand($chatid, 'setage');
    $btn = [];
    $btn[] = [['text' => Getpattern('cancels')]];
    $col = [];
    $i = 7;
    while ($i < 100) {
        if ($i % 2) {
            $col[] = ['text' => $i];
            $btn[] = $col;
            $col = [];
        } else {
            $col[] = ['text' => $i];
        }
        $i++;
    }
    $Telegram->sendMessage($chatid, msg('def'), 'html', false, [
        'keyboard' => $btn,
        'resize_keyboard' => true
    ]);

} elseif ($command == 'setage' and $data != 'back') {
    if ($text >= 7 and $text <= 100) {
        Users::updateCommand($chatid, null);
        Profile::updateAge($chatid, $text);
        $Telegram->sendMessage($chatid, msg('done'), 'html', false, $button_remove);
        $Telegram->sendMessage($chatid, msg('def'), 'html', false, $button_profile);
    } else {
        $Telegram->sendMessage($chatid, msg('JustSelectAge'), 'html', false);
    }
}
//endregion Set New Age

//region Set New State
elseif ($data == 'editestate') {
    Users::updateCommand($chatid, 'setstate');
    $btn = [];
    $btn[] = [['text' => Getpattern('cancels')]];
    $col = [];
    $i = 1;
    foreach ($ListState as $name) {
        if ($i % 2) {
            $col[] = ['text' => $name];
            $btn[] = $col;
            $col = [];
        } else {
            $col[] = ['text' => $name];
        }
        $i++;
    }

    $Telegram->sendMessage($chatid, msg('def'), 'html', false, [
        'keyboard' => $btn,
        'resize_keyboard' => true
    ]);

} elseif ($command == 'setstate' and $data != 'back') {
    if (in_array($text, $ListState)) {
        Users::updateCommand($chatid, null);
        Profile::updateState($chatid, $text);
        $Telegram->sendMessage($chatid, msg('done'), 'html', false, $button_remove);
        $Telegram->sendMessage($chatid, msg('def'), 'html', false, $button_profile);
    } else {
        $Telegram->sendMessage($chatid, msg('JustSelect'), 'html', false);

    }
}
//endregion Set New State

//region Set New city
elseif ($data == 'editecity') {
    Users::updateCommand($chatid, 'setcity');
    $Telegram->sendMessage($chatid, msg('sendcity'), 'html', false, $button_cancels);
} elseif ($command == 'setcity' and $data != 'back') {
    if ($text) {
        Users::updateCommand($chatid, null);
        Profile::updateCity($chatid, $text);
        $Telegram->sendMessage($chatid, msg('done'), 'html', false, $button_remove);
        $Telegram->sendMessage($chatid, msg('def'), 'html', false, $button_profile);
    } else {
        $Telegram->sendMessage($chatid, msg('string'), 'html', false);

    }
}
//endregion Set New city

//region Set New Gender
elseif ($data == 'editegender') {
    Users::updateCommand($chatid, 'setgender');
    $Telegram->sendMessage($chatid, msg('def'), 'html', false, $button_profilegenders);

} elseif ($command == 'setgender' and $data != 'back') {
    if (in_array($text, $ListGender)) {
        Users::updateCommand($chatid, null);
        Profile::updateGender($chatid, str_replace(['خانم', 'آقا'], ['female', 'male'], $text));
        $Telegram->sendMessage($chatid, msg('done'), 'html', false, $button_remove);
        $Telegram->sendMessage($chatid, msg('def'), 'html', false, $button_profile);
    } else {
        $Telegram->sendMessage($chatid, msg('JustSelect'), 'html', false);
    }
}
//endregion Set New Gender

//region Set New Photo
elseif ($data == 'editephoto') {
    Users::updateCommand($chatid, 'setphoto');
    $Telegram->sendMessage($chatid, msg('sendphoto'), 'html', false, $button_cancels);
} elseif ($command == 'setphoto' and $data != 'back') {
    if ($Telegram->isPhoto()) {
        $msgCopy = $Telegram->copyMessageNonCaption(CHANNELFORWARD, $chatid, $MessageID);
        $Telegram->sendMessage(CHANNELFORWARD, 'set photo #profile ' . $chatid);
        Users::updateCommand($chatid, null);
        Profile::updatePhoto($chatid, $msgCopy->getResult()->getMessageId());
        $Telegram->sendMessage($chatid, msg('done'), 'html', false, $button_remove);
        $Telegram->sendMessage($chatid, msg('def'), 'html', false, $button_profile);
    } else {
        $Telegram->sendMessage($chatid, msg('photo'), 'html', false);
    }
}
//endregion Set New Photo

//region Blocked List
elseif ($data == 'ListBlocked') {

    if (blocked::hasBlockeds($chatid)) {
        $List = '';
        foreach (blocked::getBlockeds($chatid) as $item) {
            $friend = Profile::getUser($item->userid);
            $usercod = '/Profile_' . $friend->cod;
            $name = $friend->name ?? 'بدون نام';
            $age = $friend->age ?? 'سن وارد نشده';
            $List .= '➖ ' . $name . ' - ' . $age . "\n$usercod\n";
        }

        $Telegram->sendMessage($chatid, str_replace('#list', $List, msg('ListBlockeds')), 'html', false, $button_remove);

    } else {
        $Telegram->sendMessage($chatid, msg('NoBlock'), 'html', false, $button_remove);
    }
}
//endregion Blocked List

//region Contact List
elseif ($data == 'Contact') {

    if (Follow::hasFollow($chatid)) {

        $List = '';
        foreach (Follow::getFollows($chatid, false) as $item) {
            $friend = Profile::getUser($item->userid);
            $usercod = '/Profile_' . $friend->cod;
            $name = $friend->name ?? 'بدون نام';
            $age = $friend->age ?? 'سن وارد نشده';
            $List .= '➖ ' . $name . ' - ' . $age . "\n$usercod\n";
        }

        $Telegram->sendMessage($chatid, str_replace('#list', $List, msg('ListFreinds')), 'html', false, $button_remove);

    } else {
        $Telegram->sendMessage($chatid, msg('NoFreinds'), 'html', false, $button_remove);
    }
}
//endregion Contact List

//region Get User Profile by command /profile_*
elseif (preg_match('/^\/([Pp]rofile_)(.*)/', $text) and !in_array($command, ['sendMsgForUserByCode_step1'])) {
    $txt = str_replace(['/Profile_', '/profile_'], '', $text);
    if (Profile::checkUserByCode($txt)) {
        $ProfileUser = Profile::getUserByCode($txt);

        $photo = NOPHOTO;
        if (!is_null($ProfileUser->photo)) {
            $photo = $ProfileUser->photo;
        }
        $name = is_null($ProfileUser->name) ? '?' : $ProfileUser->name;
        $age = is_null($ProfileUser->age) ? '?' : $ProfileUser->age;
        $state = is_null($ProfileUser->state) ? '?' : $ProfileUser->state;
        $gender = is_null($ProfileUser->gender) ? '?' : $ProfileUser->gender;
        $city = is_null($ProfileUser->city) ? '?' : $ProfileUser->city;
        $coin = $ProfileUser->coin;
        $invite = $ProfileUser->invite;
        $point = $ProfileUser->point;
        $usercod = '/Profile_' . $ProfileUser->cod;
        $ProfileChatId = $ProfileUser->chatid;
        $getUserBlock = blocked::countBlock($ProfileChatId);
        $countFollow = Follow::countFollow($ProfileChatId);
        if ($ProfileChatId != $chatid) {

            $CheckFollow = Follow::checkFollows($chatid, $ProfileChatId);
            $CheckBlock = blocked::hasBlocked($chatid, $ProfileChatId);
            $FollowTxt = msg('RemoveContact');
            $FollowData = 'RemoveContact_' . $ProfileChatId;
            $BlockedTxt = msg('BlockUser');
            $BlockedData = 'BlcokUser_' . $ProfileChatId;
            if ($CheckFollow == false) {
                $FollowTxt = msg('AddContact');
                $FollowData = 'AddContact_' . $ProfileChatId;
            }
            if ($CheckBlock) {
                $BlockedTxt = msg('UnlockUser');
                $BlockedData = 'UnblcokUser_' . $ProfileChatId;
            }
            $button = [
                'inline_keyboard' => [
                    [
                        ["text" => msg('StartGameByFrinds'), "callback_data" => "StartGameByFrinds_$ProfileChatId"],
                        ["text" => msg('SendMsg'), "callback_data" => "SendMsgUser_$ProfileChatId"],
                    ],
                    [
                        ["text" => $FollowTxt, "callback_data" => $FollowData],
                        ["text" => $BlockedTxt, "callback_data" => $BlockedData],
                    ],
                    [
                        ["text" => msg('ReportUser'), "callback_data" => "ReportUser_$ProfileChatId"],
                    ],
                ],
                'resize_keyboard' => true
            ];
            $getUpdateTime = Carbon::createFromTimestamp($ProfileUser->lastonline, 'Asia/Tehran')->diffForHumans();

            if (Profile::getUser($ProfileChatId)->ingame == true) {
                $getUpdateTime = msg('ingame') . $getUpdateTime;
            }

            if (getStore('inGame') == true) {
                $button = [
                    'inline_keyboard' => [
                        [
                            ["text" => $FollowTxt, "callback_data" => $FollowData],
                            ["text" => $BlockedTxt, "callback_data" => $BlockedData],
                        ],
                        [
                            ["text" => msg('ReportUser'), "callback_data" => "ReportUser_$ProfileChatId"],
                        ],
                    ],
                    'resize_keyboard' => true
                ];
            }
            $ProfText = str_replace(['#name', '#state', '#ciity', '#age', '#gender', '#point', '#usercod', '#status'], [$name, $state, $city, $age, str_replace(['female', 'male'], ['خانم', 'آقا'], $gender), $point, $usercod, $getUpdateTime], msg('UserProfile'));
            if ($photo == NOPHOTO) {
                $Telegram->sendPhoto($chatid, $photo, $ProfText, 'html', false, $button);
            } else {
                $msgCopy = $Telegram->copyMessage($chatid, CHANNELFORWARD, $photo, $ProfText, 'html', $button);
            }

        } else {
            $ProfText = str_replace(['#name', '#state', '#ciity', '#age', '#gender', '#point', '#usercod', '#coin', '#invite', '#block', '#friend'], [$name, $state, $city, $age, str_replace(['female', 'male'], ['خانم', 'آقا'], $gender), $point, $usercod, $coin, $invite, $getUserBlock, $countFollow], msg('YouProfile'));
            if ($photo == NOPHOTO) {
                $Telegram->sendPhoto($chatid, $photo, $ProfText, 'html', false, $button_profile);
            } else {
                $msgCopy = $Telegram->copyMessage($chatid, CHANNELFORWARD, $photo, $ProfText, 'html', $button_profile);
            }

        }

    } else {
        $Telegram->sendMessage($chatid, msg('NotfoundUser'), 'html', false, $button_remove);

    }

}
//endregion Get User Profile by command /profile_*

//region SendMsgForUser

elseif (preg_match('/^([Ss]endMsgUser_)(.*)/', $data)) {
    $dta = str_replace(['SendMsgUser_'], '', $data);
    if (getStore('inGame') == true) {
        $Telegram->sendMessage($chatid, msg('alerGame2'), 'html', false);
        die();
    }
    if (blocked::hasBlocked($chatid, $dta)) {
        $Telegram->sendMessage($chatid, msg('YouBlockedIsUsernotSendMsg'), 'html', false);
    } elseif (blocked::hasBlocked($dta, $chatid)) {
        $Telegram->sendMessage($chatid, msg('UserIsBlockedYouNotSendMsg'), 'html', false);
    } else {
        Users::updateCommand($chatid, 'SendForUser');
        Users::updateData($chatid, $dta);
        $Telegram->sendMessage($chatid, msg('SendDircet'), 'html', false, $button_cancels);
    }
} elseif ($command == 'SendForUser' and $data != 'back') {
    if ($text) {
        if (!$data and $Telegram->message()->issetEntities()) {
            $Telegram->sendMessage($chatid, msg('errorUrl'), 'html', false);
            die();
        }
        Users::updateCommand($chatid, null);
        Users::updateData($chatid, null);
        $msgid = Msgdirect::addMsg($chatid, $user_data, $text);
        $button_un = [
            'inline_keyboard' => [
                [
                    ["text" => msg('yes'), "callback_data" => "YesSendMsgUser_$msgid"],
                    ["text" => msg('no'), "callback_data" => "NoSendMsgUser_$msgid"]
                ],
            ],
            'resize_keyboard' => true
        ];
        $Telegram->sendMessage($chatid, str_replace('#msg', $text, msg('RealySend')), 'html', false, $button_un);


    } else {
        $Telegram->sendMessage($chatid, msg('string'), 'html', false, $button_cancels);
    }
} elseif (preg_match('/^([Nn]oSendMsgUser_)(.*)/', $data)) {
    $dta = str_replace(['NoSendMsgUser_'], '', $data);
    if (Msgdirect::hasMsgById($dta)) {
        Msgdirect::removeMsg($dta);
        $Telegram->editMessageText($chatid, $MessageID, msg('doneCancel'), 'html', '', true, $button_start);
    }
} elseif (preg_match('/^([Yy]esSendMsgUser_)(.*)/', $data)) {
    $dta = str_replace(['YesSendMsgUser_'], '', $data);
    $MsgDc = Msgdirect::getMsgById($dta);
    if ($MsgDc) {
        $bot = Users::getUser($MsgDc->userid)->bot;
        $cod = getStore('getUserProfile')->cod;
        $cod = '/Profile_' . $cod;
        $beforToken = $Telegram->bot_token;
        $Telegram->bot_token = Listbots::getBot($bot)->token;
        $Telegram->sendMessage($MsgDc->userid, str_replace(['#user', '#msg'], [$cod, $MsgDc->msg], msg('ReceivedMsg')), 'html', false);
        $Telegram->bot_token = $beforToken;
        $a = $Telegram->sendMessage($chatid, '.', 'html', false, $button_remove);
        $Telegram->deleteMessage($chatid, $a->getResult()->getMessageId());
        $Telegram->editMessageText($chatid, $MessageID, msg('doneSendMsg'), 'html', '', true, $button_start);
        Msgdirect::removeMsg($dta);

    }
}
//endregion SendMsgForUser

//region Add New Contact
elseif (preg_match('/^([Aa]ddContact_)(.*)/', $data)) {
    $dta = str_replace(['AddContact_'], '', $data);

    if (Follow::checkFollows($chatid, $dta) == false) {
        Follow::addFllow($chatid, $dta);
        $Telegram->deleteMessage($chatid, $MessageID);
        $Telegram->sendMessage($chatid, msg('doneAddFriend'));
        $beforToken = $Telegram->bot_token;
        $bot = Users::getUser($dta)->bot;
        $cod = Profile::getUser($chatid)->cod;
        $cod = '/Profile_' . $cod;
        $Telegram->bot_token = Listbots::getBot($bot)->token;
        $button = [
            'inline_keyboard' => [
                [
                    ["text" => msg('yes'), "callback_data" => "ConfirmationNewFriend_$chatid"],
                ],
            ],
            'resize_keyboard' => true
        ];
        $Telegram->sendMessage($dta, str_replace(['#user'], [$cod], msg('ReceivedNewFriend')), 'html', false, $button);
        $Telegram->bot_token = $beforToken;
    }
} //endregion Add New Contact

elseif (preg_match('/^([Cc]onfirmationNewFriend_)(.*)/', $data)) {
    $dta = str_replace(['ConfirmationNewFriend_'], '', $data);
    $Telegram->deleteMessage($chatid, $MessageID);
    if (Follow::checkFollows($chatid, $dta) == false) {
        Follow::addFllow($chatid, $dta);
        $Telegram->sendMessage($chatid, msg('done'));
    } else {
        $Telegram->sendMessage($chatid, msg('AlreadyFriends'));

    }
} //region Remove Contact
elseif (preg_match('/^([Rr]emoveContact_)(.*)/', $data)) {
    $dta = str_replace(['RemoveContact_'], '', $data);
    if (Follow::checkFollows($chatid, $dta) == true) {
        Follow::removeFollow($chatid, $dta);
        $Telegram->deleteMessage($chatid, $MessageID);
        $Telegram->sendMessage($chatid, msg('doneRemoveFriend'));
    }
}
//endregion Remove Contact

//region Block Contact
elseif (preg_match('/^([Bb]lcokUser_)(.*)/', $data)) {
    $dta = str_replace(['BlcokUser_'], '', $data);
    if (blocked::hasBlocked($chatid, $dta) == false) {
        blocked::addBlocked($chatid, $dta);
        $Telegram->deleteMessage($chatid, $MessageID);
        $Telegram->sendMessage($chatid, msg('blockeds'));
    }
}

//endregion Block Contact

//region Unblock
elseif (preg_match('/^([Uu]nblcokUser_)(.*)/', $data)) {
    $dta = str_replace(['UnblcokUser_'], '', $data);
    if (blocked::hasBlocked($chatid, $dta) == true) {
        blocked::removeBlocked($chatid, $dta);
        $Telegram->deleteMessage($chatid, $MessageID);
        $Telegram->sendMessage($chatid, msg('unblocks'));
    }
}
//endregion Unblock

//region Report User
elseif (preg_match('/^([Rr]eportUser_)(.*)/', $data)) {
    $dta = str_replace(['ReportUser_'], '', $data);
    $button = [
        'inline_keyboard' => [
            [
                ["text" => msg('Report_1'), "callback_data" => "GoToReportUser_1_$dta"],
                ["text" => msg('Report_2'), "callback_data" => "GoToReportUser_2_$dta"],
            ],
            [
                ["text" => msg('Report_3'), "callback_data" => "GoToReportUser_3_$dta"],
                ["text" => msg('Report_4'), "callback_data" => "GoToReportUser_4_$dta"],
            ],
            [
                ["text" => msg('Report_5'), "callback_data" => "GoToReportUser_5_$dta"],
                ["text" => msg('Report_6'), "callback_data" => "GoToReportUser_6_$dta"],
            ],
            [
                ["text" => msg('backmenu'), "callback_data" => "back"],
            ],
        ],
        'resize_keyboard' => true
    ];
    if (getStore('inGame') == true) {
        $button = [
            'inline_keyboard' => [
                [
                    ["text" => msg('Report_1'), "callback_data" => "goInGameUserReport_1_$dta"],
                    ["text" => msg('Report_2'), "callback_data" => "goInGameUserReport_2_$dta"],
                ],
                [
                    ["text" => msg('Report_3'), "callback_data" => "goInGameUserReport_3_$dta"],
                    ["text" => msg('Report_4'), "callback_data" => "goInGameUserReport_4_$dta"],
                ],
                [
                    ["text" => msg('Report_5'), "callback_data" => "goInGameUserReport_5_$dta"],
                    ["text" => msg('Report_6'), "callback_data" => "goInGameUserReport_6_$dta"],
                ],
            ],
            'resize_keyboard' => true
        ];
    }
    $Telegram->deleteMessage($chatid, $MessageID);
    $Telegram->sendMessage($chatid, msg('def'), 'html', false, $button);

} elseif (preg_match('/^([Gg]oToReportUser_)(.*)/', $data)) {
    $dta = str_replace(['GoToReportUser_'], '', $data);
    Users::updateCommand($chatid, 'ReportUser');
    Users::updateData($chatid, $dta);
    $button = [
        'inline_keyboard' => [
            [
                ["text" => msg('DecSkip'), "callback_data" => "SendReportUser_" . $dta],
            ],
        ],
        'resize_keyboard' => true
    ];
    $Telegram->editMessageText($chatid, $MessageID, msg('ReportDec'), 'html', '', true, $button);
} elseif (preg_match('/^([Gg]oInGameUserReport_)(.*)/', $data)) {
    $dta = str_replace(['goInGameUserReport_'], '', $data);
    Users::updateCommand($chatid, 'InGameReportUser_' . $dta);

    $button = [
        'inline_keyboard' => [
            [
                ["text" => msg('DecSkip'), "callback_data" => "sendIngameUserRep_" . $dta],
            ],
        ],
        'resize_keyboard' => true
    ];
    $Telegram->editMessageText($chatid, $MessageID, $language['ReportDec'], 'html', '', true, $button);
} elseif (preg_match('/^([Ss]endIngameUserRep_)(.*)/', $data)) {
    $dta = str_replace(['sendIngameUserRep_'], '', $data);
    $ex = explode('_', $dta);
    $RepType = $ex[0];
    $RepUser = $ex[1];
    Users::updateCommand($chatid, $ex[2]);
    $Telegram->editMessageText($chatid, $MessageID, msg('doneReportUser'), 'html', '', true, isset($ex[2]) ? null : $button_start);
    $sender = '/Profile_' . getStore('getUserProfile')->cod;
    $userRep = '/Profile_' . Profile::getUser($RepUser)->cod;
    $userRepMen = functions::generationMentions($RepUser, $RepUser);
    $senderMen = functions::generationMentions($chatid, $chatid);
    $msg = str_replace(['#msg', '#type', '#usercode', '#userid', '#chatcod', '#chatid'], ['none', msg('Report_' . $RepType), $userRep, $userRepMen, $sender, $senderMen], msg('InformationReposrt'));
    $button = [
        'inline_keyboard' => [
            [
                ["text" => msg('WaringForUser'), "callback_data" => "SendWaring_" . $RepUser],
            ],
            [
                ["text" => msg('BlockedUser'), "callback_data" => "BlockedUser_" . $RepUser],
            ],
        ],
        'resize_keyboard' => true
    ];
    if (Groups::hasGroups()) {
        $beforToken = $Telegram->bot_token;
        $Telegram->bot_token = Listbots::getBotDef()->token;
        foreach (Groups::getGroups() as $item) {
            $Telegram->sendMessage('-' . $item->chatid, $msg, 'html', false, $button);
        }
        $Telegram->bot_token = $beforToken;

    } else {
        foreach (Users::getAdmins() as $item) {
            $beforToken = $Telegram->bot_token;
            $Telegram->sendMessage($item->chatid, $msg, 'html', false, $button);

        }
        $Telegram->bot_token = $beforToken;

    }
} elseif (preg_match('/^([Ss]endReportUser_)(.*)/', $data)) {
    $dta = str_replace(['SendReportUser_'], '', $data);
    $ex = explode('_', $dta);
    $RepType = $ex[0];
    $RepUser = $ex[1];
    Users::updateCommand($chatid, isset($ex[3]) ? $ex[3] : null);
    Users::updateData($chatid, isset($ex[2]) ? $ex[2] : null);
    $Telegram->editMessageText($chatid, $MessageID, msg('doneReportUser'), 'html', '', true, isset($ex[2]) ? null : $button_start);
    $sender = '/Profile_' . getStore('getUserProfile')->cod;
    $userRep = '/Profile_' . Profile::getUser($RepUser)->cod;
    $userRepMen = functions::generationMentions($RepUser, $RepUser);
    $senderMen = functions::generationMentions($chatid, $chatid);
    $msg = str_replace(['#msg', '#type', '#usercode', '#userid', '#chatcod', '#chatid'], ['none', msg('Report_' . $RepType), $userRep, $userRepMen, $sender, $senderMen], msg('InformationReposrt'));
    $button = [
        'inline_keyboard' => [
            [
                ["text" => msg('WaringForUser'), "callback_data" => "SendWaring_" . $RepUser],
            ],
            [
                ["text" => msg('BlockedUser'), "callback_data" => "BlockedUser_" . $RepUser],
            ],
        ],
        'resize_keyboard' => true
    ];
    if (Groups::hasGroups()) {
        $beforToken = $Telegram->bot_token;
        $Telegram->bot_token = Listbots::getBotDef()->token;
        foreach (Groups::getGroups() as $item) {
            $Telegram->sendMessage('-' . $item->chatid, $msg, 'html', false, $button);
        }
        $Telegram->bot_token = $beforToken;

    } else {
        foreach (Users::getAdmins() as $item) {
            $beforToken = $Telegram->bot_token;
            $Telegram->sendMessage($item->chatid, $msg, 'html', false, $button);
        }
        $Telegram->bot_token = $beforToken;

    }
} elseif (preg_match('/^([Ii]nGameReportUser_)(.*)/', $command)) {
    $dta = str_replace(['InGameReportUser_'], '', $command);
    if (!$text) {
        $Telegram->sendMessage($chatid, msg('string'), 'html', false);
    } else {
        $ex = explode('_', $dta);
        Users::updateCommand($chatid, $ex[2]);
        $RepType = $ex[0];
        $RepUser = $ex[1];
        $a = $Telegram->sendMessage($chatid, msg('doneReportUser'), 'html', false);
        if ($a->getOk()) {
            $Telegram->deleteMessage($chatid, $a->getResult()->getMessageId() - 2);
        }
        $sender = '/Profile_' . getStore('getUserProfile')->cod;
        $userRep = '/Profile_' . Profile::getUser($RepUser)->cod;
        $userRepMen = functions::generationMentions($RepUser, $RepUser);
        $senderMen = functions::generationMentions($chatid, $chatid);
        $msg = str_replace(['#msg', '#type', '#usercode', '#userid', '#chatcod', '#chatid'], [$text, msg('Report_' . $RepType), $userRep, $userRepMen, $sender, $senderMen], msg('InformationReposrt'));
        $button = [
            'inline_keyboard' => [
                [
                    ["text" => msg('WaringForUser'), "callback_data" => "SendWaring_" . $RepUser],
                ],
                [
                    ["text" => msg('BlockedUser'), "callback_data" => "BlockedUser_" . $RepUser],
                ],
            ],
            'resize_keyboard' => true
        ];
        if (Groups::hasGroups()) {
            $beforToken = $Telegram->bot_token;
            $Telegram->bot_token = Listbots::getBotDef()->token;
            foreach (Groups::getGroups() as $item) {
                $Telegram->sendMessage('-' . $item->chatid, $msg, 'html', false, $button);
            }
            $Telegram->bot_token = $beforToken;

        } else {
            foreach (Users::getAdmins() as $item) {
                $Telegram->sendMessage($item->chatid, $msg, 'html', false, $button);
            }
            $Telegram->bot_token = $beforToken;

        }
    }

} //region Skip Dec
//endregion Skip Dec
elseif ($command == 'ReportUser' and $data != 'back') {
    if (!$text) {
        $Telegram->sendMessage($chatid, msg('string'), 'html', false);
    } else {
        Users::updateCommand($chatid, null);
        Users::updateData($chatid, null);
        $ex = explode('_', $user_data);
        $RepType = $ex[0];
        $RepUser = $ex[1];
        $a = $Telegram->sendMessage($chatid, msg('doneReportUser'), 'html', false);
        $Telegram->deleteMessage($chatid, $a['result']['message_id'] - 2);
        $sender = '/Profile_' . getStore('getUserProfile')->cod;
        $userRep = '/Profile_' . Profile::getUser($RepUser)->cod;
        $userRepMen = functions::generationMentions($RepUser, $RepUser);
        $senderMen = functions::generationMentions($chatid, $chatid);
        $msg = str_replace(['#msg', '#type', '#usercode', '#userid', '#chatcod', '#chatid'], [$text, msg('Report_' . $RepType), $userRep, $userRepMen, $sender, $senderMen], msg('InformationReposrt'));
        $button = [
            'inline_keyboard' => [
                [
                    ["text" => msg('WaringForUser'), "callback_data" => "SendWaring_" . $RepUser],
                ],
                [
                    ["text" => msg('BlockedUser'), "callback_data" => "BlockedUser_" . $RepUser],
                ],
            ],
            'resize_keyboard' => true
        ];
        if (Groups::hasGroups()) {
            $beforToken = $Telegram->bot_token;
            $Telegram->bot_token = Listbots::getBotDef()->token;
            foreach (Groups::getGroups() as $item) {
                $Telegram->sendMessage('-' . $item->chatid, $msg, 'html', false, $button);
            }
            $Telegram->bot_token = $beforToken;

        } else {
            foreach (Users::getAdmins() as $item) {
                $Telegram->sendMessage($item->chatid, $msg, 'html', false, $button);

            }
            $Telegram->bot_token = $beforToken;

        }
    }
}
//endregion Report User

//region Send waring For User
elseif (preg_match('/^([Ss]endWaring_)(.*)/', $data)) {
    $dta = str_replace(['SendWaring_'], '', $data);
    $bot = Users::getUser($dta)->bot;
    $beforToken = $Telegram->bot_token;
    $Telegram->bot_token = Listbots::addBot($bot)->token;
    $msg = str_replace(['#user'], [functions::generationMentions($dta, $dta)], msg('DoneSendWaring'));
    $Telegram->sendMessage($dta, msg('TextWaring'), 'html', false);
    $Telegram->bot_token = $beforToken;
    $Telegram->editMessageText($chatid, $MessageID, $msg, 'html', '', true);

}
//endregion Send waring For User

//region Blocked User
elseif (preg_match('/^([Bb]lockedUser_)(.*)/', $data)) {
    $dta = str_replace(['BlockedUser_'], '', $data);
    Users::updateActive($dta, 0);
    $msg = str_replace(['#user'], [functions::generationMentions($dta, $dta)], msg('DoneBlockedUser'));
    $Telegram->editMessageText($chatid, $MessageID, $msg, 'html', '', true);
} //endregion Blocked User
//region Back
elseif ($data == 'back') {
    $Telegram->deleteMessage($chatid, $MessageID);
    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('def'), 'html', false, $button_start);
} elseif ($data == 'back2') {
    Users::updateCommand($chatid, null);
    Users::updateData($chatid, null);
    $Telegram->deleteMessage($chatid, $MessageID);
    $Telegram->sendMessage($chatid, msg('def'), 'html', false, $button_start);
}
//region Profile Data
//endregion Back