"use strict";

import Luci from "../luci.js";
import Scenari from "../scenari.js";
import Antifurto from "../antifurto.js";
import ApiMqttOut from "./api-mqtt-out.js";

class Api {

    static isConnected = false;
    // /api/luci/
    /**
     * gets the luci of the user from the server
     * @returns {*} all the lights of the user
     */
    static getLuci = () => { console.log('ciao');
        ApiMqttOut.getStatusLuci();
    };
    static sendLuciWeb = luci => { console.log('ciao');
        Luci.luciFromMqtt(luci);
    }

    // static callbackGetAllLuci = null;
    static getAllLuci = () => { console.log('ciao');
        ApiMqttOut.getAllLuci();
    }
    static sendDataLuciWeb = luci => { console.log('ciao');
        Luci.luciFromMqtt(luci);
    }

    static luceMakeNewLuci = null;
    static callbackMakeNewLuci = null;
    /**
     * adds a new luce to the server
     * @param {luceTemplate} luce the luce to add to the server
     * @returns {*} null if the light was added
     */
    static makeNewLuci = luce => { console.log('ciao');
        if (luce == null)
            throw new Error("luce must be defined");
        const luceSent = {nome: luce.luogo, stato: luce.stato, in: luce["sensore-in"], out: luce["luce-out"]};
        Api.luceMakeNewLuci = luce;
        ApiMqttOut.makeNewLuci(luceSent);
    }
    static sendWebNewLuci = luce => { console.log('ciao');
        const oldLuce = Api.luceMakeNewLuci;
        const luceReceived = {luogo: luce.nome, stato: luce.stato, "sensore-in": luce.in, "luce-out": luce.out, id : oldLuce.id};
        if (oldLuce !== luce) {
            console.log('le luci non corrispondono', { oldLuce, luceReceived });
        }
        Luci.showCreatedLight(luce);
    }

    static sensoreMakeNewSensore = null;
    /**
     * invia il sensore al microservizio per aggiornarlo (YET TO BE USED)
     * @param {*} sensore l'oggetto sensore che dev'essere inviato al microservizio
     */
    static makeNewSensoreLuci = sensore => { console.log('ciao');
        if (sensore == null || sensore.nome == null)
            throw new Error("sensore must be defined");
        Api.sensoreMakeNewSensore = sensore;
        ApiMqttOut.makeNewSensoreLuci({sensM: sensore.nome});
    }
    static sendWebNewSensore = sensore => { console.log('ciao');
        const oldSensore = Api.sensoreMakeNewSensore;
        if(oldSensore.nome !== sensore.sensM) {
            console.log('i sensori non corrispondono', { oldSensore, sensore });
        }
        Luci.showCreatedSensore(sensore);
    }
    static callbackLuciStatoToBeStatic = null;
    static toggleForLuciStato = null;
    static fixedCallbackLuciStato = null;
    // /api/luci/stato/
    /**
     * changes the status of a luce on the server
     * @param {luceTemplate} luce the luce to change the status of
     * @returns {*} null if the light's status was changed
     */
    static setLuciStato = (luce, toggle, callback) => { console.log('ciao');
        if (Api.fixedCallbackLuciStato == null)
            return;
        if (luce == null)
            throw new Error("luce must be defined");
        if (toggle == null)
            throw new Error("toggle must be defined");
        if (callback == null)
            throw new Error("callback must be defined");

        if(Api.callbackLuciStatoToBeStatic == null)
            Api.callbackLuciStatoToBeStatic = callback;
        Api.toggleForLuciStato = toggle;
        ApiMqttOut.setLuciStato(luce);
    }
    static sendLuciStatoWeb = response => { console.log('ciao');
        if (response == null) {
            const toggle = Api.toggleForLuciStato;
            Api.toggleForLuciStato = null;
            Api.callbackLuciStatoToBeStatic(toggle);
        } else {
            Api.fixedCallbackLuciStato(response);
        }
    }
    // /api/scenari/
    /**
     * gets the scenari of the user from the server
     * @returns {*} all the scenarios of the user
     */
    static getScenari = () => { console.log('ciao');
        ApiMqttOut.getAllFromScenari();
    };
    static sendScenariWeb = scenari => { console.log('ciao');
        Scenari.mostraScenariServer(scenari);
    }

    static setAntifurtoINbtnFromScenari = newINbtn => { console.log('ciao');
    console.log(newINbtn)
        if(newINbtn == null || newINbtn?.nome == null)
            throw new Error("newINbtn must be defined");
        ApiMqttOut.setAntifurtoINbtnFromScenari(newINbtn.nome);
    }
    // /api/scenari/attiva/
    static scenarioSetScenarioStatus = null;
    static activatingSetScenarioStatus = null;
    /**
     * activates or deactivates a scenario on the server
     * @param {scenarioTemplate} scenario the scenario to activate or deactivate
     * @returns {*} null if the scenario was activated or deactivated
     */
    static setScenarioStatus = (scenario, activating) => { console.log('ciao');
        if (scenario == null)
            throw new Error("scenario must be defined");
        if (activating == null)
            throw new Error("activating must be defined");
        Api.scenarioSetScenarioStatus = scenario;
        Api.activatingSetScenarioStatus = activating;
        ApiMqttOut.setScenarioStatus(scenario);
    };
    static sendWebNewScenarioStatus = scenarioNome => { console.log('ciao');
        const scenario = Api.scenarioSetScenarioStatus;
        const activating = Api.activatingSetScenarioStatus;
        Api.scenarioSetScenarioStatus = null;
        Api.activatingSetScenarioStatus = null;
        if(scenario == null || activating == null)
            return;
        // FIXME forse dovrei controllare scenarioNome
        console.log(scenario)
        if (scenarioNome.startsWith(scenario.nome))
            Scenari.showScenarioAfterToggling(scenario, activating,true);
    }

    static sendWebScenarioAttivoAutomatico = scenarioNome => { console.log('ciao');
        Scenari.showScenarioAfterToggling(scenarioNome, true,true);
    }
    // /api/scenari/registra/
    static recordingRecordScenario = null;
    static learnINBtnRecordScenario = null;
    /**
     * starts/stop the recording of a scenario
     * @param {Boolean} recording true to start the recording, false to stop it
     * @returns {*} null if the recording was started or stopped
     */
    static recordScenario = (recording, learnINBtn) => { console.log('ciao');
        if (recording == null)
            throw new Error("recording must be defined");
        if (learnINBtn == null)
            throw new Error("learnINBtn must be defined");
        Api.recordingRecordScenario = recording;//true se voglio registrare
        Api.learnINBtnRecordScenario = learnINBtn;
        console.log(learnINBtn)
        ApiMqttOut.recordScenario(learnINBtn.nome);
    }
    static retriesRecordScenario = 0;
    static sendWebScenarioAutoma = stato => { console.log('ciao');
        if(isNaN(stato))
            return;
        const recording = Api.recordingRecordScenario;
        // const statoAspettato = recording ? 1 : 0;
        if (recording != null && stato !== recording && Api.retriesRecordScenario < 3) {
            Api.retriesRecordScenario++;
            ApiMqttOut.recordScenario(Api.learnINBtnRecordScenario);
            return;
        }
        Api.recordingRecordScenario = null;
        Api.learnINBtnRecordScenario = null;
        Api.retriesRecordScenario = 0;
        Scenari.apiCallbackRecordScenario(recording);
    }
    // /api/scenari/salva/
    /**
     * tells the server to save the currently recorded scenario
     * @returns {*} the scenario if it was saved
     */
    static saveScenario = nome => { console.log('ciao');
        if (nome == null)
            throw new Error("nome must be defined");
        ApiMqttOut.saveScenario(nome);
    };
    static sendWebSavedScenario = scenario => { console.log('ciao');
        Scenari.saveScenarioCallback(scenario);
    }
    // /api/antifurto/
    /**
     * gets everything about the antifurto (stato, allarme, attenzione (valore progress bar), soglia, sensori)
     * @returns {*} the antifurto's values
     */
    static getAntifurto = () => { console.log('ciao');
        ApiMqttOut.getAntifurto();
    };
    static sendWebGetAntifurto = antifurto => { console.log('ciao');
        Antifurto.mostraAntifurtoServer(antifurto);
    }
    // /api/antifurto/stato/
    static payloadSetAntifurtoStatus = null;
    /**
     * sets the status of the antifurto
     * @param {Boolean} payload {previousStatus, fromScenari, fromServer}
     * @returns {*} null if the status was changed
     */
    static setAntifurtoStatus = payload => { console.log('ciao');
        if (payload == null)
            throw new Error("payload must be defined");
        // Api.newStatusSetAntifurtoStatus = newStatus;
        // Api.antifurtoINbtnSetAntifurtoStatus = antifurtoINbtn;
        Api.payloadSetAntifurtoStatus = payload;
        console.log(Antifurto.attivaAntifurtoINbtn)
        const nomeIN = Antifurto.attivaAntifurtoINbtn != null ? Antifurto.attivaAntifurtoINbtn.nome : Scenari.antifurtoINbtn.nome;
        ApiMqttOut.setAntifurtoStatus(nomeIN);
    };
    static retriesSetAntifurtoStatus = 0;
    static sendWebSetAntifurtoStatus = stato => { console.log('ciao');
        if(stato === NaN)
            return;
        const newStatus = Antifurto?.status != null ? !Antifurto.status : !Scenari.antifurtoStatus;
        const expectedStatus = newStatus ? 2 : 0;
        console.log(Antifurto.attivaAntifurtoINbtn)
        const nomeIN = Antifurto.attivaAntifurtoINbtn != null ? Antifurto.attivaAntifurtoINbtn.nome : Scenari.antifurtoINbtn.nome;
        if (stato !== expectedStatus && Api.retriesSetAntifurtoStatus < 3) {
            Api.retriesSetAntifurtoStatus++;
            ApiMqttOut.setAntifurtoStatus(nomeIN);
            return;
        }
        const payload = Api.payloadSetAntifurtoStatus;
        // Api.newStatusSetAntifurtoStatus = null;
        // Api.antifurtoINbtnSetAntifurtoStatus = null;
        Api.payloadSetAntifurtoStatus = null;
        Api.retriesSetAntifurtoStatus = 0;
        try{
            Antifurto.showChangeAntifurtoStatus(payload?.previousStatus ? payload?.previousStatus : Antifurto.status, newStatus, payload?.fromScenari ? payload?.fromScenari : false, payload?.fromServer ? payload?.fromServer : true);
        } catch (error) {
            if(error.message !== "Antifurto.showChangeAntifurtoStatus is not a function")
                throw error;
            else Scenari.showChangeAntifurtoStatus(newStatus, payload?.fromServer ? payload?.fromServer : true);
        }
    }
    // /api/antifurto/allarme/
    static fixedCallbackStatoAllarme = null;
    /**
     * sets the status of the antifurto's alarm
     * @param {Boolean} allarme the new status of the antifurto's alarm
     * @returns {*} null if the status was changed
     */
    static setAntifurtoAllarme = allarme => { console.log('ciao');
        // const booleanAlarm = allarme == 'true';
        if (allarme == null || Api.fixedCallbackStatoAllarme == null)
            return;
        Api.fixedCallbackStatoAllarme(allarme);
    };
    // /api/antifurto/attenzione/
    static fixedCallbackValoreAttenzione = null;
    /**
     * sets the status for the progress bar of the antifurto's alarm
     * @param {Number} attenzione the new value of the antifurto's attention
     * @returns {*} null if the value was changed
     */
    static setAntifurtoAttenzione = attenzione => { console.log('ciao');
        if (attenzione == null || isNaN(parseInt(attenzione)) || Api.fixedCallbackValoreAttenzione == null)
            return;
        const val = parseInt(attenzione);
        Api.fixedCallbackValoreAttenzione(val);
    };
    // /api/antifurto/soglia/
    static sogliaSetAntifurtoSoglia = null;
    static fromServerSetAntifurtoSoglia = null;
    static callbackSetAntifurtoSoglia = null;
    /**
     * sets the value of the antifurto's threshold
     * @param {Number} soglia the user's value of the antifurto's threshold
     * @returns {*} null if the value was changed
     */
    static setAntifurtoSoglia = (soglia, fromServer) => { console.log('ciao');
        if (soglia == null || isNaN(parseInt(soglia)))
            throw new Error("soglia must be a set integer");
        const val = parseInt(soglia);
        if (val < 0 || val > 100)
            throw new Error("soglia must be between 0 and 100");
        if (fromServer == null)
            throw new Error("fromServer must be defined");
        Api.sogliaSetAntifurtoSoglia = val;
        Api.fromServerSetAntifurtoSoglia = fromServer;
        ApiMqttOut.setAntifurtoSoglia(val);
    };
    static sendWebSoglia = soglia => { console.log('ciao');
        const expectedSoglia = Api.sogliaSetAntifurtoSoglia;
        if (expectedSoglia !== soglia) {
            console.log('soglie differenti', { expectedSoglia, soglia });
        }
        const fromServer = Api.fromServerSetAntifurtoSoglia;
        Api.sogliaSetAntifurtoSoglia = null;
        Api.fromServerSetAntifurtoSoglia = null;
        Antifurto.showEditsSoglia(soglia, fromServer);
    }
}

export default Api;