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


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

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

    def start(self):
        self.command_alias_service.add_alias("playfields", "playfield")

    @command(command="playfield", params=[Const("all", is_optional=True)], access_level="member",
             description="Show a list of playfields")
    def playfield_list_command(self, _, const_all):
        if const_all:
            data = self.db.query("SELECT * FROM playfields ORDER BY long_name")
        else:
            data = self.db.query("SELECT * FROM playfields WHERE short_name != '' ORDER BY long_name")

        blob = ""
        for row in data:
            blob += f"[<highlight>{row.id:d}</highlight>] {row.long_name} ({row.short_name})\n"

        return ChatBlob("Playfields", blob)

    @command(command="waypoint",
             params=[Regex("waypoint_data", r"\s+.*?Pos: ([0-9.]+), ([0-9.]+), ([0-9.]+), Area: ([a-zA-Z ]+).*",
                           num_groups=4)],
             access_level="member",
             description="Create a waypoint link from F9 output",
             extended_description="Example: <symbol>waypoint Pos: 123.1, 456.1, 789.1, Area: Perpetual Wastelands")
    def waypoint1_command(self, _, regex):
        x_coords, y_coords, _, playfield_arg = regex

        return self.create_waypoint_blob(x_coords, y_coords, playfield_arg)

    @command(command="waypoint",
             params=[Regex("waypoint_data", r"\s+.*?([0-9.]+) ([0-9.]+) y ([0-9.]+) ([0-9]+).*",
                           num_groups=4)],
             access_level="member",
             description="Create a waypoint link from Shift + F9 output",
             extended_description="Example: <symbol>waypoint 123.1 456.1 y 789.1 570")
    def waypoint2_command(self, _, regex):
        x_coords, y_coords, _, playfield_arg = regex

        return self.create_waypoint_blob(x_coords, y_coords, playfield_arg)

    @command(command="waypoint",
             params=[Int("x_coords"), Int("y_coords"), Any("playfield")],
             access_level="member",
             description="Manually create a waypoint link",
             extended_description="Example: !waypoint 123 456 PW")
    def waypoint3_command(self, _, x_coords, y_coords, playfield_arg):
        return self.create_waypoint_blob(x_coords, y_coords, playfield_arg)

    def create_waypoint_blob(self, x_coords, y_coords, playfield_arg):
        x_coords = int(float(x_coords))
        y_coords = int(float(y_coords))
        playfield = self.get_playfield_by_name(playfield_arg) or self.get_playfield_by_id(playfield_arg)

        if not playfield:
            return f"Could not find playfield <highlight>{playfield_arg}</highlight>."
        else:
            title = f"waypoint: {x_coords}x{y_coords} {playfield.long_name}"
            blob = f"Zone: {playfield.long_name} ({playfield.id})\n"
            blob += f"Coords: {x_coords} x {y_coords}\n\n"
            waypoint = f'/waypoint {x_coords} {y_coords} {playfield.id:d}'
            blob += f"<center>{self.text.make_chatcmd(self.text.make_image(11336), waypoint)}\n"
            blob += self.text.make_chatcmd("Click for waypoint", f"/waypoint {x_coords} {y_coords} {playfield.id:d}")

            return ChatBlob(title, blob)

    def get_playfield_by_name(self, name):
        return self.db.query_single("SELECT * FROM playfields "
                                    "WHERE long_name LIKE ? "
                                    "OR short_name LIKE ? "
                                    "LIMIT 1", [name, name])

    def get_playfield_by_name_or_id(self, name):
        return self.db.query_single("SELECT * FROM playfields "
                                    "WHERE long_name LIKE ? "
                                    "OR short_name LIKE ? "
                                    "OR id LIKE ? "
                                    "LIMIT 1", [name, name, name])

    def get_playfield_by_id(self, playfield_id):
        return self.db.query_single("SELECT * FROM playfields "
                                    "WHERE id = ?", [playfield_id])
