/**
 * View -> Modal communication for sensor inputs.
 * 
 * @category Webserver
 * @package ATProcessor
 * @author Sam Meredith <sam@aandtinstruments.com>
 * @version 0.1
 * 
 */

 /**
  * Common functionality
  */
class SensorController extends BasicController {
    constructor(model) {
        super(model);
    }

    // Redirect to CANbus configuration - persist local changes
    canbusRedirect() {
        window.location.href = "canFilter.php?previous=" + window.location.pathname
            + "&src=" + this.model.sensorNumber + "&parameters="
            + JSON.stringify(this.model.sensorCalibrationData);
    }

    // Change (NMEA0183) instance
    changeInstance(e) {
        this.model.sensorInstance = $(e.target).val();
        this.model.notifyAll();
    }

    // Update sensor name
    changeName(e) {
        this.model.sensorName = $(e.target).val();
    }

    // Change input source
    changeSource(e) {
        this.model.sensorSource = $(e.target).val();
        this.model.notifyAll();
    }    

    // Redirect to heel correction page - persist local changes
    correctionRedirect() {
        window.location.href = "heelCorrections.php?previous=" + window.location.pathname
            + "&src=" + this.model.sensorNumber + "&parameters=" 
            + JSON.stringify(this.model.sensorCalibrationData);
    }

    // Redirect to NMEA0183 configuration - persist local changes
    nmeaRedirect() {
        window.location.href = "nmeaFilter.php?previous=" + window.location.pathname
            + window.location.search + "&parameters=" 
            + JSON.stringify(this.model.sensorCalibrationData);
    }

    changeCanbusDevice(e) {
        this.model.uniqueid = $(e.target).val();
        this.model.notifyAll();
    }
}

/**
 * Speed sensor-specific functionaltiy
 */
class SpeedSensorController extends SensorController {
    constructor(model) {
        super(model);
    }

    // Calibration factor value has been changed
    changeCalibrationFactor(e) {
        validateRange( $(e.target), 2, 0.01, 5, 1 );
        this.model.calibrationFactor = $(e.target).val();
    }

    // Computation estimator value has been changed
    changeComputationEstimator(e) {
        validateRange( $(e.target), 1, 0.1, 60, 2);
        this.model.computationEstimator = $(e.target).val();
    }

    // Pulse calibration value has been changed
    changePulseCalibration(e) {
        validateRange( $(e.target), 2, 0.5, 100, 0.5 );
        this.model.pulseCalibration = $(e.target).val();
    }

    // Use SOG as boat speed has been toggled
    toggleSoG(e) {
        this.model.useSoG = $(e.target).is(':checked');
        this.model.notifyAll();
    }
}

/**
 * Depth sensor-specific functionality
 */
class DepthSensorController extends SensorController {
    constructor(model) {
        super(model); 
    }

    // Datum value has been changed.
    changeDatumValue(e) {
        validateRange($(e.target), 1, -32.7, 32.7, 0);
        this.model.datum = $(e.target).val();
    }

    // Minimum depth value has been changed.
    changeMinDepth(e) {
        if (this.model.sensorSource.toLowerCase().includes('board')) {
            validateRange( $(e.target), 1, 1, 10, 0);
            this.model.activeSource.minDepth = $(e.target).val();
        }
    }
}

/**
 * Wind sensor-specific functionality
 */
class WindSensorController extends SensorController {
    constructor(model) {
        super(model);
    }  

    // Multiplicative calibration factor has been changed
    changeCalibrationFactor(e) {
        validateRange( $(e.target), 2, 0.01, 5, 1 );
        this.model.calibrationFactor = $(e.target).val();
    }

    // Computation window has been changed
    changeComputationEstimator(e) {
        validateRange( $(e.target), 1, 0.1, 60, 5);
        this.model.computationEstimator = $(e.target).val();
    }

    // Mast rotation source has been changed
    changeMastRotationSource(e) {
        this.model.mastRotationSource = $(e.target).val();
        this.model.notifyAll();
    }

    // MHU offset has been changed
    changeMHUOffset(e) {
        validateRange( $(e.target), 1, -180, 180, 0.0 );
        this.model.mhuOffset = $(e.target).val();
    }

    // Raw pulse -> pulse frequency calibration value has been changed
    changePulseCalibration(e) {
        validateRange( $(e.target), 2, 0.5, 100, 0.98 );
        this.model.pulseCalibration = $(e.target).val();
    }

    // Mast motion sensor height has been changed
    changeSensorHeight(e) {
        validateRange( $(e.target), 1, 0, 100, 45 );
        this.model.mastHeight = $(e.target).val();
    }

    // Wind speed offset has been changed
    changeSpeedOffset(e) {
        validateRange( $(e.target), 2, -10, 10, 0.76 );
        this.model.speedOffset = $(e.target).val();
    }

    changeWindShear(e) {
        validateRange( $(e.target), 1, -180, 180, 0.0 );
        this.model.windShear = $(e.target).val();
    }

    // Redirect to mast motion page - persist local changes
    mastMotionRedirect(e) {
        window.open("/mastMotion.php?previous=" + window.location.pathname
            + "&src=" + this.model.sensorNumber + "&parameters=" 
            + JSON.stringify(this.model.sensorCalibrationData), '_blank').focus();
    }

    // Heel speed correction checkbox
    toggleHeelSpeedCorrection(e) {
        this.model.heelSpeedCorrection = $(e.target).is(':checked');
    }

    // Heel angle correction checkbox
    toggleHeelAngleCorrection(e) {
        this.model.heelAngleCorrection = $(e.target).is(':checked');
    }

    // Mast motion enable checkbox
    toggleMastMotionAngleCorrection(e)
    {
        this.model.mastMotionAWAEnabled = $(e.target).is(':checked');
    }
    toggleMastMotionSpeedCorrection(e) 
    {
        this.model.mastMotionAWSEnabled = $(e.target).is(':checked');
    }

    // Mast rotation enable checkbox
    toggleMastRotation(e) {
        this.model.mastRotation = $(e.target).is(':checked');
        this.model.notifyAll();
    }
}