import {Widget, DatasourceConfiguration} from "../../Widget";
import {Metadata} from "../../../models/enea/ParametricQuery";


export class ChartWidget extends Widget {
	datasourceConfiguration?: DatasourceConfiguration = new DatasourceConfiguration();
    chartType?: string;
    seriesList?: Series[] = [];
    sortData?: boolean = true;
    skipXLabel?: boolean = false;
    target?: string;
    parameterName?: string;
    // data?: any;
    public constructor(init?: Partial<ChartWidget>) {
        super();        
        Object.assign(this, init);
        this.type = "CHART";
    }
}

export class Series {
    datasourceConfiguration?: DatasourceConfiguration;
    selectedColumns: string[] = [];
    xaxisColumn?: string;
    styles: SeriesStyle[] = [];
    label: String = "";

    public constructor() {
        this.datasourceConfiguration = new DatasourceConfiguration();
    }
}

export class SeriesStyle {
    column: string;
    color?: string;
    chartType?: string;
    fill?: boolean = false;
    interpolate?: boolean = false;

/*    constructor(column: string, init?: Partial<SeriesStyle>) {
        Object.assign(this, init);
        this.column = column;
    }
*/  
    constructor(column: string, init?: Partial<SeriesStyle>) {
        Object.assign(this, init);
        this.column = column;
    }

    public static getFor(column: string, styles: SeriesStyle[]): SeriesStyle {
        const columnStyles = styles.filter(o => o.column === column);
        return columnStyles.length > 0 ? columnStyles[0] : new SeriesStyle(column);
    }



    
}

export class SeriesMetadata {
	unitOfMeasure: string ="";
    name: string = "";
    description: string = "";
    type: string = "";
    example: string = "";

   constructor(name: string, unitOfMeasure?: string, init?: Partial<SeriesStyle>) {
        Object.assign(this, init);
        this.name = name;
        this.unitOfMeasure = unitOfMeasure ? unitOfMeasure : "";
    }

    public static getFor(name: string, metadata: Metadata[]): Metadata {
        const columnMetadata = metadata.filter(o => o.name === name);
        return columnMetadata.length > 0 ? columnMetadata[0] : new SeriesMetadata(name);
    }
}