from mower.solvers.infra.base_choose import RIIC_ChooseSolver
from mower.solvers.infra.drone import DroneSolver
from mower.solvers.infra.get_agent_from_room import GetAgentFromRoomSolver
from mower.solvers.infra.get_order_remaining_time import (
    GetOrderRemainingTimeSolver,
)
from mower.utils import config
from mower.utils.digit_reader import DigitReader
from mower.utils.graph import SceneGraphSolver
from mower.utils.log import logger

from .read_original_order_remaining_time import ReadOriginalOrderRemainTimeSolver
from .wait_for_order import WaitForOrderSolver

limit_time = 1800


class RunOrderSolver(SceneGraphSolver):
    def run(
        self,
        room: str,
        agent_list: list,
        drone: bool = False,
    ) -> None:
        logger.info("Start:跑单插拔")
        # 读取原来的干员列表
        original_agent_list = [
            data["agent"] for data in GetAgentFromRoomSolver().run(room)
        ]
        # 使用无人机加速
        if drone:
            # 读取当前无人机数量避免重复开关
            cur_drone_count = DigitReader().get_drone(config.recog.gray)
            drone_count = ReadOriginalOrderRemainTimeSolver().run(room) // 180 - 1
            DroneSolver().run(room, drone_count, cur_count=cur_drone_count)

        wait_time = max(
            GetOrderRemainingTimeSolver().run(room) - config.conf.run_order_buffer_time,
            0,
        )
        if wait_time > limit_time:
            logger.error("检测到漏单")
            return
        logger.info(f"等待时间:{wait_time}")
        # 换上跑单干员等待
        RIIC_ChooseSolver().run(room, agent_list, wait_time)
        if (wait_time := GetOrderRemainingTimeSolver().run(room)) > limit_time:
            logger.error("检测到漏单")
            return
        logger.info(f"等待时间:{wait_time}")
        # 等待订单完成
        WaitForOrderSolver().run(room, wait_time)
        # 换回原来的干员
        RIIC_ChooseSolver().run(room, original_agent_list)
