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


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

    def pre_start(self):
        self.db.load_sql_file(self.module_dir + "/sql/" + "dyna.sql", pre_optimized=True)
        self.db.create_view("dynadb")

    @command(command="dyna", params=[], access_level="member",
             description="Show a list of dyna mob types")
    def dyna_mob_types_command(self, _):
        data = self.db.query("SELECT mob, MIN(minQl) AS minQl, MAX(maxQl) AS maxQl "
                             "FROM dynadb GROUP BY mob ORDER BY mob")

        blob = ""
        for row in data:
            blob += "%s (%d - %d)\n" % (self.text.make_tellcmd(row.mob, "dyna %s" % row.mob), row.minQl, row.maxQl)

        return ChatBlob("Dyna Mobs (%d)" % len(data), blob)

    @command(command="dyna", params=[Int("level")], access_level="member",
             description="Show a list of dyna camps +/- 25 of QL")
    def dyna_level_command(self, _, level):
        min_level = level - 25
        max_level = level + 25

        data = self.db.query("SELECT * FROM dynadb d "
                             "JOIN playfields p ON d.playfield_id = p.id "
                             "WHERE d.minQl >= ? AND d.maxQl <= ? "
                             "ORDER BY minQl", [min_level, max_level])

        blob = f"Results of dyna camps between QL <highlight>{min_level:d}</highlight> " \
               f"and <highlight>{max_level:d}</highlight>\n\n"
        blob += self.format_results(data)
        # noinspection HttpUrlsUsage
        url = "http://creativestudent.com/ao/files-helpfiles.html"
        blob += "Dyna camp information taken from CSP help files: " + self.text.make_chatcmd(url, "/start " + url)

        return ChatBlob("Dyna Camps (%d)" % len(data), blob)

    @command(command="dyna", params=[Any("search")], access_level="member",
             description="Search for dyna camps based on playfield or mob type")
    def dyna_search_command(self, _, search):
        search_param = "%" + search + "%"
        data = self.db.query("SELECT * FROM dynadb d JOIN playfields p ON d.playfield_id = p.id "
                             "WHERE p.long_name LIKE ? OR p.short_name LIKE ? OR d.mob LIKE ? ORDER BY d.minQl",
                             [search_param, search_param, search_param])

        blob = f"Results of dyna camps search for <highlight>{search}</highlight>\n\n"
        blob += self.format_results(data)
        # noinspection HttpUrlsUsage
        url = "http://creativestudent.com/ao/files-helpfiles.html"
        blob += "Dyna camp information taken from CSP help files: " + self.text.make_chatcmd(url, "/start " + url)

        return ChatBlob(f"Dyna Camps ({len(data):d})", blob)

    def format_results(self, data):
        blob = ""
        for row in data:
            coordinates = self.text.make_chatcmd(f"{row.long_name} {row.cX:d}x{row.cY:d}",
                                                 f"/waypoint {row.cX:d} {row.cY:d} {row.playfield_id:d}")
            blob += "<pagebreak>" + coordinates + "\n"
            blob += f"{row.mob} - Level <highlight>{row.minQl:d}-{row.maxQl:d}</highlight>\n\n"
        return blob
