// APGA example for the KMC321 autopilot
//
// Please replace MyCompany-MyAircraft in the KMC321.html file by your own Unique aircraft ident.

// No screen in the KMC321
// We just implement the standard APGA events for beeing used by external application or hardware controler.
//

// Workaround to a sim issue when posting H events after restarting the aircraft
function postHEvent(event) {
    let useWatcher = SimVar.g_bUseWatcher;
    if(useWatcher)
        SimVar.g_bUseWatcher = false;
    SimVar.SetSimVarValue("H:" + event, "number", 0);
    if(useWatcher)
        SimVar.g_bUseWatcher = true;
}


class KMC321 extends BaseInstrument {
    constructor() {
        super();
    }
    get templateID() { return "KMC321"; }
    get instrumentAlias() { return "APGA"; }
    onInteractionEvent(_args) {
        if (this.isElectricityAvailable()) {
            switch (_args[0]) {
                case "APGA_AP":
                    postHEvent("PMS50_APGA_AP_AP");
                    break;
                case "APGA_FD":
                    postHEvent("PMS50_APGA_AP_FD");
                    break;
                case "APGA_YD":
                    postHEvent("PMS50_APGA_AP_YD");
                    break;
                case "APGA_HDG":
                    postHEvent("PMS50_APGA_AP_HDG");
                    break;
                case "APGA_NAV":
                    postHEvent("PMS50_APGA_AP_NAV");
                    break;
                case "APGA_APR":
                    postHEvent("PMS50_APGA_AP_APR");
                    break;
                case "APGA_REV":
                    postHEvent("PMS50_APGA_AP_REV");
                    break;
                case "APGA_ALT":
                    postHEvent("PMS50_APGA_AP_ALT");
                    break;
                case "APGA_VS":
                    postHEvent("PMS50_APGA_AP_VS");
                    break;
                case "APGA_FLC":
                    postHEvent("PMS50_APGA_AP_FLC");
                    break;
                case "APGA_CGA":
                    postHEvent("PMS50_APGA_AP_CGA");
                    break;
                case "APGA_UP":
                    postHEvent("PMS50_APGA_AP_UP");
                    break;
                case "APGA_DOWN":
                    postHEvent("PMS50_APGA_AP_DOWN");
                    break;
                case "APGA_ARM":
                    postHEvent("PMS50_APGA_AP_ARM");
                    break;
                case "APGA_SEL_ALT_SMALL_INC":
                    postHEvent("PMS50_APGA_SEL_ALT_SMALL_INC");
                    break;
                case "APGA_SEL_ALT_SMALL_DEC":
                    postHEvent("PMS50_APGA_SEL_ALT_SMALL_DEC");
                    break;
                case "APGA_SEL_ALT_LARGE_INC":
                    postHEvent("PMS50_APGA_SEL_ALT_LARGE_INC");
                    break;
                case "APGA_SEL_ALT_LARGE_DEC":
                    postHEvent("PMS50_APGA_SEL_ALT_LARGE_DEC");
                    break;
                case "APGA_SEL_VS_SMALL_INC":
                    postHEvent("PMS50_APGA_SEL_VS_SMALL_INC");
                    break;
                case "APGA_SEL_VS_SMALL_DEC":
                    postHEvent("PMS50_APGA_SEL_VS_SMALL_DEC");
                    break;
                case "APGA_SEL_VS_LARGE_INC":
                    postHEvent("PMS50_APGA_SEL_VS_LARGE_INC");
                    break;
                case "APGA_SEL_VS_LARGE_DEC":
                    postHEvent("PMS50_APGA_SEL_VS_LARGE_DEC");
                    break;
            }
        }
    }
}
registerInstrument("kmc321-element", KMC321);
