from mower.solvers.navigation import NavigationSolver
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: list,
        actions: list,
        groups: list = [],
    ):
        """
        Args:
            level_name: 关卡名
            opers: 干员列表
            actions: 行动列表
            groups: 干员组列表
        """
        logger.info("Start: 自动战斗")
        logger.info("地图坐标计算：https://github.com/yuanyan3060/Arknights-Tile-Pos")
        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 transition(self):
        if (scene := self.scene()) == Scene.OPERATOR_FIGHT:
            if self.loading:
                self.fight_init()
                return
            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.tap_element("story_skip")
        elif scene == Scene.STORY_SKIP:
            self.tap_element("story_skip_confirm_dialog", x_rate=0.94)
        else:
            NavigationSolver().run(self.level_name, mode="copy")
