// This file contains the custom version of your APGA gauge for your aircraft autopilot.
// Usually, this is not necessary to customize it (except parameters) but some autopilots may require some specific logic.

// The APGA code is open source.
// You can use it, modify it and deliver it in your aircraft package without any authorization from PMS50.
// However, PMS50 will not provide any support if you modify the source code (except parameters).



// Update this class if you want to change the default APGA behavior
class MY_APGA extends PMS50_APGA {
    constructor() {
        super();
        // Updated parameters
        this.altPreselMode = altPreselMode.manual;
        this.useVsForArmedVerticalCheck = false;
        this.targetAltitudeReachedThreshold = 50;   // Target altitude reached phase threshold
        this.defaultLateralMode = lateralMode.lvl;
    }
    connectedCallback() {
        // Called when the instrument is connected
        super.connectedCallback();
    }
    init() {
        super.init();
    }
    onUpdate(_deltaTime) {
        super.onUpdate(_deltaTime);
        if(this.isAutopilotEngaged()) {
            // If the KAP140 is in ROLL mode, we must force it to LVL mode
            if(this.lateralMode == lateralMode.roll && !this.settingLVL) {
                this.settingLVL = true;
                SimVar.SetSimVarValue("K:AP_WING_LEVELER_ON", "number", 0);
                setTimeout(()=>{
                    this.settingLVL = false;
                }, 1000);
            }
            // If the KAP140 is in PITCH mode, we must force it to VS mode
            if(this.verticalMode == verticalMode.pitch && !this.settingVS) {
                this.settingVS = true;
                this.onEventVS();
                setTimeout(()=>{
                    this.settingVS = false;
                }, 1000);
            }
        }
    }

    onEvent(_event) {
        super.onEvent(_event);
    }

    // Events
    onEventAP() {
        let apOn = this.isAutopilotEngaged();
        super.onEventAP();
        if (!apOn) {
            setTimeout(()=>{
                SimVar.SetSimVarValue("K:AP_WING_LEVELER_ON", "number", 0);
                if(this.verticalMode != verticalMode.vs)
                    this.onEventVS();
                else {
                    // Be sure to engage with the current VS in all cases
                    // Necessary with the stock sim
                    let vsValue = SimVar.GetSimVarValue("VERTICAL SPEED", "feet per second")*60;
                    vsValue = 100 * Math.round(vsValue / 100);
                    SimVar.SetSimVarValue("AUTOPILOT VERTICAL HOLD VAR", "feet/minute", vsValue);
                }
                // Auto arm when engaging the AP
                if(!SimVar.GetSimVarValue("L:PMS50_APGA_ARM_BUTTON_STATE", "Bool"))
                    postHEvent("PMS50_APGA_AP_ARM");
            }, 500);
        }
    }
    onEventFD() {
        super.onEventFD();
    }
    onEventYD() {
        super.onEventYD();
    }
    onEventHDG() {
        if(this.isAutopilotEngaged())
            super.onEventHDG();
    }
    onEventNAV() {
        if(this.isAutopilotEngaged())
            super.onEventNAV();
    }
    onEventAPR() {
        if(this.isAutopilotEngaged())
            super.onEventAPR();
    }
    onEventREV() {
        if(this.isAutopilotEngaged())
            super.onEventREV();
    }
    onEventALT() {
        if(this.isAutopilotEngaged()) {
            let currentVerticalMode = this.verticalMode;
            super.onEventALT();
            if(currentVerticalMode == verticalMode.alt) {
                // Engage VS when leaving ALT mode
                this.onEventVS();
            }
        }
    }
    onEventVS() {
        super.onEventVS();
    }
    onEventFLC() {
        super.onEventFLC();
    }
    onEventLVL() {
        super.onEventLVL();
    }
    onEventCGA() {
        super.onEventCGA();
    }
    onEventCLVL() {
        super.onEventCLVL();
    }
    onEventARM() {
        super.onEventARM();
    }
    onEventARM_PRESEL() {
        super.onEventARM_PRESEL();
    }
    onEventUP() {
        super.onEventUP();
    }
    onEventDOWN() {
        super.onEventDOWN();
    }
    onEventSEL_ALT_SMALL_INC() {
        super.onEventSEL_ALT_SMALL_INC();
    }
    onEventSEL_ALT_LARGE_INC() {
        super.onEventSEL_ALT_LARGE_INC();
    }
    onEventSEL_ALT_VERY_LARGE_INC() {
        super.onEventSEL_ALT_VERY_LARGE_INC();
    }
    onEventSEL_ALT_SMALL_DEC() {
        super.onEventSEL_ALT_SMALL_DEC();
    }
    onEventSEL_ALT_LARGE_DEC() {
        super.onEventSEL_ALT_LARGE_DEC();
    }
    onEventSEL_ALT_VERY_LARGE_DEC() {
        super.onEventSEL_ALT_VERY_LARGE_DEC();
    }
    onEventSEL_VS_SMALL_INC() {
        super.onEventSEL_VS_SMALL_INC();
    }
    onEventSEL_VS_SMALL_DEC() {
        super.onEventSEL_VS_SMALL_DEC();
    }
    onEventSEL_VS_LARGE_INC() {
        super.onEventSEL_VS_LARGE_INC();
    }
    onEventSEL_VS_LARGE_DEC() {
        super.onEventSEL_VS_LARGE_DEC();
    }

    // Update the current lateral mode
    updateActiveLateralMode() {
        super.updateActiveLateralMode();
    }

    // Update the current vertical mode
    updateActiveVerticalMode() {
        super.updateActiveVerticalMode();
    }

    // Update the armed lateral mode
    updateArmedLateralMode() {
        super.updateArmedLateralMode();
    }

    // Update the armed vertical modes (global, altitude and approach)
    updateArmedVerticalModes() {
        super.updateArmedVerticalModes();
    }

    // Update the selected altitude phase
    updateSelectedAltitudePhase() {
        super.updateSelectedAltitudePhase();
    }

    // set the target selected altitude
    setSelectedAltitude(_value) {
        super.setSelectedAltitude(_value);
    }

    // Send the selected altitude to the sim
    setSimSelectedAltitude(_value, _blockUpdate = false) {
        super.setSimSelectedAltitude(_value, _blockUpdate);
    }

    // Enable / disable combined mode (CLVL, CGA)
    enableCombinedMode(mode, enable) {
        super.enableCombinedMode(mode, enable);
    }

    // Engage or disengage VS mode
    setVsVerticalMode(enable) {
        super.setVsVerticalMode(enable);
    }

    // Check and update the combined level mode
    checkUpdateLevelMode() {
        super.checkUpdateLevelMode();
    }

    // Disable any combined mode (CLVL, CGA)
    disableAnyCombinedMode() {
        super.disableAnyCombinedMode();
    }

    // Set LVars for reporting the gauge state
    setLvars() {
        super.setLvars();
    }
}


registerInstrument('pms50-autopilot', MY_APGA);
