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


@instance()
class ResearchController:
    def inject(self, registry):
        self.db: 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/" + "research.sql", pre_optimized=True)
        self.db.create_view("research")

    @command(command="research", params=[Int("research_level")], access_level="member",
             description="Show information about a specific research level")
    def research_command(self, _, research_level):
        if research_level > 10 or research_level < 1:
            return "Research level must be between 1 and 10."

        row = self.db.query_single("SELECT * FROM research WHERE level = ?", [research_level])

        capsk = int(row.sk * 0.1)

        blob = f"You must be level <highlight>{row.levelcap:d}</highlight> to research " \
               f"<highlight>Research Level {research_level:d}</highlight>.\n"
        blob += f"You need <highlight>{self.util.format_number(row.sk)} SK</highlight> to reach " \
                f"<highlight>Research Level {research_level:d}</highlight> per research line.\n\n"
        blob += f"This equals <highlight>{self.util.format_number(row.sk * 1000)} XP</highlight>.\n\n"
        blob += f"Your research will cap at <highlight>{self.util.format_number(capsk * 1000)} XP</highlight> or " \
                f"<highlight>{self.util.format_number(capsk)} SK</highlight>."

        return ChatBlob("Research Level %d" % research_level, blob)

    @command(command="research", params=[Int("research_level"), Int("research_level")], access_level="member",
             description="Show the amount of SK needed from one research level to another")
    def research_span_command(self, _, research_level1, research_level2):
        if research_level1 > 10 or research_level1 < 1 or research_level2 > 10 or research_level2 < 1:
            return "Research level must be between 1 and 10."
        elif research_level1 == research_level2:
            return "You must specify different research levels."

        if research_level1 > research_level2:
            # swap researches so the lower is research_level1 and higher is research_level2
            research_level1, research_level2 = research_level2, research_level1

        row = self.db.query_single("SELECT SUM(sk) AS total_sk, MAX(levelcap) AS levelcap FROM research "
                                   "WHERE level > ? AND level <= ?",
                                   [research_level1, research_level2])

        blob = f"You must be <highlight>Level {row.levelcap:d}</highlight> to reach " \
               f"Research Level <highlight>{research_level2:d}.</highlight>\n"
        blob += f"It takes <highlight>{self.util.format_number(row.total_sk)} SK</highlight> to go from " \
                f"Research Level <highlight>{research_level1:d}</highlight> to " \
                f"Research Level <highlight>{research_level2:d}</highlight> per research line.\n\n"
        blob += f"This equals <highlight>{self.util.format_number(row.total_sk * 1000)} XP</highlight>."

        return ChatBlob(f"Research Levels {research_level1:d} - {research_level2:d}", blob)
