from mower.solvers.navigation import NavigationSolver
from mower.utils import config
from mower.utils import typealias as tp
from mower.utils.graph import SceneGraphSolver
from mower.utils.log import logger
from mower.utils.scene import Scene
from mower.utils.tile_pos import Calc, find_level

from .mixin import FightMixin


class AutoFight(SceneGraphSolver, FightMixin):
    solver_name = "自动战斗"

    def run(
        self,
        level_name: str,
        opers: tp.Opers = [],
        actions: list = [],
        groups: tp.Groups = [],
    ):
        """
        Args:
            level_name: 关卡名
            opers: 干员列表
            actions: 行动列表
            groups: 干员组列表
        """
        logger.info("地图坐标计算：https://github.com/yuanyan3060/Arknights-Tile-Pos")
        if level_name.endswith("#"):
            level_name = level_name[:-1]
        self.level_name = level_name
        if (level := find_level(level_name, level_name)) is None:
            logger.error(f"找不到关卡：{level_name}")
            return False
        self.calc = Calc(1920, 1080, level)
        self.opers: list[dict] = opers  # 干员列表
        self.groups = groups  # 干员组
        self.strategies_copy = []  # 无策略,用于统一与保全的格式
        self.actions_copy = actions  # 行动列表备份
        self.success = True
        self.reset()
        super().run()
        return self.success

    def kills(self) -> int:
        "获取击杀数"
        return super().kills(((800, 30), (950, 60)))

    def check_name(self, name):
        return super().check_name(name, ((10, 270), (280, 320)))

    def breach_solver(self):
        if (strategy := config.conf.work.enemy_breach_strategy) == "give_up":
            self.scene_graph_navigation(Scene.OPERATOR_FAILED)
        elif strategy == "restart_game":
            config.device.exit()
            self.success = False
            return True
        return False

    def transition(self):
        if (scene := self.scene()) == Scene.OPERATOR_FIGHT:
            if self.loading:
                self.fight_init()
                return
            if self.find("fight/pause_sign") and not self.find(
                "fight/pause"
            ):  # 对话剧情
                self.tap((20, 20))
                return
            if self.find("fight/breach"):
                return self.breach_solver()
            self.travel_watching_skills()
            self.execute_action()

        elif scene == Scene.OPERATOR_FAILED:
            logger.warning("行动失败，请检查干员/练度")
            self.success = False
            return True

        elif scene == Scene.OPERATOR_FINISH:
            logger.info("行动结束")
            return True

        elif scene in self.waiting_scene:
            self.waiting_solver()
        elif scene == Scene.OPERATOR_SELECT:
            self.tap((1655, 781))
        elif scene == Scene.STORY:
            self.ctap("story_skip", 3)
        elif scene == Scene.STORY_SKIP:
            self.tap("story_skip_confirm_dialog", x_rate=0.94)
        elif scene == Scene.OPERATOR_BAN:
            self.tap((1400, 900))
        else:
            NavigationSolver().run(self.level_name, mode="copy")
