import hjson

from core.aochat.BaseModule import BaseModule
from core.chat_blob import ChatBlob
from core.command_param_types import Any, Const, Options, Time, Character
from core.db import DB
from core.decorators import instance, command
from core.lookup.character_service import CharacterService
from core.translation_service import TranslationService
from core.igncore import IgnCore
from modules.core.ban.ban_service import BanService


@instance()
class BanController(BaseModule):
    # noinspection DuplicatedCode
    def inject(self, registry):
        self.bot: IgnCore = registry.get_instance("bot")
        self.db: DB = registry.get_instance("db")
        self.text = registry.get_instance("text")
        self.util = registry.get_instance("util")
        self.ban_service: BanService = registry.get_instance("ban_service")
        self.command_alias_service = registry.get_instance("command_alias_service")
        self.ts: TranslationService = registry.get_instance("translation_service")
        self.character_service: CharacterService = registry.get_instance("character_service")
        self.getresp = self.ts.get_response

    def start(self):
        self.command_alias_service.add_alias("unban", "ban rem")
        self.ts.register_translation("module/ban", self.load_ban_msg)

    def load_ban_msg(self):
        with open("modules/core/ban/ban.msg", mode="r", encoding="UTF-8") as f:
            return hjson.load(f)

    @command(command="ban", params=[Const("list", is_optional=True)], access_level="moderator",
             description="Show the ban list")
    def ban_list_cmd(self, _, _1):
        query = self.db.query("SELECT a.*, p.*, b.reason, b.finished_at, b.created_at, b.sender_char_id from account a "
                              "left join player p on a.char_id = p.char_id "
                              "left join ban_list b on a.char_id = b.char_id "
                              "where main=a.char_id "
                              "and disabled=1 "
                              "and p.org_id NOT IN (SELECT org_id from ban_org_list)")
        blob = ""
        for row in query:
            ends = "never" if (row.finished_at or -1) == -1 else self.util.format_datetime(row.finished_at)
            blob += f"<highlight>{row.name}</highlight> by " \
                    f"{self.character_service.resolve_char_to_name(row.sender_char_id or self.bot.get_char_id())}\n"
            if row.sender_char_id != 0 and row.sender_char_id is not None:
                blob += f"<tab>Added at: <highlight>{self.util.format_datetime(row.created_at)}</highlight> " \
                        f"Ends: <highlight>{ends}</highlight>\n"
                blob += f"<tab>Reason: {row.reason or 'None given'}\n"
            blob += "\n"
        return ChatBlob("Banned Accounts", blob)

    @command(command="ban", params=[Options(["rem", "remove"]), Character("character")], access_level="moderator",
             description="Remove a character from the ban list")
    def ban_remove_cmd(self, _, _1, char):
        if not char.char_id:
            return self.getresp("global", "char_not_found", {"char": char.name})
        elif not self.ban_service.get_ban(char.char_id):
            return self.getresp("module/ban", "not_banned", {"char": char.name})
        else:
            self.ban_service.remove_ban(char.char_id)
            return self.getresp("module/ban", "unbanned_self", {"char": char.name})

    @command(command="ban",
             params=[Const("add", is_optional=True), Character("character"), Time("duration", is_optional=True),
                     Any("reason", is_optional=True)], access_level="moderator",
             description="Add a character to the ban list")
    def ban_add_cmd(self, request, _, char, duration, reason):
        if not char.char_id:
            return self.getresp("global", "char_not_found", {"char": char.name})
        elif self.ban_service.get_ban(char.char_id):
            return f"<highlight>{char.name}</highlight> is already banned."
        else:
            self.ban_service.add_ban(char.char_id, request.sender.char_id, duration, reason)
            request.reply(f"<highlight>{char.name}</highlight> has been added to the ban list.")
