import {ChartConfiguration, ChartPoint, TimeUnit} from "chart.js";

interface ClimateChartSettings  {
    humidity: ChartPoint[];
    temp: ChartPoint[];
    co2: ChartPoint[];
    colors?: {
        humidity?: string;
        temp?: string;
        co2?: string;
    }
}

const defaultHumidityColor = 'rgb(45,141,45)';
const defaultTempColor = 'rgb(0,134,222)';
const defaultCo2Color = 'rgb(194,30,30)';

export function generateClimateChartConfig(settings: ClimateChartSettings): ChartConfiguration {
    return {
        type: 'line',
        data: {
            datasets: [{
                label: 'Humidity',
                data: settings.humidity,
                borderColor: settings.colors?.humidity ?? defaultHumidityColor,
                fill: false,
                yAxisID: 'y-axis-3',
            }, {
                label: 'Temperature',
                data: settings.temp,
                borderColor: settings.colors?.temp ?? defaultTempColor,
                fill: false,
                yAxisID: 'y-axis-2',
            }, {
                label: 'Co2 Concentration',
                data: settings.co2,
                borderColor: settings.colors?.co2 ?? defaultCo2Color,
                fill: false,
                yAxisID: 'y-axis-1',
            }]
        },
        options: {
            title: {
            display: true,
                text: 'Ledda\'s Room Climate',
            },
            scales: {
                xAxes: [{
                    type: 'time',
                    time: {
                        unit: 'second' as TimeUnit
                    }
                }],
                    yAxes: [{
                    type: 'linear',
                    display: true,
                    position: 'right',
                    id: 'y-axis-1',
                    ticks: {
                        fontColor: settings.colors?.co2 ?? defaultCo2Color,
                        suggestedMin: 400,
                        suggestedMax: 1100,
                    },
                }, {
                    type: 'linear',
                    display: true,
                    position: 'left',
                    id: 'y-axis-2',
                    ticks: {
                        fontColor: settings.colors?.temp ?? defaultTempColor,
                        suggestedMin: 10,
                        suggestedMax: 35,
                    },
                    gridLines: {
                        drawOnChartArea: false,
                    },
                }, {
                    type: 'linear',
                    display: true,
                    position: 'left',
                    id: 'y-axis-3',
                    ticks: {
                        fontColor: settings.colors?.humidity ?? defaultHumidityColor,
                        suggestedMin: 15,
                        suggestedMax: 85,
                    },
                    gridLines: {
                        drawOnChartArea: false,
                    },
                }],
            }
        },
    }
}