from core.command_param_types import Int, Character, NamedParameters
from core.decorators import instance, command
from core.lookup.character_history_service import CharacterHistoryService
from core.text import Text
from core.igncore import IgnCore


@instance()
class CharacterHistoryController:
    def __init__(self):
        self.PAGE_SIZE = 25
        pass

    def inject(self, registry):
        self.bot: IgnCore = registry.get_instance("bot")
        self.text: Text = registry.get_instance("text")
        self.util = registry.get_instance("util")
        self.character_history_service: CharacterHistoryService = registry.get_instance("character_history_service")
        self.command_alias_service = registry.get_instance("command_alias_service")

    def start(self):
        self.command_alias_service.add_alias("h", "history")

    @command(command="history",
             params=[Character("character"), Int("server_num", is_optional=True), NamedParameters(['page'])],
             access_level="member",
             description="Get history of character",
             extended_description="Use server_num 6 for RK2019 and server_num 5 for live")
    def handle_history_cmd1(self, _, char, server_num, named_params):
        server_num = server_num or self.bot.dimension
        page = int(named_params.page or "1")
        offset = (page - 1) * self.PAGE_SIZE
        data = self.character_history_service.get_character_history(char.name, server_num)
        if data:
            data = [x for x in data]
        else:
            return f"Could not find history for <highlight>{char.name}</highlight> " \
                   f"on server <highlight>{server_num}</highlight>."
        blob = " | ".join(["Date", "Lvl", "AI", "Side", "Breed", "CharId", "Name", "Guild (Rank)"]) + "\n"
        blob += "__________________________________________________________\n"
        return self.text.format_pagination(data, offset, page, self.formatter,
                                           f'History of {char.name} (RK{server_num})',
                                           f"Could not find history for <highlight>{char.name}</highlight> "
                                           f"on server <highlight>{server_num:d}</highlight>.",
                                           f'history {char.name} {server_num} ', self.PAGE_SIZE, blob)

    def formatter(self, row, _, data):
        if row.guild_name:
            org = f"{row.guild_name} ({row.guild_rank_name})"
        else:
            org = ""
        last_changed = self.util.format_date(int(float(row.last_changed)))
        if row.deleted == "1":  # This value is output as string
            return "<red>DELETED</red>\n"
        return f"{last_changed} | <white>{self.text.zfill(int(row.level), 220)}</white> | " \
               f"<green>{self.text.zfill(int(row.defender_rank or 0), 30)}</green> | {row.faction:^7} | " \
               f"{row.breed:^10} | {self.text.zfill(int(row.char_id or 0), int(data[0].char_id or 0))} | {row.nickname:^13} | " \
               f"<white>{org}</white>\n"
