<?php

use inc\fileStorage;
use Database\{Users, Settings, Shop};

if ($text == patternAdmin('shop')) {
    $Telegram->sendMessage($chatid, msg('def'), 'html', false, $button_shop);
} elseif ($text == patternAdmin('addItemToShop')) {
    Users::updateCommand($chatid, 'addItemToShop');
    $Telegram->sendMessage($chatid, msg('snedPrice'), 'html', false, $button_backhome);
} elseif ($command == 'addItemToShop' && $text != patternAdmin('back')) {
    Users::updateCommand($chatid, 'addItemToShop_step2');
    Users::updateData($chatid, $text);
    $Telegram->sendMessage($chatid, msg('snedCoin'), 'html', false, $button_backhome);
} elseif ($command == 'addItemToShop_step2' && $text != patternAdmin('back')) {
    if (Shop::hasItem($text, $user_data)) {
        $Telegram->sendMessage($chatid, msg('alerGame'), 'html', false, $button_backhome);
    } else {
        Users::updateCommand($chatid, null);
        Users::updateData($chatid, null);
        Shop::addItem($text, $user_data);
        fileStorage::removeFile('bot');
        $Telegram->sendMessage($chatid, msg('done'), 'html', false, $button_shop);
    }
} elseif ($text == patternAdmin('listItemInShop')) {
    $list = Shop::getItems();
    if ($list == []) {
        $Telegram->sendMessage($chatid, msg('notFoundItem'), 'html', false, $button_shop);
        die();
    }
    $btn = [];
    $btn[] = [["text" => msg('item'), "callback_data" => "none"], ["text" => msg('price'), "callback_data" => "none"], ["text" => msg('hot'), "callback_data" => "none"]];
    foreach ($list as $bot) {
        $btn[] = [["text" => $bot->coin, "callback_data" => "setHotShop_" . $bot->id], ["text" => $bot->price, "callback_data" => "setHotShop_" . $bot->id], ["text" => $bot->hot == 1 ? '*' : ' ', "callback_data" => "setHotShop_" . $bot->id]];
    }

    $Telegram->sendMessage($chatid, msg('listItem'), 'html', false, [
        'inline_keyboard' =>
            $btn
        ,
        'resize_keyboard' => true
    ]);
} elseif ($text == patternAdmin('rmoveItemToShop')) {
    $list = Shop::getItems();
    if ($list == []) {
        $Telegram->sendMessage($chatid, msg('notFoundItem'), 'html', false, $button_shop);
        die();
    }
    $btn = [];
    $btn[] = [["text" => msg('item'), "callback_data" => "none"], ["text" => msg('price'), "callback_data" => "none"]];
    foreach ($list as $bot) {
        $btn[] = [["text" => $bot->coin, "callback_data" => "removeShop_" . $bot->id], ["text" => $bot->price, "callback_data" => "removeShop_" . $bot->id]];
    }


    $Telegram->sendMessage($chatid, msg('selectItem'), 'html', false, [
        'inline_keyboard' =>
            $btn
        ,
        'resize_keyboard' => true
    ]);
} elseif (preg_match('/^([Rr]emoveShop_)(.*)/', $data)) {
    $dta = str_replace(['removeShop_'], '', $data);
    $Telegram->EditMessageText($chatid, $MessageID, msg('realy'), 'html', '', true, [
        'inline_keyboard' =>
            [[["text" => msg('yes'), "callback_data" => "yesremoveShop_" . $dta], ["text" => msg('no'), "callback_data" => "noremoveShop_" . $dta]]]
        ,
        'resize_keyboard' => true
    ]);
} elseif (preg_match('/^([Yy]esremoveShop_)(.*)/', $data)) {
    $dta = str_replace(['yesremoveShop_'], '', $data);
    Shop::removeItem($dta);
    fileStorage::removeFile('bot');
    $Telegram->EditMessageText($chatid, $MessageID, msg('done'), 'html', '', true);
} elseif (preg_match('/^([Nn]oremoveShop_)(.*)/', $data)) {
    $dta = str_replace(['noremoveShop_'], '', $data);
    $Telegram->deleteMessage($chatid, $MessageID);
} elseif (preg_match('/^([Ss]etHotShop_)(.*)/', $data)) {
    $dta = str_replace(['setHotShop_'], '', $data);
    Shop::updateHot($dta, Shop::getItemsByiId($dta)->hot == true ? false : true);
    fileStorage::removeFile('bot');

    $list = Shop::getItems();
    $btn = [];
    $btn[] = [["text" => msg('item'), "callback_data" => "none"], ["text" => msg('price'), "callback_data" => "none"], ["text" => msg('hot'), "callback_data" => "none"]];
    foreach ($list as $bot) {
        $btn[] = [["text" => $bot->coin, "callback_data" => "setHotShop_" . $bot->id], ["text" => $bot->price, "callback_data" => "setHotShop_" . $bot->id], ["text" => $bot->hot == 1 ? '*' : ' ', "callback_data" => "setHotShop_" . $bot->id]];
    }
    $Telegram->editMessageReplyMarkup($chatid, $MessageID, msg('listItem'), [
        'inline_keyboard' =>
            $btn,
        'resize_keyboard' => true
    ]);
}
