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


@instance()
class PerksController:
    def __init__(self):
        self.grades = ["shiny", "bright", "faded"]

    def inject(self, registry):
        self.db = registry.get_instance("db")
        self.util = registry.get_instance("util")

    def pre_start(self):
        self.db.load_sql_file(self.module_dir + "/" + "perks.sql", pre_optimized=True)
        self.db.create_view("perk")
        self.db.create_view("perk_prof")
        self.db.create_view("perk_level")
        self.db.create_view("perk_level_buffs")

    @command(command="perks", params=[Int("level"), Any("profession")], access_level="member",
             description="Show what perks are available for specified level and profession")
    def perks_cmd(self, _, level, profession):
        if level < 1 or level > 220:
            return "Level must be between <highlight>1</highlight> and <highlight>220</highlight>."

        prof = self.util.get_profession(profession)
        if not prof:
            return f"Could not find profession <highlight>{profession}</highlight>"

        sql = """
            SELECT
                p.name AS perk_name,
                MAX(pl.number) AS max_perk_level,
                SUM(plb.amount) AS buff_amount,
                plb.skill
            FROM
                perk p
                JOIN perk_prof pp ON p.id = pp.perk_id
                JOIN perk_level pl ON p.id = pl.perk_id
                JOIN perk_level_buffs plb ON pl.id = plb.perk_level_id
            WHERE
                pp.profession = ?
                AND pl.min_level <= ?
            GROUP BY
                p.name,
                plb.skill
            ORDER BY
                p.name"""

        data = self.db.query(sql, [prof, level])

        blob = ""
        current_perk = ""
        for row in data:
            if row.perk_name != current_perk:
                blob += f"\n<header2>{row.perk_name} {row.max_perk_level}</header2>\n"
                current_perk = row.perk_name
            blob += f"{row.skill} <highlight>{row.buff_amount}</highlight>\n"

        return ChatBlob(f"Buff Perks for {level} {prof}", blob)
