<?php

namespace Battles;

use Battles\Database\DBPDO;

class User
{
    protected $id = 0;
    protected $login = '<em>Некто</em>';
    protected $pass;
    protected $email = '<em>неизвестно</em>';
    protected $realname;
    protected $borndate;
    protected $info;
    protected $level;
    protected $align;
    protected $clan;
    protected $money;
    protected $ip = 0;

    protected $admin = 0;
    protected $enter_game;
    protected $room;
    protected $block;
    protected $shadow;

    // Пока несуществующие, для совместимости.
    protected $experience = 200;
    protected $battle = 0;
    protected $in_tower = 0; // Скорее башню похороним чем запустим...
    protected $zayavka = 0;
    protected static $db;

    public const INFO_CHAR_LIMIT = 1500;

    public function __construct($user)
    {
        self::$db = DBPDO::INIT();
        $user_query = self::$db->fetch('SELECT * FROM users WHERE id = ? OR login = ?', [$user, $user]);
        foreach ($this as $key => $value) {
            if (isset($user_query[$key])) {
                $this->$key = $user_query[$key];
            }
        }
    }


    protected function showStarSign(): ?string
    {
        /*
         * 1 aries
         * 2 taurus
         * 3 gemini
         * 4 cancer
         * 5 leo
         * 6 virgo
         * 7 libra
         * 8 scorpio
         * 9 sagittarios
         * 10 capricorn
         * 11 aquarius
         * 12 pisches
        */
        $zodiac = [
            356 => "10",
            326 => "09",
            296 => "08",
            266 => "07",
            235 => "06",
            203 => "05",
            172 => "04",
            140 => "03",
            111 => "02",
            78 => "01",
            51 => "12",
            20 => "11",
            0 => "10",
        ];
        $dayOfYear = date("z", strtotime($this->borndate));
        $isLeapYear = date("L", strtotime($this->borndate)); //Высокосный?
        if ($isLeapYear && $dayOfYear > 59) {
            --$dayOfYear;
        }
        foreach ($zodiac as $day => $sign) {
            if ($dayOfYear > $day) {
                break;
            }
        }
        return $sign ?? null;
    }


    /**
     * @param int         $userId
     * @param int         $type
     * @param string      $name
     * @param int         $time
     * @param string|null $json_modifiers_list (str, dex, int, end, intel, wis).
     * @return bool
     */
    public static function setUserEffect(int $userId, int $type, string $name, int $time, string $json_modifiers_list = null): bool
    {
        $mods = json_decode($json_modifiers_list);
        return self::$db->execute('INSERT INTO users_effects (owner_id, type, name, remaining_time, mod_strength, mod_dexterity, mod_intuition, mod_endurance, mod_intellect, mod_wisdom) VALUES (?,?,?,?,?,?,?,?,?,?)', [$userId, $type, $name, $time, $mods->str ?? null, $mods->dex ?? null, $mods->int ?? null, $mods->end ?? null, $mods->intel ?? null, $mods->wis ?? null]);
    }

    public static function removeUserEffect(int $userId, int $type): bool
    {
        if (self::$db->fetch('SELECT 1 FROM users_effects WHERE owner_id = ? AND type = ?', [$userId, $type])) {
            self::$db->execute('DELETE FROM users_effects WHERE owner_id = ? AND type = ?', [$userId, $type]);
        }
        return false;
    }

    /**
     * @return int
     */
    public function getId(): int
    {
        return $this->id;
    }

    /**
     * @return string
     */
    public function getLogin(): string
    {
        return $this->login;
    }

    /**
     * @param string $login
     */
    public function setLogin(string $login): void
    {
        $this->login = $login;
    }

    /**
     * @return mixed
     */
    public function getPass()
    {
        return $this->pass;
    }

    /**
     * @param mixed $pass
     */
    public function setPass($pass): void
    {
        $this->pass = $pass;
    }

    public function savePass()
    {
        self::$db->execute('UPDATE users SET pass = ? WHERE id = ?', [$this->pass, $this->id]);
    }

    /**
     * @return string
     */
    public function getEmail(): string
    {
        return $this->email;
    }

    /**
     * @param string $email
     */
    public function setEmail(string $email): void
    {
        $this->email = $email;
    }

    /**
     * @return mixed
     */
    public function getRealname()
    {
        return $this->realname;
    }

    /**
     * @param mixed $realname
     */
    public function setRealname($realname): void
    {
        $this->realname = $realname;
    }

    /**
     * @return mixed
     */
    public function getBorndate()
    {
        return $this->borndate;
    }

    /**
     * @param mixed $borndate
     */
    public function setBorndate($borndate): void
    {
        $this->borndate = $borndate;
    }

    /**
     * @return mixed
     */
    public function getInfo()
    {
        return $this->info;
    }

    /**
     * @param mixed $info
     */
    public function setInfo($info): void
    {
        $this->info = $info;
    }

    /**
     * @return int
     */
    public function getLevel(): int
    {
        return $this->level;
    }

    /**
     * @param int $level
     */
    public function setLevel(int $level): void
    {
        $this->level = $level;
    }

    /**
     * @return int
     */
    public function getAlign(): int
    {
        return $this->align;
    }

    /**
     * @param int $align
     */
    public function setAlign(int $align): void
    {
        $this->align = $align;
    }

    /**
     * @return string
     */
    public function getClan(): string
    {
        return $this->clan;
    }

    /**
     * @param int $clan
     */
    public function setClan(string $clan): void
    {
        $this->clan = $clan;
    }

    /**
     * @return int
     */
    public function getMoney(): int
    {
        return $this->money;
    }

    /**
     * @param int $money
     */
    public function setMoney(int $money): void
    {
        $this->money = $money;
    }


    /**
     * @return mixed
     */
    public function getIp()
    {
        return $this->ip;
    }

    /**
     * @param mixed $ip
     */
    public function setIp($ip): void
    {
        $this->ip = $ip;
    }


    /**
     * @return int
     */
    public function getAdmin(): int
    {
        return $this->admin;
    }

    /**
     * @return mixed
     */
    public function getEnterGame()
    {
        return $this->enter_game;
    }

    /**
     * @param mixed $enter_game
     */
    public function setEnterGame($enter_game): void
    {
        $this->enter_game = $enter_game;
    }

    /**
     * @return mixed
     */
    public function getRoom()
    {
        return $this->room;
    }

    /**
     * @param mixed $room
     */
    public function setRoom($room): void
    {
        $this->room = $room;
    }

    /**
     * @return mixed
     */
    public function getBlock()
    {
        return $this->block;
    }

    /**
     * @param mixed $block
     */
    public function setBlock($block): void
    {
        $this->block = $block;
    }

    /**
     * @return mixed
     */
    public function getShadow()
    {
        return $this->shadow;
    }

    /**
     * @param mixed $shadow
     */
    public function setShadow($shadow): void
    {
        $shadows = [
            'm01', 'm02', 'm03', 'm04', 'm05', 'm06', 'm07', 'm08', 'm09', 'm10',
            'f01', 'f02', 'f03', 'f04', 'f05', 'f06', 'f07', 'f08', 'f09', 'f10',
        ];
        if (in_array($shadow, $shadows) && $this->getShadow() == '0.png') {
            $this->shadow = $shadow . '.png';
        }
    }

    public function saveShadow()
    {
        self::$db->execute('UPDATE users SET shadow = ? WHERE id = ?', [$this->getShadow(), $this->getId()]);
    }

    /**
     * @return int
     */
    public function getExperience(): int
    {
        return $this->experience;
    }

    /**
     * @param int $experience
     */
    public function setExperience(int $experience): void
    {
        $this->experience = $experience;
    }

    /**
     * @return int
     */
    public function getBattle(): int
    {
        return $this->battle;
    }

    /**
     * @param int $battle
     */
    public function setBattle(int $battle): void
    {
        $this->battle = $battle;
    }

    /**
     * @return int
     */
    public function getInTower(): int
    {
        return $this->in_tower;
    }

    /**
     * @param int $in_tower
     */
    public function setInTower(int $in_tower): void
    {
        $this->in_tower = $in_tower;
    }

    /**
     * @return int
     */
    public function getZayavka(): int
    {
        return $this->zayavka;
    }

    /**
     * @param int $zayavka
     */
    public function setZayavka(int $zayavka): void
    {
        $this->zayavka = $zayavka;
    }

    public function saveAnketa()
    {
        self::$db->execute('UPDATE users SET realname = ?, info = ? WHERE id = ?', [$this->realname, $this->info, $this->id]);
    }

    public function setOnline()
    {
        self::$db->execute('update online set real_time = ? where user_id = ?', [time(), $this->getId()]);
    }
}