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


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

    def pre_start(self):
        self.db.load_sql_file(self.module_dir + "/sql/ofab_armor.sql", pre_optimized=True)
        self.db.create_view("ofab_armor")
        self.db.create_view("ofab_armor_cost")
        self.db.create_view("ofab_armor_type")

    @command(command="ofabarmor", params=[], access_level="member",
             description="Show ofab armor")
    def ofabarmor_list_command(self, request):
        data = self.db.query("SELECT type, profession FROM ofab_armor_type ORDER BY profession")

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

        return ChatBlob("Ofab Armor", blob)

    @command(command="ofabarmor",
             params=[Int("ql", is_optional=True), Any("profession"), Int("ql", is_optional=True)],
             access_level="member",
             description="Show info about ofab armor",
             extended_description="QL is optional and can come before or after the profession")
    def ofabarmor_show_command(self, request, ql1, prof_name, ql2):
        profession = self.util.get_profession(prof_name)
        ql = ql1 or ql2 or 300

        if not profession:
            return "Could not find Ofab Armor for profession <highlight>%s</highlight>." % prof_name

        data = self.db.query("SELECT * FROM ofab_armor o1 "
                             "LEFT JOIN ofab_armor_cost o2 ON o1.slot = o2.slot "
                             "WHERE o1.profession = ? AND o2.ql = ? "
                             "ORDER BY upgrade, name",
                             [profession, ql])
        if not data:
            return f"Could not find Ofab Armor for QL <highlight>{ql:d}</highlight>."

        upgrade_type = self.db.query_single("SELECT type FROM ofab_armor_type WHERE profession = ?", [profession]).type

        type_ql = round(ql * 0.8)
        type_link = self.text.make_tellcmd(f"Kyr'Ozch Bio-Material - Type {upgrade_type:d}",
                                           f"bioinfo {upgrade_type:d} {type_ql:d}")

        blob = "Upgrade with %s (minimum QL %d)\n\n" % (type_link, type_ql)

        cost_data = self.db.query("SELECT DISTINCT ql FROM ofab_weapons_cost ORDER BY ql")
        for row in cost_data:
            blob += self.text.make_tellcmd(row.ql, f"ofabarmor {profession} {row.ql:d}") + " "
        blob += "\n\n"

        current_upgrade = ""
        total_vp = 0
        for row in data:
            if current_upgrade != row.upgrade:
                current_upgrade = row.upgrade
                blob += "\n"

            blob += "<pagebreak>" + self.text.make_item(row.lowid, row.highid, ql, row.name)

            if row.upgrade == 0 or row.upgrade == 3:
                blob += f"  (<highlight>{row.vp:d}</highlight> VP)"
                total_vp += row.vp
            blob += "\n"

        blob += f"\nVP cost for full set: <highlight>{total_vp:d}</highlight>"

        return ChatBlob("%s Ofab Armor (QL %d)" % (profession, ql), blob)
