from core.chat_blob import ChatBlob
from core.command_param_types import Any
from core.decorators import instance, command
from core.text import Text


@instance()
class LeProcsController:
    def inject(self, registry):
        self.db = registry.get_instance("db")
        self.text: Text = registry.get_instance("text")
        self.util = registry.get_instance("util")
        self.command_alias_service = registry.get_instance("command_alias_service")

    def pre_start(self):
        self.db.load_sql_file(self.module_dir + "/sql/leprocs.sql", pre_optimized=True)
        self.db.create_view("leprocs")
        self.command_alias_service.add_alias("leproc", "leprocs")

    @command(command="leprocs", params=[], access_level="member",
             description="Show a list of professions with LE procs")
    def leprocs_list_command(self, request):
        data = self.db.query("SELECT DISTINCT profession FROM leprocs ORDER BY profession")

        blob = ""
        for row in data:
            blob += f"<pagebreak>{self.text.make_tellcmd(row.profession, f'leprocs {row.profession}')}\n"

        blob += "\nProc info provided by Wolfbiter (RK1), Gatester (RK2), DrUrban"

        return ChatBlob("LE Procs", blob)

    @command(command="leprocs", params=[Any("profession")], access_level="member",
             description="Show LE proc information for a specific profession")
    def leprocs_show_command(self, request, prof_name):
        profession = self.util.get_profession(prof_name)

        if not profession:
            return f"Could not find profession <highlight>{prof_name}</highlight>."

        data = self.db.query("SELECT * FROM leprocs WHERE profession LIKE ? "
                             "ORDER BY proc_type, research_lvl DESC", [profession])
        proc_type = ""
        blob = ""
        for row in data:
            if proc_type != row.proc_type:
                proc_type = row.proc_type
                blob += f"\n<highlight>{proc_type}</highlight>\n"

            blob += f"<pagebreak>[{self.text.zfill(row.research_lvl, 10)}] " \
                    f"{row.name} <orange>{row.modifiers}</orange> " \
                    f"{row.duration} <green>{row.proc_trigger}</green>\n"

        blob += "\n\nNote: Offensive procs have a 5% chance of firing every time you attack; " \
                "Defensive procs have a 10% chance of firing every time something attacks you."
        blob += "\n\nProc info provided by Wolfbiter (RK1), Gatester (RK2)"

        return ChatBlob(f"{profession} LE Procs", blob)
