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

from .battle_agent_choose import BattleAgentChooseSolver
from .battle_fill_choose import BattleFillChooseSolver
from .choose_squad import ChooseSquadSolver
from .choose_support import ChooseSupportSolver


class BattleChooseSolver(SceneGraphSolver):
    def run(
        self,
        level_name: str = "",
        opers: list = [],
        groups: list = [],
        support_oper: list = [],
    ):
        """
        Args:
            level_name: 关卡名
            opers: 干员列表
            groups: 干员组列表
            squad: 队伍编号
            support_oper: 助战干员
        """
        if config.conf.work.squad:
            ChooseSquadSolver().run(level_name, config.conf.work.squad)
        lack = BattleAgentChooseSolver().run(opers + groups, level_name)
        if len(lack) + len(support_oper) >= 2:
            return False
        if config.conf.choose.add_low_trust_opers:
            num = 12 - len(opers) - len(groups) + len(lack)
            BattleFillChooseSolver().run("信赖值", num)
        super().run()
        support_oper = support_oper if support_oper else lack
        ChooseSupportSolver().run(support_oper)
        return True

    def transition(self):
        if (scene := self.scene()) == Scene.OPERATOR_AGENT_SELECT:
            self.tap_element("choose_agent/battle_confirm")
        elif scene in self.waiting_scene:
            self.waiting_solver()
        else:
            return True
