var sensorSectionNames = {
    Speed: "HeelSpeedCorrection:",
    Wind: "Wind Correction Table:"
}

class CorrectionModel extends ObservableModel {
    constructor(sensorNumber, sensorType) {
        super();

        this.correctionData = new Object();
        this.increment = 5;
        this.sensors = new Array();
        this.sensorNumber = sensorNumber;
        this.sensorType = sensorType;
        this.widgets = new Object();

        this.init();
    }

    get sensor() {
        return this.sensors[this.sensorNumber];
    }

    getSensorCorrections(sensorNumber) {
        var deferred = new $.Deferred();
        var self = this;
        
        readSection(sensorSectionNames[self.sensorType] + sensorNumber).done(function(correctionSection) {
            self.correctionData = $.extend(true, self.correctionData, JSON.parse(correctionSection));
            deferred.resolve();
        });

        return deferred.promise();
    }

    init() {
        var async = new Array();
        var deferred = new $.Deferred();
        var sensorDeferred = new $.Deferred();
        var self = this;

        async.push(sensorDeferred.promise());
        readSingle("Settings", self.sensorType + "Inputs").done(function(numberOfInputs) {
            var sensorAsync = new Array();

            if (numberOfInputs == '') numberOfInputs = 1;

            for (var i = 0; i < numberOfInputs; i++) {
                var sensor = (self.sensorType == "Speed") ? new SpeedSensorModel(i) : new WindSensorModel(i);
                sensorAsync.push(sensor.init());
                self.sensors.push(sensor);
            }

            $.when.apply($, sensorAsync).then(function() {
                sensorDeferred.resolve();
            });
        });

        async.push(self.getSensorCorrections(self.sensorNumber));

        $.when.apply($, async).then(function() {
            self.setDrawableWidgets();
            self.notifyAll();
            deferred.resolve();
        });
            
        return deferred.promise();
    }

    persistData() {
        var databaseArray = new Array();
        var self = this;

        $.each(self.correctionData, function(key, value) {
            databaseArray.push(
                {'section': sensorSectionNames[self.sensorType] + self.sensorNumber, 'key': key, 'value': value}
            );
        });
        
        return writeToDatabase(databaseArray, self.category);
    }

    resetCorrections() {
        for (var i = 0; i < this.numberOfRows; i++) {
            for (var j = 0; j < this.numberOfColumns; j++) {
                this.setCorrectionAtIndex(i, j, 0);
            }
        }
    }

    setDrawableWidgets() {
        this.widgets['display-correction-container'] = true;
        this.widgets['display-correction-table'] = true;
        this.widgets['display-data-fields'] = true;
    }
}

class SpeedCorrectionModel extends CorrectionModel {
    constructor(sensorNumber) {
        super(sensorNumber, "Speed");

        this.category = "SpeedLog";
        this.numberOfColumns = 5;
        this.numberOfRows = 9;
    }

    getCorrectionAtIndex(row, column) {
        var correctionString = "";
        var self = this;

        //Sends -1 for the Heel Angle column
        if (column == -1) {
            correctionString += (row == 0) ? "&leq;" : (row == (self.numberOfRows - 1)) ? "&geq;" : "";
            var heelValue = self.correctionData["bsp.10"].split(',')[row * 2];
            correctionString += (heelValue > 0) ? heelValue + "&deg; S" : (heelValue < 0) ? heelValue + "&deg; P" : heelValue + "&deg;";
        } else {
            correctionString += self.correctionData["bsp." + (column + 1)*self.increment].split(',')[row*2 + 1];
        }

        return correctionString;
    }

    getSensorCorrections(sensorNumber) {
        this.correctionData = {
            "bsp.5": "-20,0.00,-15,0.00,-10,0.00,-5,0.00,0,0.00,5,0.00,10,0.00,15,0.00,20,0.00",
            "bsp.10": "-20,0.00,-15,0.00,-10,0.00,-5,0.00,0,0.00,5,0.00,10,0.00,15,0.00,20,0.00",
            "bsp.15": "-20,0.00,-15,0.00,-10,0.00,-5,0.00,0,0.00,5,0.00,10,0.00,15,0.00,20,0.00",
            "bsp.20": "-20,0.00,-15,0.00,-10,0.00,-5,0.00,0,0.00,5,0.00,10,0.00,15,0.00,20,0.00",
            "bsp.25": "-20,0.00,-15,0.00,-10,0.00,-5,0.00,0,0.00,5,0.00,10,0.00,15,0.00,20,0.00"
        }

        return super.getSensorCorrections(sensorNumber);
    }

    setCorrectionAtIndex(row, column, value) {
        var correctionParams = this.correctionData["bsp." + (column + 1)*this.increment].split(',');
        correctionParams[row*2 + 1] = value;
        this.correctionData["bsp." + (column + 1)*this.increment] = correctionParams.join(',');
    }

    setDrawableWidgets() {
        if (this.sensor.useSOG) {
            this.widgets['display-sog-warning'] = true;
        } else {
            super.setDrawableWidgets();
        }
    }
}
