from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from isar.state_machine.state_machine import StateMachine

from isar.apis.models.models import ControlMissionResponse
from robot_interface.models.mission.status import MissionStatus, TaskStatus


def trigger_stop_mission_event(state_machine: "StateMachine") -> bool:
    state_machine.events.state_machine_events.stop_mission.trigger_event(True)
    return True


def stop_mission_cleanup(state_machine: "StateMachine") -> bool:
    if state_machine.current_mission is None:
        state_machine._queue_empty_response()
        state_machine.reset_state_machine()
        return True

    state_machine.current_mission.status = MissionStatus.Cancelled

    for task in state_machine.current_mission.tasks:
        if task.status in [
            TaskStatus.NotStarted,
            TaskStatus.InProgress,
            TaskStatus.Paused,
        ]:
            task.status = TaskStatus.Cancelled

    stopped_mission_response: ControlMissionResponse = (
        state_machine._make_control_mission_response()
    )
    state_machine.events.api_requests.stop_mission.response.put(
        stopped_mission_response
    )
    state_machine.publish_task_status(task=state_machine.current_task)
    state_machine._finalize()
    return True


def stop_mission_failed(state_machine: "StateMachine") -> bool:
    stopped_mission_response: ControlMissionResponse = (
        state_machine._make_control_mission_response()
    )
    state_machine.events.api_requests.stop_mission.response.put(
        stopped_mission_response
    )
    return True


def stop_return_home_mission_cleanup(state_machine: "StateMachine") -> bool:
    if state_machine.current_mission is None:
        state_machine._queue_empty_response()
        state_machine.reset_state_machine()
        return True

    if not state_machine.events.api_requests.start_mission.request.has_event():
        state_machine.current_mission.status = MissionStatus.Cancelled

        for task in state_machine.current_mission.tasks:
            if task.status in [
                TaskStatus.NotStarted,
                TaskStatus.InProgress,
                TaskStatus.Paused,
            ]:
                task.status = TaskStatus.Cancelled

        stopped_mission_response: ControlMissionResponse = (
            state_machine._make_control_mission_response()
        )
        state_machine.events.api_requests.stop_mission.response.put(
            stopped_mission_response
        )

    state_machine._finalize()
    return True


def stop_return_home_mission_failed(state_machine: "StateMachine") -> bool:
    if state_machine.events.api_requests.start_mission.request.has_event():
        return True
    stopped_mission_response: ControlMissionResponse = (
        state_machine._make_control_mission_response()
    )
    state_machine.events.api_requests.stop_mission.response.put(
        stopped_mission_response
    )
    return True
