<script setup>
import axios from 'axios';
import moment from 'moment';
import { Chart, TimeScale, LinearScale, LineController, Tooltip, Legend, PointElement, LineElement } from 'chart.js';
import 'chartjs-adapter-moment';
import { onMounted, useTemplateRef, watch } from 'vue';
import { state, selectedNodeLatestPowerMetric } from '../../store.js';
import { powerMetricsTimeRange } from '../../config.js';
import { getTimeSpans } from '../../utils.js';
const powerMetricsChartEl = useTemplateRef('power-metrics-chart');

function initChart() {
    // destroy existing chart
    const existingChart = Chart.getChart(powerMetricsChartEl.value);
    if (existingChart != null) {
        existingChart.destroy();
    }
    // create chart data
    const labels = [];
    const channel1VoltageReadings = [];
    const channel2VoltageReadings = [];
    const channel3VoltageReadings = [];
    const channel1CurrentReadings = [];
    const channel2CurrentReadings = [];
    const channel3CurrentReadings = [];
    for(const powerMetric of state.selectedNodePowerMetrics) {
        labels.push(moment(powerMetric.created_at));
        channel1VoltageReadings.push(powerMetric.ch1_voltage);
        channel2VoltageReadings.push(powerMetric.ch2_voltage);
        channel3VoltageReadings.push(powerMetric.ch3_voltage);
        channel1CurrentReadings.push(powerMetric.ch1_current);
        channel2CurrentReadings.push(powerMetric.ch2_current);
        channel3CurrentReadings.push(powerMetric.ch3_current);
    }
    // create chart
    new Chart(powerMetricsChartEl.value, {
        type: 'line',
        data: {
            labels: labels,
            datasets: [
                {
                    label: 'Ch1 Voltage',
                    suffix: "V",
                    borderColor: '#3b82f6',
                    backgroundColor: '#3b82f6',
                    pointStyle: false, // no points
                    fill: false,
                    data: channel1VoltageReadings,
                    yAxisID: 'y',
                },
                {
                    label: 'Ch2 Voltage',
                    suffix: "V",
                    borderColor: '#22c55e',
                    backgroundColor: '#22c55e',
                    pointStyle: false, // no points
                    fill: false,
                    data: channel2VoltageReadings,
                    yAxisID: 'y',
                },
                {
                    label: 'Ch3 Voltage',
                    suffix: "V",
                    borderColor: '#f97316',
                    backgroundColor: '#f97316',
                    pointStyle: false, // no points
                    fill: false,
                    data: channel3VoltageReadings,
                    yAxisID: 'y',
                },
                {
                    label: 'Ch1 Current',
                    suffix: "mA",
                    borderColor: '#93c5fd',
                    backgroundColor: '#93c5fd',
                    pointStyle: false, // no points
                    fill: false,
                    data: channel1CurrentReadings,
                    yAxisID: 'y1',
                },
                {
                    label: 'Ch2 Current',
                    suffix: "mA",
                    borderColor: '#86efac',
                    backgroundColor: '#86efac',
                    pointStyle: false, // no points
                    fill: false,
                    data: channel2CurrentReadings,
                    yAxisID: 'y1',
                },
                {
                    label: 'Ch3 Current',
                    suffix: "mA",
                    borderColor: '#fdba74',
                    backgroundColor: '#fdba74',
                    pointStyle: false, // no points
                    fill: false,
                    data: channel3CurrentReadings,
                    yAxisID: 'y1',
                },
            ],
        },
        options: {
            responsive: true,
            borderWidth: 2,
            spanGaps: 1000 * 60 * 60 * 3, // only show lines between metrics with a 3 hour or less gap
            elements: {
                point: {
                    radius: 2,
                },
            },
            scales: {
                x: {
                    position: 'top',
                    type: 'time',
                    time: {
                        unit: 'day',
                        displayFormats: {
                            day: 'MMM DD', // Jan 01
                        },
                    },
                },
                y: {
                    min: 0,
                    max: 30,
                    ticks: {
                        callback: (label) => `${label}V`,
                    },
                },
                y1: {
                    min: -500,
                    max: 500,
                    ticks: {
                        stepSize: 50,
                        callback: (label) => `${label}mA`,
                    },
                    position: 'right',
                    grid: {
                        drawOnChartArea: false, // only want the grid lines for one axis to show up
                    },
                },
            },
            plugins: {
                legend: {
                    display: false,
                },
                tooltip: {
                    mode: "index",
                    intersect: false,
                    callbacks: {
                        label: (item) => {
                            return `${item.dataset.label}: ${item.formattedValue}${item.dataset.suffix}`;
                        },
                    },
                },
            },
        }
    });
}
watch(
  () => state.selectedNodePowerMetrics,
  (newValue) => {
    if (newValue !== []) {
        initChart()
    }
  }, {deep: true}
)
onMounted(() => {
    Chart.register(TimeScale, LinearScale, LineController, Tooltip, Legend, PointElement, LineElement)
})
</script>
<template>
    <!-- power metrics -->
    <div>
        <div class="flex bg-gray-200 p-2 font-semibold">
            <div class="my-auto">Power Metrics</div>
            <div class="my-auto ml-auto">
                <select v-model="powerMetricsTimeRange" class="block w-full rounded-md border-0 py-0.5 pl-2 pr-8 text-gray-900 ring-1 ring-inset ring-gray-300 focus:ring-2 focus:ring-blue-500 sm:text-sm sm:leading-6">
                    <option v-for="(range, index) in getTimeSpans()" :value="index">{{ range.name }}</option>
                </select>
            </div>
        </div>
        <ul role="list" class="flex-1 divide-y divide-gray-200">

            <!-- power metrics chart -->
            <li>
                <div class="px-4 py-2">
                    <div class="w-full">
                        <canvas id="powerMetricsChart" style="height:150px;" ref="power-metrics-chart"></canvas>
                        <div class="flex">
                            <div class="mx-auto flex space-x-2">
                                <div class="flex mx-auto">
                                    <div class="my-auto w-2 h-2 bg-blue-500 rounded-full"></div>
                                    <div class="my-auto ml-1 text-sm text-gray-500">Channel 1</div>
                                </div>
                                <div class="flex mx-auto">
                                    <div class="my-auto w-2 h-2 bg-green-500 rounded-full"></div>
                                    <div class="my-auto ml-1 text-sm text-gray-500">Channel 2</div>
                                </div>
                                <div class="flex mx-auto">
                                    <div class="my-auto w-2 h-2 bg-orange-500 rounded-full"></div>
                                    <div class="my-auto ml-1 text-sm text-gray-500">Channel 3</div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </li>

            <!-- channel 1 -->
            <li class="flex p-3">
                <div class="text-sm font-medium text-gray-900">Channel 1</div>
                <div class="ml-auto text-sm text-gray-700">
                    <span v-if="selectedNodeLatestPowerMetric">
                        <span v-if="selectedNodeLatestPowerMetric?.ch1_voltage">{{ Number(selectedNodeLatestPowerMetric.ch1_voltage).toFixed(2) }}V</span>
                        <span v-else>???</span>
                        <span v-if="selectedNodeLatestPowerMetric?.ch1_current"> / {{ Number(selectedNodeLatestPowerMetric.ch1_current).toFixed(2) }}mA</span>
                    </span>
                    <span v-else>???</span>
                </div>
            </li>

            <!-- channel 2 -->
            <li class="flex p-3">
                <div class="text-sm font-medium text-gray-900">Channel 2</div>
                <div class="ml-auto text-sm text-gray-700">
                    <span v-if="selectedNodeLatestPowerMetric">
                        <span v-if="selectedNodeLatestPowerMetric?.ch2_voltage">{{ Number(selectedNodeLatestPowerMetric.ch2_voltage).toFixed(2) }}V</span>
                        <span v-else>???</span>
                        <span v-if="selectedNodeLatestPowerMetric?.ch2_current"> / {{ Number(selectedNodeLatestPowerMetric.ch2_current).toFixed(2) }}mA</span>
                    </span>
                    <span v-else>???</span>
                </div>
            </li>

            <!-- channel 3 -->
            <li class="flex p-3">
                <div class="text-sm font-medium text-gray-900">Channel 3</div>
                <div class="ml-auto text-sm text-gray-700">
                    <span v-if="selectedNodeLatestPowerMetric">
                        <span v-if="selectedNodeLatestPowerMetric?.ch3_voltage">{{ Number(selectedNodeLatestPowerMetric.ch3_voltage).toFixed(2) }}V</span>
                        <span v-else>???</span>
                        <span v-if="selectedNodeLatestPowerMetric?.ch3_current"> / {{ Number(selectedNodeLatestPowerMetric.ch3_current).toFixed(2) }}mA</span>
                    </span>
                    <span v-else>???</span>
                </div>
            </li>

        </ul>
    </div>
</template>
