/**
 * Class definitions to control current.php webpage.
 * 
 * @category Webserver
 * @package ATProcessor
 * @author Cameron Guest <cameron@aandtinstruments>
 * @version 0.1
 */

 /**
 * Defines current configuration 
 */
class CurrentModel extends ObservableModel {
    constructor() {
        super();

        this._SOGWarningFlag = false;
        this.category = "SpeedLog";
        this.data = {
            "logCal" : {
                    "currentCalcTime" : 1800
            } 
        };
        this.init();
    }

    get currentCalcTime() {
        return this.data.logCal.currentCalcTime;
    }
    set currentCalcTime(time) {
        this.data.logCal.currentCalcTime = time; 
    }

    init() {
        const self = this;
        let async = [];

        async.push(readSingle("logCal", "CurrentCalcTime").done( function(seconds){
            self.currentCalcTime = seconds;
        } ));

        async.push(readSingle("SpeedInput", "UseGPS").done(function(isEnabled) {
            self._SOGWarningFlag = isEnabled;
        }));

        $.when.apply($, async).then(function() {
            self.notifyAll();
        });
    }
}

/**
 * View -> Model interaction for Current page
 */
class CurrentController extends BasicController {
    constructor(model) {
        super(model);
    }

    // Convert hour, minute and second values into a single seconds value.
    toSeconds(hours, minutes, seconds) {
        let result = parseInt(hours) * 3600;
        result += parseInt(minutes) * 60;
        result += parseInt(seconds);
        return result;
    }

    // Validate user input then update model with current calc time in seconds
    changeCalcTime() {
        validateRange($("#calc-hours"), 0, 0, 2, 0);
        validateRange($("#calc-minutes"), 0, 0, 59, 30);
        validateRange($("#calc-seconds"), 0, 0, 59, 0);

        let calcTime = 
            this.toSeconds($("#calc-hours").val(), $("#calc-minutes").val(), $("#calc-seconds").val());

        this.model.currentCalcTime = calcTime;
    }
}

/**
 * Handle display for Current page
 */
class CurrentView extends BasicView {
    constructor(controller){
        super(controller);

        this.fieldDaemon.callback = this.errorCallback.bind(this);
    }

    bindEvents(){
        super.bindEvents();

        $("#calc-hours, #calc-minutes, #calc-seconds").on("change", this.controller.changeCalcTime.bind(this.controller));
    }

    // Display current calculation errors if they exist
    errorCallback($container, valueReference) {
        if (valueReference.valueString.toLowerCase() === "hidden"
            || valueReference.valueString === "") {
            $container.hide();
        } else {
            $container.show();
        }
    }

    // Convert a seconds value into an object containing hours, minutes and seconds.
    toHoursMinsSeconds(numSeconds){
        let numHours = Math.floor(numSeconds/3600);
        numSeconds = numSeconds % 3600;

        let numMins = Math.floor(numSeconds/60);
        numSeconds = numSeconds % 60;

        return { hours: numHours, minutes : numMins, seconds : numSeconds };
    }

    update(model) {
        let time = this.toHoursMinsSeconds(model.currentCalcTime);
        $("#calc-hours").val(time.hours);
        $("#calc-minutes").val(time.minutes);
        $("#calc-seconds").val(time.seconds);

        if(model._SOGWarningFlag){
            $("#sog-warning").show();
        }
    }
}
