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


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

        self.professions = [
            "PIONEER",
            "WARRIOR",
            "TANK",
            "SNIPER",
            "CASTER",
            "MEDIC",
            "SUPPORT",
            "SPECIAL",
        ]
        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"):
            for i in self.professions:
                str = "choose_agent/profession/" + i
                pos = self.find(str)
                if pos[0][1] > arrow[0][1] and pos[1][1] < arrow[1][1]:
                    return i

        return False

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

        elif pos := self.find(str):
            arrow = self.find("choose_agent/profession/choose_arrow")

            if arrow is None:
                self.ctap(pos, interval=config.screenshot_avg / 1000)
                return
            else:
                if pos[0][1] > arrow[0][1] and pos[1][1] < arrow[1][1]:
                    return True
                else:
                    self.ctap(pos, interval=config.screenshot_avg / 1000)
                    return

    def transition(self) -> bool:
        # if self.find("fight/collection") or self.find("fight/collection_on"):
        if self.scene() == Scene.OPERATOR_AGENT_SELECT:
            if pos := self.find("choose_agent/open_profession"):
                self.ctap(pos)

            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}-->全部")
                            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

        else:
            return False
