package com.multictrl.common.constant;

import com.multictrl.modules.business.dto.WaypointActionDTO;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.Element;

/**
 * 动作类型
 *
 * @author Sdy
 * @since 1.0.0
 */
@Slf4j
public enum SetActionParams {
    takePhoto {//拍照

        @Override
        public void setActionParam(WaypointActionDTO actionDTO, Element actionActuatorFuncParam) {
            actionActuatorFuncParam.addElement("wpml:payloadPositionIndex").setText("0");
            actionActuatorFuncParam.addElement("wpml:fileSuffix").setText("航点" + actionDTO.getWaypointOrder());
            actionActuatorFuncParam.addElement("wpml:payloadLensIndex").setText("wide,zoom,ir,visable");
            actionActuatorFuncParam.addElement("wpml:useGlobalPayloadLensIndex").setText("1");
        }

        @Override
        public void getActionParam(WaypointActionDTO actionDTO, Element actionActuatorFuncParam) {
//            String payloadLensIndex = actionActuatorFuncParam.elementText("payloadLensIndex");

        }
    }, startRecord {//开始录像

        @Override
        public void setActionParam(WaypointActionDTO actionDTO, Element actionActuatorFuncParam) {
            takePhoto.setActionParam(actionDTO, actionActuatorFuncParam);

        }

        @Override
        public void getActionParam(WaypointActionDTO actionDTO, Element actionActuatorFuncParam) {

        }
    }, stopRecord {//结束录像

        @Override
        public void setActionParam(WaypointActionDTO actionDTO, Element actionActuatorFuncParam) {
            actionActuatorFuncParam.addElement("wpml:payloadPositionIndex").setText("0");
        }

        @Override
        public void getActionParam(WaypointActionDTO actionDTO, Element actionActuatorFuncParam) {

        }
    }, zoom {//变焦

        @Override
        public void setActionParam(WaypointActionDTO actionDTO, Element actionActuatorFuncParam) {
            actionActuatorFuncParam.addElement("wpml:payloadPositionIndex").setText("0");
            actionActuatorFuncParam.addElement("wpml:focalLength").setText(
                    Math.round(Double.parseDouble(actionDTO.getActionValue()) * 23.75f) + "");
            actionActuatorFuncParam.addElement("wpml:isUseFocalFactor").setText("0");
        }

        @Override
        public void getActionParam(WaypointActionDTO actionDTO, Element actionActuatorFuncParam) {
            String focalLength = actionActuatorFuncParam.elementText("focalLength");
            actionDTO.setActionValue(Math.round(Double.parseDouble(focalLength) / 23.75f) + "");
        }
    }, hover {//悬停

        @Override
        public void setActionParam(WaypointActionDTO actionDTO, Element actionActuatorFuncParam) {
            actionActuatorFuncParam.addElement("wpml:hoverTime").setText(actionDTO.getActionValue());
        }

        @Override
        public void getActionParam(WaypointActionDTO actionDTO, Element actionActuatorFuncParam) {
            String hoverTime = actionActuatorFuncParam.elementText("hoverTime");
            actionDTO.setActionValue(hoverTime);
        }
    }, rotateYaw {//飞行器偏航角

        @Override
        public void setActionParam(WaypointActionDTO actionDTO, Element actionActuatorFuncParam) {
            actionActuatorFuncParam.addElement("wpml:aircraftHeading").setText(actionDTO.getActionValue());
            actionActuatorFuncParam.addElement("wpml:aircraftPathMode").setText("counterClockwise");
        }

        @Override
        public void getActionParam(WaypointActionDTO actionDTO, Element actionActuatorFuncParam) {
            String aircraftHeading = actionActuatorFuncParam.elementText("aircraftHeading");
            actionDTO.setActionValue(aircraftHeading);
        }
    }, gimbalRotate {//云台俯仰角

        @Override
        public void setActionParam(WaypointActionDTO actionDTO, Element actionActuatorFuncParam) {
            setGimbalParam(actionActuatorFuncParam);
            actionActuatorFuncParam.addElement("wpml:gimbalYawRotateEnable").setText("0");
            actionActuatorFuncParam.addElement("wpml:gimbalYawRotateAngle").setText("0");
            actionActuatorFuncParam.addElement("wpml:gimbalPitchRotateEnable").setText("1");
            actionActuatorFuncParam.addElement("wpml:gimbalPitchRotateAngle").setText(actionDTO.getActionValue());
        }

        @Override
        public void getActionParam(WaypointActionDTO actionDTO, Element actionActuatorFuncParam) {
            String gimbalPitchRotateAngle = actionActuatorFuncParam.elementText("gimbalPitchRotateAngle");
            actionDTO.setActionValue(gimbalPitchRotateAngle);
        }
    }, gimbalRotate_yaw {//云台偏航角

        @Override
        public void setActionParam(WaypointActionDTO actionDTO, Element actionActuatorFuncParam) {
            setGimbalParam(actionActuatorFuncParam);
            actionActuatorFuncParam.addElement("wpml:gimbalPitchRotateEnable").setText("0");
            actionActuatorFuncParam.addElement("wpml:gimbalPitchRotateAngle").setText("0");
            actionActuatorFuncParam.addElement("wpml:gimbalYawRotateEnable").setText("1");
            actionActuatorFuncParam.addElement("wpml:gimbalYawRotateAngle").setText(actionDTO.getActionValue());
        }

        @Override
        public void getActionParam(WaypointActionDTO actionDTO, Element actionActuatorFuncParam) {
            String gimbalYawRotateAngle = actionActuatorFuncParam.elementText("gimbalYawRotateAngle");
            actionDTO.setActionValue(gimbalYawRotateAngle);
        }
    }, panoShot {//全景拍照

        @Override
        public void setActionParam(WaypointActionDTO actionDTO, Element actionActuatorFuncParam) {
            takePhoto.setActionParam(actionDTO, actionActuatorFuncParam);
            actionActuatorFuncParam.addElement("wpml:panoShotSubMode").setText("panoShot_360");
        }

        @Override
        public void getActionParam(WaypointActionDTO actionDTO, Element actionActuatorFuncParam) {

        }
    }, multipleTiming {//等时拍照

        @Override
        public void setActionParam(WaypointActionDTO actionDTO, Element actionActuatorFuncParam) {
            takePhoto.setActionParam(actionDTO, actionActuatorFuncParam);
        }

        @Override
        public void getActionParam(WaypointActionDTO actionDTO, Element actionActuatorFuncParam) {

        }
    }, multipleDistance {//等距拍照

        @Override
        public void setActionParam(WaypointActionDTO actionDTO, Element actionActuatorFuncParam) {
            takePhoto.setActionParam(actionDTO, actionActuatorFuncParam);
        }

        @Override
        public void getActionParam(WaypointActionDTO actionDTO, Element actionActuatorFuncParam) {

        }
    };

    /**
     * 设置参数
     */
    public abstract void setActionParam(WaypointActionDTO actionDTO, Element actionActuatorFuncParam);

    /**
     * 获取值
     */
    public abstract void getActionParam(WaypointActionDTO actionDTO, Element actionActuatorFuncParam);

    /**
     * 根据传入的动作类型执行对应的方法
     *
     * @param actionDTO               动作
     * @param actionActuatorFuncParam 参数元素
     */
    public static void executeAction(WaypointActionDTO actionDTO, Element actionActuatorFuncParam) {
        try {
            SetActionParams.valueOf(actionDTO.getActionType()).setActionParam(actionDTO, actionActuatorFuncParam);
        } catch (IllegalArgumentException e) {
            // 处理未知动作类型的异常情况
            System.err.println("未知的动作类型: " + actionDTO.getActionType());
        }
    }

    /**
     * 根据传入的动作类型执行对应的方法
     *
     * @param actionDTO               动作
     * @param actionActuatorFuncParam 参数元素
     */
    public static void getAction(WaypointActionDTO actionDTO, Element actionActuatorFuncParam) {
        try {
            SetActionParams.valueOf(actionDTO.getActionType()).getActionParam(actionDTO, actionActuatorFuncParam);
        } catch (IllegalArgumentException e) {
            // 处理未知动作类型的异常情况
            log.error("未知的动作类型: {}", actionDTO.getActionType());
        }
    }

    /**
     * 云台参数
     */
    private static void setGimbalParam(Element actionActuatorFuncParam) {
        actionActuatorFuncParam.addElement("wpml:gimbalRotateMode").setText("absoluteAngle");
        actionActuatorFuncParam.addElement("wpml:gimbalRollRotateEnable").setText("0");
        actionActuatorFuncParam.addElement("wpml:gimbalRollRotateAngle").setText("0");
        actionActuatorFuncParam.addElement("wpml:gimbalRotateTimeEnable").setText("0");
        actionActuatorFuncParam.addElement("wpml:gimbalRotateTime").setText("0");
        actionActuatorFuncParam.addElement("wpml:payloadPositionIndex").setText("0");
    }
}
