/**
 * @category    Webserver
 * @package     ATPX
 * @author      Sam Meredith <sam@aandtinstruments.com>
 * @version     0.1
 */

const chartOptionsGyro =
{
    type: 'scatter',
    data:
    {
        datasets: 
        [{
            label: 'Heel Gyro',
            data: [],
            showLine: true,
            borderColor: 'rgb(255, 65, 54)',
            parsing: false,
            normalised: true
        },
        {
            label: 'Pitch Gyro',
            data: [],
            showLine: true,
            borderColor: 'rgb(0, 116, 217)',
            parsing: false,
            normalised: true
        },
        {
            label: 'Heel',
            data: [],
            showLine: true,
            borderColor: 'rgb(46, 204, 64)',
            parsing: false,
            normalised: true
        },
        {
            label: 'Pitch',
            data: [],
            showLine: true,
            borderColor: 'rgb(255, 215, 0)',
            parsing: false,
            normalised: true
        }]
    },
    options:
    {
        animation: false,
        datasets:
        {
            scatter:
            {
                pointRadius: 0
            }
        },
        maintainAspectRatio: false,
        parsing: false,
        plugins:
        {
            decimation:
            {
                enabled: true,
                algorithm: 'min-max'
            },
            legend:
            {
                display: true,
                position: 'bottom',
                labels:
                {
                    boxHeight: 1,
                    boxWidth: 20,
                    padding: 20
                },
            },
            title:
            { 
                display: true,
                font:
                {
                    size: 18
                },
                position: 'top',
                text: 'Heel/Pitch + Gyros'
            },
            tooltip:
            {
                enabled: false
            }
        },
        scales:
        {
            x:
            {
                type: 'realtime',
                realtime:
                {
                    duration: 60000,
                    delay: 300,
                    pause: false,
                    // Duration of data to be kept
                    // Undefined causes data to be deleted as soon as it falls off the chart
                    ttl: undefined,
                    // Frequency at which the chart is drawn
                    // Decreasing this has the biggest impact on CPU utilisation
                    frameRate: 10
                },
                ticks:
                {
                    display: false
                }
            },
            y:
            {
                suggestedMin: -10.0,
                suggestedMax: 10.0,
                title:
                {
                    display: true,
                    font:
                    {
                        size: 18
                    },
                    text: 'Angle/Rate of Change'
                }
            }
        },
        spanGaps: true
    }
}

const updateChartGyro = function(chart, sender, instance)
{
    const fields = new Set(['0x0766', '0x0767', '0x0710', '0x0722']);

    $.ajax(
    {
        type: 'POST',
        url: '/atp.fields.php',
        data: 
        {
            'action': 'getFields',
            'fields': Array.from(fields),
            'id': sender,
            'instance': instance
        },
        success: function(response) 
        {
            // Parse value and append to dataset
            const json = JSON.parse(response);
        
            if (json.status)
            {
                let responseData = [];

                // Strip HTML tags
                const match = json.response.match(/.*<BODY>(.*)<\/BODY>.*/s);
                if (match)
                {
                    match[1].split("\r\n").forEach(datum =>
                    {
                        const split = datum.split('=');
                        responseData.push({ 'field' : parseInt(split[0]), 'data' : parseFloat(split[1]) });
                    });
                }

                const heelGyroValue = (responseData.find(element => element.field == 0x0766) || { data: "NaN" }).data;

                chart.data.datasets[0].data.push(
                    {
                        x: Date.now(),
                        y: parseFloat(heelGyroValue)
                    }
                );

                const pitchGyroValue = (responseData.find(element => element.field == 0x0767) || { data: "NaN" }).data;

                chart.data.datasets[1].data.push(
                    {
                        x: Date.now(),
                        y: parseFloat(pitchGyroValue)
                    }
                );
                
                const heelValue = (responseData.find(element => element.field == 0x0710) || { data: "NaN" }).data;

                chart.data.datasets[2].data.push(
                    {
                        x: Date.now(),
                        y: parseFloat(heelValue)
                    }
                );

                const trimValue = (responseData.find(element => element.field == 0x0722) || { data: "NaN" }).data;

                chart.data.datasets[3].data.push(
                    {
                        x: Date.now(),
                        y: parseFloat(trimValue)
                    }
                );

                // Update chart
                chart.update('quiet');
            }
        }
    });
}
