<?php

namespace Moderation;

use Core\Db;
use DateTime;
use Delo;

// todo Заменить уродский костыль с тюремным сундуком. Возможен конфликт с автоудалением предметов!
// todo Понять как мониторить переводы.

class Moderation
{
    private const JAIL_ROOM = 274;
    private const CENTRAL_SQUARE_ROOM = 9;
    private const JAIL_STORAGE = 1357908642; /* Ух, костыль! */
    private const NOT_SET = 'Не указано.';
    public const EXPIRATION_DATETIME_FORMAT = 'd M Y H:i';
    private int $target;

    public function __construct(int $userid)
    {
        $this->target = $userid;
    }

    /**
     * Молчание
     * @param DateTime $expiration срок истечения.
     * @param string $reason причина применения.
     */
    public function silence(DateTime $expiration, string $reason = self::NOT_SET): void
    {
        Db::sql('update users set molch1 = ? where id = ?', [$expiration->getTimestamp(), $this->target]);
        Delo::add(
            10,
            'moderation.silence',
            $this->target,
            'Молчанка в чате до ' . $expiration->format(self::EXPIRATION_DATETIME_FORMAT) . '. ' . $reason,
        );
    }

    /**
     * Снятие молчания
     */
    public function unsilence(): void
    {
        if (Db::getValue('select count(molch1) from users where id = ? and molch1 != 0', [$this->target]) === 0) {
            return;
        }
        Db::sql('update users set molch1 = default where id = ?', [$this->target]);
        Delo::add(
            10,
            'moderation.unsilence',
            $this->target,
            'Отмена молчанки.',
        );
    }

    /**
     * Обезличивание
     * @param DateTime $expiration срок истечения.
     * @param string $reason причина применения.
     */
    public function depersonalize(DateTime $expiration, string $reason = self::NOT_SET): void
    {
        Db::sql('update users set info_delete = ? where id = ?', [$expiration->getTimestamp(), $this->target]);
        Delo::add(
            10,
            'moderation.depersonalize',
            $this->target,
            'Скрытие анкеты до ' . $expiration->format(self::EXPIRATION_DATETIME_FORMAT) . '. ' . $reason,
        );
    }

    /**
     * Снятие обезличивания
     */
    public function undepersonalize(): void
    {
        if (Db::getValue('select count(info_delete) from users where id = ? and info_delete <= unix_timestamp()', [$this->target]) === 0) {
            return;
        }
        Db::sql('update users set info_delete = default where id = ?', [$this->target]);
        Delo::add(
            10,
            'moderation.undepersonalize',
            $this->target,
            'Раскрытие анкеты.',
        );
    }

    /**
     * Тюрьма
     * @param DateTime $expiration срок истечения.
     * @param string $reason причина применения.
     * @todo Корректно выбрасывать игрока из подземелья.
     */
    public function prison(DateTime $expiration, string $reason = self::NOT_SET): void
    {
        Db::sql('update users set jail = ?, room = ? where id = ?', [$expiration->getTimestamp(), self::JAIL_ROOM, $this->target,]);
        Db::sql('update items_users set is_arrested = 1 where uid = ?', [$this->target,]);
        Delo::add(
            10,
            'moderation.prison',
            $this->target,
            'Тюрьма до ' . $expiration->format(self::EXPIRATION_DATETIME_FORMAT) . '. ' . $reason,
        );
    }

    /**
     * Снятие тюрьмы
     */
    public function unprison(): void
    {
        Db::sql('update users set jail = default, room = ? where id = ?', [self::CENTRAL_SQUARE_ROOM, $this->target,]);
        Db::sql('update items_users set is_arrested = default where uid = ?', [$this->target,]);
        Delo::add(
            10,
            'moderation.unprison',
            $this->target,
            'Выпуск из тюрьмы.',
        );
    }

    /**
     * Блокировка
     * @param string $reason причина применения.
     */
    public function ban(string $reason = self::NOT_SET): void
    {
        Db::sql('update users set banned = unix_timestamp() where id = ?', [$this->target]);
        Delo::add(
            10,
            'moderation.ban',
            $this->target,
            'Блокировка. ' . $reason,
        );
    }

    /**
     * Снятие блокировки
     */
    public function unban(): void
    {
        Db::sql('update users set banned = default where id = ?', [$this->target]);
        Delo::add(
            10,
            'moderation.unban',
            $this->target,
            'Снятие блокировки.',
        );
    }
}