<?php

use Database\Botads;
use Database\Users;

if ($text == patternAdmin('ads')) {
    $Telegram->SendMessage($chatid, msg('def'), 'html', false, $button_ads);
} elseif ($text == patternAdmin('addAds')) {
    Users::updateCommand($chatid, 'addAds');
    $Telegram->SendMessage($chatid, msg('sendAds'), 'html', false, $button_backhome);
} elseif ($command == 'addAds' && $text != patternAdmin('back')) {
    $msgCopy = $Telegram->copyMessageNonCaption(CHANNELFORWARD, $chatid, $MessageID);
    $Telegram->sendMessage(CHANNELFORWARD, 'bot #ads');
    $lastId = Botads::addAds(CHANNELFORWARD, $msgCopy->getResult()->getMessageId(), 0);
    Users::updateData($chatid, 'ads_' . $lastId);
    Users::updateCommand($chatid, 'addMaxView');
    $Telegram->SendMessage($chatid, msg('sendView'), 'html', false, $button_backhome);
} elseif ($command == 'addMaxView' && $text != patternAdmin('back')) {
    $ex = explode('_', $user_data)[1];
    if ($text) {
        Botads::updateViewMax($ex, $text);
        Users::updateCommand($chatid, 'adsBtnTitle');
        $Telegram->SendMessage($chatid, msg('sendKeyboardTitle'), 'html', false, $button_backhome);
    }
} elseif ($command == 'adsBtnTitle' && $text != patternAdmin('back')) {
    $ex = explode('_', $user_data)[1];
    if ($text == '/skip') {
        Users::updateData($chatid, null);
        Users::updateCommand($chatid, null);
        Botads::updateStatus($ex, true);
        \inc\fileStorage::removeFile('bot');
        $Telegram->SendMessage($chatid, str_replace('#id', $ex, msg('doneAds')), 'html', false, $button_ads);
        die();
    }
    Botads::updateBtnTitle($ex, $text);
    Users::updateCommand($chatid, 'adsBtnUrl');
    $Telegram->SendMessage($chatid, msg('sendKeyboardUrl'), 'html', false, $button_backhome);
} elseif ($command == 'adsBtnUrl' && $text != patternAdmin('back')) {
    $ex = explode('_', $user_data)[1];
    Users::updateCommand($chatid, null);
    Users::updateData($chatid, null);
    Botads::updateBtnUrl($ex, $text);
    Botads::updateStatus($ex, true);
    \inc\fileStorage::removeFile('bot');
    $Telegram->SendMessage($chatid, str_replace('#id', $ex, msg('doneAds')), 'html', false, $button_ads);
} elseif ($text == patternAdmin('listAds')) {
    if (Botads::hasAdss() == false) {
        $Telegram->SendMessage($chatid, msg('notFoundAds'), 'html', false);
        die();
    }
    Users::updateCommand($chatid, 'SelectAds');
    $btn = [];
    $btn[] = [['text' => patternAdmin('back')]];
    foreach (Botads::getAllAds() as $item) {
        $btn[] = [['text' => $item->id]];
    }
    $Telegram->SendMessage($chatid, msg('def'), 'html', false, [
        'keyboard' => $btn,
        'resize_keyboard' => true
    ]);
} elseif ($command == 'SelectAds' && $text != patternAdmin('back')) {
    $get = Botads::getAds($text);
    if (!empty($get)) {
        Users::updateCommand($chatid, null);
        $count = $get->view;
        $max_view = $get->max_view;
        $btn_title = $get->btn_title;
        $btn_url = $get->btn_url;
        $btn = null;
        if ($btn_title != null) {
            $btn = ['inline_keyboard' => [[["text" => $btn_title, "url" => $btn_url]]], 'resize_keyboard' => true];
        }

        $Telegram->copyMessageNonCaption($chatid, $get->chatid, $get->msgid, 'html', $btn);

        $Telegram->SendMessage($chatid, str_replace(['#view', '#max', '#command'], [
            $count,
            $max_view,
            '/canselads_' . $text
        ], msg('infoAds')), 'html', false, $button_ads);

    }
} elseif (preg_match('/^\/([Cc]anselads_)(.*)/', $text)) {
    $ex = str_replace('/canselads_', '', $text);
    Botads::RemoveAds($ex);
    \inc\fileStorage::removeFile('bot');
    $Telegram->SendMessage($chatid, msg('done'), 'html', false);
}
