using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace Project.StateMachine
{
    public class AGVProtocolHandler
    {
        private static AGVProtocolHandler instance;
        private AGVStateManager stateManager;

        private AGVProtocolHandler()
        {
            stateManager = AGVStateManager.Instance;
        }

        public static AGVProtocolHandler Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new AGVProtocolHandler();
                }
                return instance;
            }
        }

        public bool ProcessProtocol(string protocol)
        {
            try
            {
                // 프로토콜 형식: COMMAND:DESTINATION
                var parts = protocol.Split(':');
                if (parts.Length != 2)
                {
                    return false;
                }

                var command = parts[0].ToUpper();
                var destination = parts[1].ToUpper();

                switch (command)
                {
                    case "MOVE_TO":
                        return HandleMoveToCommand(destination);
                    case "PICKUP":
                        return HandlePickupCommand(destination);
                    case "DROPOFF":
                        return HandleDropoffCommand(destination);
                    case "EMERGENCY_STOP":
                        return HandleEmergencyStopCommand();
                    default:
                        return false;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }

        private bool HandleMoveToCommand(string destination)
        {
            switch (destination)
            {
                case "T1":
                    return stateManager.ProcessCommand(AGVStateManager.AGVCommand.MoveToTops1);
                case "S1":
                    return stateManager.ProcessCommand(AGVStateManager.AGVCommand.MoveToSstron1);
                case "S2":
                    return stateManager.ProcessCommand(AGVStateManager.AGVCommand.MoveToSstron2);
                default:
                    return false;
            }
        }

        private bool HandlePickupCommand(string destination)
        {
            if (destination != "T1")
            {
                return false;
            }
            return stateManager.ProcessCommand(AGVStateManager.AGVCommand.PickupCart);
        }

        private bool HandleDropoffCommand(string destination)
        {
            if (destination != "S1" && destination != "S2")
            {
                return false;
            }
            return stateManager.ProcessCommand(AGVStateManager.AGVCommand.DropoffCart);
        }

        private bool HandleEmergencyStopCommand()
        {
            return stateManager.ProcessCommand(AGVStateManager.AGVCommand.EmergencyStop);
        }

        public string GetCurrentStatus()
        {
            var state = stateManager.CurrentState;
            return $"Current State: {state}";
        }
    }
} 