import {Series, SeriesStyle} from "./ChartWidget";

export class ChartData implements ChartData {
    labels: string[] = [];
    datasets: ChartDataSet[] = [];
    backgroundColors: BackgroundColor[]= [];

    constructor(labels: string[], datasets: ChartDataSet[], colors: BackgroundColor[]) {
        this.labels = labels;
        this.datasets = datasets;
        this.backgroundColors = colors;
    }

    public static fromObjectsList(objectsList: any, series: Series): ChartDataLocal {
        const chartDataSets: ChartDataSet[] = [];
        const mapDataSets: any = {};
        const colors: BackgroundColor[]= [];
        	
        for (const column of series.selectedColumns)
            mapDataSets[column] = [];

        const labels: string[] = [];
        const type: string[] = [];

        for (const row of objectsList.result) {
            for (const column of series.selectedColumns) {
                mapDataSets[column].push(row[column]);
            }
            if (series.xaxisColumn)
                labels.push(row[series.xaxisColumn])
    //          if (series.styles.chartType)
    //                type.push(row[series.chartType])
        }

        for (const column of series.selectedColumns) {
            const columnStyle = SeriesStyle.getFor(column, series.styles);
			
            chartDataSets.push(new ChartDataSet({
                //label: column,
                label: series.datasourceConfiguration?.parametricQuery?.description,
                data: mapDataSets[column],
                backgroundColor: columnStyle.color,
                type: columnStyle.chartType
            }));
            colors.push(new BackgroundColor({
					backgroundColor: columnStyle.color
					}));
        }

        return new ChartDataLocal(labels, chartDataSets, colors);
    }
}

export class ChartDataSet {
    label?: string;
    backgroundColor?: string = "#92b3db";
    data: Number[] = [];
    type?: string;
   
    constructor(init?: Partial<ChartDataSet>) {
        Object.assign(this, init);
    }
}

export class BackgroundColor{
	backgroundColor: string = "#92b3db";
	
	constructor(init?: Partial<BackgroundColor>) {
		
        Object.assign(this, init);
    }
}