<?php

use Battles\{Database\Db, Hostel, Travel, User, UserEffect};

require_once 'config.php';

$host = new Hostel();

if (!empty($_GET['exit'])) {
    Travel::toRoom(26, 661);
}

if (!empty($_GET['to_room'])) {
    if (empty($host->getHid())) {
        $host->setError('У Вас, нету комнаты!');
    }
    if ($host->getTime() <= time()) {
        $host->setError('У Вас просрочена аренда. Оплатите что-бы продолжить пользоваться нашими услугами!');
    }
    if ($host->getStatus()['type'] !== 'error') {
        Travel::toRoom(661, 26);
    }
}

if (!empty($_GET['pays'])) {
    $host->changeTime($_GET['pays']);
}

if (!empty($_POST['select']) && !empty($_POST['tariff'])) {
    $host->newRent((int)$_POST['tariff']);
}

if (!empty($_GET['del'])) {
    $host->remove();
}

if (!empty($_POST['deselect']) && !empty($_POST['retariff'])) {
    $host->changeType((int)$_POST['retariff']);
    header('Location: main.php');
}

if (!empty($_GET['sleep'])) {
    if (UserEffect::getRemainingEffectTime(User::getInstance()->getId(), 8) <= time()) {
        //Разморозка таймеров эффектов??!
        UserEffect::add(User::getInstance()->getId(), 8, UserEffect::$effectName[8], time() + 60 * 60 * 2);
        Db::getInstance()->execute('update users_effects set remaining_time = remaining_time - ? where owner_id = ? and type not in (11,12,13,14,5,4,3,2,8)', [time(), User::getInstance()->getId()]);
        header('Location: hostel.php');
    } else {
        $host->setError('Нельзя спать! Приходите через: ' . timeOut($user['sleep_time'] - time()));
    }
}

if (!empty($_GET['unsleep'])) {
    //Заморозка таймеров эффектов??!
    UserEffect::remove(User::getInstance()->getId(), 8);
    Db::getInstance()->execute('update users_effects set remaining_time = remaining_time + ? where owner_id = ? and type not in (11,12,13,14,5,4,3,2,8)', [time(), User::getInstance()->getId()]);
    header('Location: hostel.php');
}

require_once 'views/hostel.php';