// Medulla虚拟遥控器和夹臂控制
using MDCSToolBox.Medulla.Chassis.MultiWheel;
using CartActivator;

namespace MedullaAdapter
{
    // M层单车虚拟遥控器：使用父类提供的底盘控制界面。
    public class Remote : MultiWheelRemote<DiverCartDefinition>
    {
        // 将父类虚拟遥控器界面输入统一转发到本车的ManualControl。
        public override void ChassisOperation()
        {
            if (MultiVehicleMode.on)
            {
                MultiVehicleModeChassisLogic();
                statusText = "单车版本不支持多车联动遥控";
                return;
            }

            // 当前单车适配层只定义Normal、Crab和Spin三种模式。
            // 禁止这些旧按钮绕过适配层直接修改底盘坐标偏置。
            if (AckermannMode.on ||
                SwayMode.on ||
                XYThMode.on)
            {
                cart.Chassis?.PredefinedDriveStop();
                statusText = "当前单车版本暂不支持阿克曼、斜行或全向模式";
                return;
            }

            var mode = SpinMode.on
                ? DiverCartDefinition.ManualControlMode.Spin
                : CrabMode.on
                    ? DiverCartDefinition.ManualControlMode.Crab
                    : DiverCartDefinition.ManualControlMode.Normal;

            cart.ManualControl(
                mode,
                SpeedPad.x,
                SpeedPad.y,
                FrontDirection.dval * 180,
                SpeedThreshold.val);

            statusText =
                $"{mode}, x={SpeedPad.x:0.00}, " +
                $"y={SpeedPad.y:0.00}, " +
                $"speed={SpeedThreshold.val:0.00}";
        }
        [AsControlItem(name = "夹抱速度", LayoutRow = 0, LayoutCol = 4)]
        public Throttle ArmSpeed;

        [AsControlItem(name = "夹抱打开", LayoutRow = 1, LayoutCol = 0)]
        public Button Open;

        [AsControlItem(name = "夹抱关闭", LayoutRow = 1, LayoutCol = 2)]
        public Button Close;
        // M层虚拟遥控器：控制左右夹臂同步打开或关闭。
        public override void CustomOperation()
        {
            if (Open.pressed)
            {
                cart.SpeedLeftArm =
                    -ArmSpeed.val * cart.ManualArmSpeedFac;
                cart.SpeedRightArm =
                    -ArmSpeed.val * cart.ManualArmSpeedFac;
            }
            else if (Close.pressed)
            {
                cart.SpeedLeftArm =
                    ArmSpeed.val * cart.ManualArmSpeedFac;
                cart.SpeedRightArm =
                    ArmSpeed.val * cart.ManualArmSpeedFac;
            }
            else
            {
                cart.SpeedLeftArm = 0;
                cart.SpeedRightArm = 0;
            }
        }
        // 单车不支持多车联动，误打开多车开关时主动停车。
        public override void MultiVehicleModeChassisLogic()
        {
            cart.Chassis?.PredefinedDriveStop();
        }
    }
}
