class SpeedCorrectionView extends BasicView {
    constructor(controller) {
        super(controller);

        this.correctionContainer = $('#correction-container').clone();
        this.correctionTable = $('#heel-table').clone();
    }

    bindEvents() {
        super.bindEvents();

        $('#button-container').on('click', '#reset-button a', this.controller.resetCorrections.bind(this.controller));
        $('#correction-container').on('change', '#heel-table input', this.controller.changeCorrection.bind(this.controller));
    }

    drawCorrectionContainer(model) {
        $('#correction-container').html(this.correctionContainer.html());

        $('#correction-container').show()
    }

    drawCorrectionTable(model) {
        $('#heel-table').html(this.correctionTable.html());

        for (var i = 0; i < model.numberOfRows; i++) {
            var $row = $("<tr></tr>");
            $row.append("<th>" + model.getCorrectionAtIndex(i, -1) + "</th>");
            for (var j = 0; j < model.numberOfColumns; j++) {
                $row.append("<td><input type='number' step=0.1 value='" + model.getCorrectionAtIndex(i, j) + "'><span>%</span></td>");
            }
            $('#heel-table tbody').append($row);
        }
        $('#heel-table tr:eq(6) td:eq(1) input').attr('type', '').val('BASE').prop('disabled', true).addClass('correction-base');
        $('#heel-table').show();
        $('#reset-button').show();
    }

    drawDataFields(model) 
    {
        if (model.sensor.isPulseSource) 
        {
            $('#raw-input-heading').text('Pulse Frequency');
            $('#base-field').text(model.sensor.pulseCalibration + ' Hz');

            $('#raw-bspd').attr('data-field', '0x26' + (parseInt(model.sensor.sensorNumber) + 1) + '2');
            $('#raw-bspd').attr('data-sender', 'ATProcessor');
            $('#raw-bspd').attr('data-instance', '0');
        } 
        else 
        {
            $('#raw-input-heading').text('Source Speed');
            $('#base-field').text(model.sensor.calibrationFactor);

            $('#raw-bspd').attr('data-field', '0x0202');
            $('#raw-bspd').attr('data-sender', model.sensor.sensorSource);
            $('#raw-bspd').attr('data-instance', model.sensor.sensorInstance);
        }
        $('#field-container .field-container').not('#heel, #bspd, #raw-bspd').attr('data-sender', model.sensor.sensorSource);
        $('#field-container .field-container').not('#heel, #bspd, #raw-bspd').attr('data-instance', model.sensor.sensorInstance);
        $('#bspd').attr('data-field', '0x26' + (parseInt(model.sensor.sensorNumber) + 1) + '1');
    }

    drawPulseWarning(model) {
        $('#pulse-warning').show();
    }

    drawSoGWarning(model) {
        $('#sog-warning').show();
    }

    resetView() {
        $('#correction-container').empty();
        $('#heel-table').empty();
        $('#reset-button').hide();
        $('#pulse-warning').hide();
        $('#sog-warning').hide();
    }

    update(model) {
        this.resetView();

        if (model.sensor.sensorName != "") {
            $('.page-title h1').html('Advanced Speed Calibration - ' + model.sensor.sensorName);
        } else {
            $('.page-title h1').html('Advanced Speed Calibration - Speed Input ' + (parseInt(this.controller.model.sensorNumber) + 1));
        }
        if (model.widgets['display-correction-container']) {
            this.drawCorrectionContainer(model);
        }
        if (model.widgets['display-correction-table']) {
            this.drawCorrectionTable(model);
        }
        if (model.widgets['display-data-fields']) {
            this.drawDataFields(model);
        }
        if (model.widgets['display-sog-warning']) {
            this.drawSoGWarning(model);
        }
        if (model.widgets['display-pulse-warning']) {
            this.drawPulseWarning(model);
        }
    }
}
