import dayjs, { Dayjs } from 'dayjs';
import { FillTarget } from 'chart.js';
import { hexToRgba } from '@utils/functions';
import { ILicenseReport } from '@red/domain';
import { ComplexFillTarget } from 'chart.js/dist/types';

const monthNames = dayjs.monthsShort();

export const verboseDate = (date: Dayjs) => `${monthNames[date.month()]} ${date.year()}`;

export const getLineConfig: (
    color: string,
    displayLine: boolean,
    target: FillTarget,
) => {
    type: 'line';
    borderColor: string;
    backgroundColor: string;
    pointBackgroundColor: string;
    fill: ComplexFillTarget;
} = (color, displayLine, target) => ({
    type: 'line',
    borderColor: hexToRgba(color, displayLine ? 1 : 0),
    backgroundColor: hexToRgba(color, 1),
    pointBackgroundColor: hexToRgba(color, 1),
    fill: {
        target: target ?? '-1',
        above: hexToRgba(color, 0.3),
        below: hexToRgba(color, 0.3),
    },
    pointStyle: false,
});

export const getLabelsFromLicense = (license: ILicenseReport) => {
    let startMonth = dayjs(license.startDate).startOf('month');
    const endMonth = dayjs(license.endDate).endOf('month');

    const result = [];

    while (startMonth.isBefore(endMonth)) {
        result.push(verboseDate(startMonth));
        startMonth = startMonth.add(1, 'month');
    }

    return result;
};
