from core.chat_blob import ChatBlob
from core.command_param_types import Any, Const, Character, Options
from core.decorators import instance, command, event
from modules.standard.raid.leader_controller import LeaderController


@instance()
class AssistController:
    def __init__(self):
        self.assist = []

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

    @command(command="caller", params=[], access_level="member",
             description="Show current assist targets")
    def assist_command(self, _):
        blob = ""
        for caller in self.assist:
            blob += caller + f" - [<a href='chatcmd:///assist {caller}'>assist</a>] " \
                             f"[<a href='chatcmd:///callers rem {caller}'>REM</a>]<br>"
        blob += self.get_assist_output()
        return ChatBlob(f"Callers ({len(self.assist):d})", blob)

    @command(command="caller", params=[Const("clear")], access_level="leader",
             description="Clear all assist targets", sub_command="modify")
    def assist_clear_command(self, request, _):
        if not self.assist:
            return "No assist targets set."

        if not self.leader_controller.can_use_command(request.sender.char_id):
            return LeaderController.NOT_LEADER_MSG
        else:
            self.assist = []
            return "Assist targets have been cleared."

    @command(command="caller", params=[Options(["rem", "del"]), Character("char")], access_level="leader",
             description="Remove an assisting target", sub_command="modify")
    def assist_remove_command(self, request, _, char):
        if not self.assist:
            return "No assist targets set."

        if not self.leader_controller.can_use_command(request.sender.char_id):
            return LeaderController.NOT_LEADER_MSG
        else:
            try:
                self.assist.remove(char.name)
                return f"<highlight>{char.name}</highlight> is no longer a caller. " \
                       f"Use: <highlight>{self.get_assist_output()}</highlight>"
            except ValueError:
                return f"<highlight>{char.name}</highlight> is not on the caller list."

    @command(command="caller", params=[Any("assist_targets")], access_level="leader",
             description="Set one or more assist targets",
             sub_command="modify",
             extended_description="Multiple assist targets should be space-delimited")
    def assist_set_command(self, request, assist_targets):
        targets = assist_targets.split(" ")

        if not self.leader_controller.can_use_command(request.sender.char_id):
            return LeaderController.NOT_LEADER_MSG
        else:
            for caller in targets:
                print(targets)
                self.assist.append(caller)
            return self.assist_command(request)

    def get_assist_output(self):
        if not self.assist:
            return "No assist targets set."

        return "/macro assist " + "\\n ".join(map(lambda x: "/assist " + x.capitalize(), reversed(self.assist)))

    @event("RAID_END", "automatic caller clearing on raid end")
    def leader_remove_on_leave_private(self, _, event_data):
        if self.assist:
            self.assist = []
