import { CommonModule } from '@angular/common';
import { Component, Input } from '@angular/core';
import { MatSlideToggleModule } from '@angular/material/slide-toggle';
import { BehaviorSubject, map } from 'rxjs';
import { TimePerTourMetric } from '../../../../core/data-access/graphql/generated/generated';
import { DiagrammType, MetricTimeScale } from '../../types';
import { AverageRevenueMetricComponent } from '../average-revenue-metric/average-revenue-metric.component';
import { AverageRevenueSummaryComponent } from '../average-revenue-summary/average-revenue-summary.component';
import { BreakevenMetricComponent } from '../breakeven-metric/breakeven-metric.component';
import { BreakevenSummaryComponent } from '../breakeven-summary/breakeven-summary.component';
import { TimePerTourMetricComponent } from '../time-per-tour-metric/time-per-tour-metric.component';
import { TimePerTourSummaryComponent } from '../time-per-tour-summary/time-per-tour-summary.component';

@Component({
    selector: 'dks-metric-summary',
    imports: [
        CommonModule,
        AverageRevenueMetricComponent,
        AverageRevenueSummaryComponent,
        BreakevenMetricComponent,
        BreakevenSummaryComponent,
        TimePerTourMetricComponent,
        TimePerTourSummaryComponent,
        MatSlideToggleModule,
    ],
    templateUrl: './metric-summary.component.html',
    styleUrls: ['./metric-summary.component.css']
})
export class MetricSummaryComponent {
  @Input() set metrics(metrics: TimePerTourMetric[]) {
    this._metrics$.next(metrics);
  }
  @Input() selectedMetric: DiagrammType = DiagrammType.TimePerTour;
  @Input() metricTimeScale!: MetricTimeScale;
  @Input() labelFormat!: string;

  DiagrammType = DiagrammType;
  _metrics$ = new BehaviorSubject<TimePerTourMetric[]>([]);
  showCategories = false;

  vm$ = this._metrics$.pipe(
    map((metrics) => ({
      metrics,
    }))
  );

  getLabelFormatForMetricTimeScale(timeScale: MetricTimeScale): string {
    if (timeScale === MetricTimeScale.Dayview) {
      return 'HH:mm';
    } else if (timeScale === MetricTimeScale.Monthview) {
      return 'd.M.yy';
    } else {
      throw new Error(
        'No label format for time scale defined, TimeScale: ' + timeScale
      );
    }
  }
}
