/**
 * MVC Nmea Filter Page Controller.
 * 
 * Allows user to setup a filter for incoming and outgoing
 * NMEA connections.
 * 
 * @category Webserver
 * @package ATProcessor
 * @author Cameron Guest <cameron@aandtinstruments.com>
 * @version 0.2
 */

class NmeaFilterController extends BasicController
{

    constructor(model)
    {
        super(model);
    }

    /**
     * Event handler for baud rate select change.
     */
    UpdateBaud(e)
    {
        let section = $(e.target).attr('data-section');
        let baud = $(e.target).val();
        this.model.baudRates[section] = baud;
    }

    /**
     * Event handler for TxRx checkbox trigger.
     */
    UpdateFilter(e)
    {
        let sentence = $(e.target).attr('value');
        let channel = parseInt($(e.target).attr('data-channelno'));
        let isEnabled = $(e.target).is(':checked');
        let isRx = $(e.target).attr('name') === 'Receive' ? true : false;

        if(typeof(channel) !== 'number')
            return;
        
        if(isEnabled)
        {
            // Add to filter
            if(isRx)
            {
                // Rx
                this.model.NMEARxFilter[channel].add(sentence);

                if (this.model.NMEARxFilter[channel].has("HDG") + 
                    this.model.NMEARxFilter[channel].has("HDM") +
                    this.model.NMEARxFilter[channel].has("HDT") > 1)
                {
                    // Warn that multiple Heading inputs are selected
                    $('#dialog-text').text('Multiple heading inputs (HDG/HDM/HDT) selected on a single source may interfere with eachother. \
                        It is recommended to select only the input you intend to use.');
                    $('#dialog').dialog({
                        title: 'Warning: Mutliple Heading'
                    }).dialog("open");
                }
            }
            else
            {
                // Tx
                this.model.NMEATxFilter[channel].add(sentence);
            }
        }
        else
        {
            // Remove from filter
            if(isRx)
            {
                // Rx
                this.model.NMEARxFilter[channel].delete(sentence);
            }
            else
            {
                // Tx     
                this.model.NMEATxFilter[channel].delete(sentence);
            }
        }
    }
}

