from mower.utils.graph.utils import SceneGraphSolver
from mower.utils.log import logger
from mower.utils.scene import Scene

professions = [
    "PIONEER",
    "WARRIOR",
    "TANK",
    "SNIPER",
    "CASTER",
    "MEDIC",
    "SUPPORT",
    "SPECIAL",
]


class RIICTagChoose(SceneGraphSolver):
    def run(self, tag) -> None:
        self.tag = tag
        self.first_tag = None
        self.turned = True
        return super().run()

    def get_now_tag(self):
        if self.find("choose_agent/foldup"):
            return "ALL"

        if arrow := self.find("choose_agent/profession/choose_arrow"):
            return professions[int((arrow[0][1] - 120) / 120)]

    def tag_choosed(self, profession: str = "ALL"):
        str = "choose_agent/profession/" + profession
        if profession == "ALL":
            if self.find("choose_agent/foldup"):
                return True

            self.ctap(str, id=str)
            return
        elif self.get_now_tag() != profession:
            self.ctap(str, id=str)
            return

        return True

    def transition(self) -> bool:
        if (scene := self.scene()) == Scene.RIIC_OPERATOR_SELECT:
            if pos := self.find("choose_agent/riic/open_profession"):
                self.ctap(pos, self.ctap(pos))
                return

            if self.first_tag is None:
                self.first_tag = self.get_now_tag()
                if self.first_tag == self.tag:
                    self.turned = False

            if self.first_tag == self.tag:
                if self.turned is False:
                    if self.first_tag == "ALL" and self.tag == "ALL":
                        if self.tag_choosed("PIONEER"):
                            logger.debug(f"切换职业：{self.first_tag}-->PIONEER")
                            self.turned = True
                    else:
                        if self.tag_choosed():
                            logger.debug(f"切换职业：{self.first_tag}-->ALL")
                            self.turned = True
                else:
                    self.first_tag = self.get_now_tag()
            if self.turned:
                now_tag = self.get_now_tag()
                if self.tag != now_tag:
                    logger.debug(f"切换职业：{now_tag}-->{self.tag}")
                    self.tag_choosed(self.tag)
                else:
                    return True
        elif scene in self.waiting_scene:
            self.waiting_solver()

        else:
            return True
