<?php

use Database\{Users, Profile,Listbots};

if ($text == patternAdmin('sendForUser')) {
    $Telegram->SendMessage($chatid, msg('def'), 'html', false, $button_sendmsgForUser);
} elseif ($text == patternAdmin('sendForUserByCode')) {
    Users::updateCommand($chatid, 'sendMsgForUserByCode_step1');
    $Telegram->SendMessage($chatid, msg('sendUserCode'), 'html', false, $button_backhome);
} elseif ($text == patternAdmin('sendForUserByUserid')) {
    Users::updateCommand($chatid, 'sendMsgForUser_step1');
    $Telegram->SendMessage($chatid, msg('sendUserId'), 'html', false, $button_backhome);
} elseif ($command == 'sendMsgForUserByCode_step1' && $text != patternAdmin('back')) {
    $txt = str_replace(['/Profile_', '/profile_', '/'], '', $text);
    if (Profile::checkUserByCode($txt) == false) {
        $Telegram->SendMessage($chatid, msg('NotfoundUser'), 'html', false, $button_backhome);
        die();
    }
    Users::updateData($chatid, Profile::getUserByCode($txt)->chatid);
    Users::updateCommand($chatid, 'sendMsgForUserByCode_step1');

    $Telegram->SendMessage($chatid, msg('sendTextForUser'), 'html', false, $button_backhome);
} elseif ($command == 'sendMsgForUser_step1' && $text != patternAdmin('back')) {
    if (Profile::checkUser($text) == false) {
        $Telegram->SendMessage($chatid, msg('NotfoundUser'), 'html', false, $button_backhome);
        die();
    }

    Users::updateData($chatid, $text);
    Users::updateCommand($chatid, 'sendMsgForUser_step2');
    $Telegram->SendMessage($chatid, msg('sendTextForUser'), 'html', false, $button_backhome);
} elseif ($command == 'sendMsgForUser_step2' && $text != patternAdmin('back')) {
    $userid = $user_data;
    if ($Telegram->isMedia()) {
        $msgCopy = $Telegram->copyMessageNonCaption(CHANNELFORWARD, $chatid, $MessageID);
        $Telegram->sendMessage(CHANNELFORWARD, 'send for user ' . $chatid);
    }

    $bot = getStore('getUser')->bot;
    $beforToken = $Telegram->bot_token;
    $Telegram->bot_token = Listbots::getBot($bot)->token;
    if ($Telegram->isMedia()) {
        if (!$msgCopy->getOk()) {
            $Telegram->sendMessage($chatid, msg('un_error2'), 'html', false);
            die();
        }
        $Telegram->copyMessageNonCaption($userid, CHANNELFORWARD, $msgCopy->getResult()->getMessageId());
    } else {
        $Telegram->sendMessage($userid, str_replace(['<', '>'], '', $text), 'html');
    }
    $Telegram->bot_token = $beforToken;
    Users::updateData($chatid, null);
    Users::updateCommand($chatid, null);
    $Telegram->SendMessage($chatid, msg('done'), 'html', false, $button_admin);

}
