import {Component, Input, OnInit} from '@angular/core';
import {ChartWidget, Series, SeriesStyle} from 'src/app/widgets/chart/models/ChartWidget';
import {ParametricQuery} from "../../../../models/enea/ParametricQuery";
import {Parameter} from "../../../../models/Parameter";
import {DatasourceConfiguration} from "../../../Widget";
import {DataService} from "../../../../services/data.service";
import {UtilsService} from "../../../../services/utils.service";
import {TranslateService} from "@ngx-translate/core";

@Component({
  selector: 'app-chart-widget-configuration',
  templateUrl: './chart-widget-configuration.component.html',
  styleUrls: ['./chart-widget-configuration.component.css']
})
export class ChartWidgetConfigurationComponent implements OnInit {

  @Input() widget?: ChartWidget;
  public parametricQueries: ParametricQuery[] = [];

  public chartTypes: { name: string, description: string }[] = [];
  
  constructor(private dataService: DataService, private utils: UtilsService, private translateService: TranslateService) { }

  ngOnInit(): void {
    this.translateService.get([
      "CHART_WIDGET_CONFIGURATION.CHART_TYPES.BAR",
      "CHART_WIDGET_CONFIGURATION.CHART_TYPES.PIE",
      "CHART_WIDGET_CONFIGURATION.CHART_TYPES.DOUGHNUT",
      "CHART_WIDGET_CONFIGURATION.CHART_TYPES.LINE",
      "CHART_WIDGET_CONFIGURATION.CHART_TYPES.POLARAREA",
      "CHART_WIDGET_CONFIGURATION.CHART_TYPES.RADAR",
    ]).subscribe(o => {
      const values = Object.values(o) as string[];
      this.chartTypes = [
        { name: "bar", description: values[0] },
        { name: "pie", description: values[1] },
        { name: "doughnut", description: values[2] },
        { name: "line", description: values[3] },
        { name: "polarArea", description: values[4] },
        { name: "radar", description: values[5] }
      ];
    });
    
    if (this.widget && !this.widget?.seriesList)
      this.widget.seriesList = [];
    this.loadParametricQueries();
    this.refreshAllStylesList();

  }

  loadParametricQueries() {
    this.dataService.getListOfFunctions().subscribe(
        parametricQueries => this.parametricQueries = parametricQueries,
        error => this.utils.showErrorMessage(error)
    );
  }

  resetParameters() {
    if (this.widget && this.widget.seriesList) {
      /*this.widget.configuration.parameters = [];
      if (this.widget.configuration.parametricQuery && this.widget.configuration.parametricQuery.parameters) {
        this.widget.configuration.parameters = this.widget.configuration.parametricQuery.parameters.map(o => new Parameter(o.name || '', '', undefined));
      }*/
    }
  }

  addSeries() {
    if (this.widget) {
      if (!this.widget.seriesList)
        this.widget.seriesList = [];

      this.widget.seriesList.push(new Series());
    }
  }

  removeSeries(series: Series, index: number) {
    this.widget?.seriesList?.splice(index, 1);
  }

  selectedColumnsChanged(series: Series, event: any) {
    this.refreshColumnsStylesListForSeries(series);
  }

  refreshAllStylesList() {
    if (!this.widget?.seriesList)
      return;
    for (const series of this.widget?.seriesList)
      this.refreshColumnsStylesListForSeries(series);
  }

  refreshColumnsStylesListForSeries(series: Series) {
    if (!series.styles)
      series.styles = [];

    const seriesStyle: SeriesStyle[] = [];
    for (const column of series.selectedColumns) {
      const columnStyles = series.styles.filter(o => o.column === column);
      if (columnStyles.length > 0)
        seriesStyle.push(columnStyles[0])
      else
        seriesStyle.push(new SeriesStyle(column));
    }
    series.styles = seriesStyle;
  }
}
