import { Component, OnInit } from '@angular/core';
import { MatTableDataSource } from '@angular/material/table';
import { ServiceComm } from '../../services/service-comm.service';
import { StatsService } from '../../services/stats.service';
import {
  StatsByDzResp,
  StatsByAircraftResp,
  StatsByGearResp,
  StatsByJumpTypeResp,
  StatsByYearResp,
  StatType
} from '../../models/stats';

@Component({
  selector: 'app-summary',
  templateUrl: './summary.component.html',
  styleUrls: ['./summary.component.css']
})

export class SummaryComponent implements OnInit {
  public dsNbJumpByDz: MatTableDataSource<StatsByDzResp>;
  public dsNbJumpByAircraft: MatTableDataSource<StatsByAircraftResp>;
  public dsNbJumpByGear: MatTableDataSource<StatsByGearResp>;
  public dsNbJumpByType: MatTableDataSource<StatsByJumpTypeResp>;
  public dsNbJumpByYear: MatTableDataSource<StatsByYearResp>;
  public dsJumpForLastYearByDz: MatTableDataSource<StatsByDzResp>;
  public dsJumpForLastYearByJumpType: MatTableDataSource<StatsByJumpTypeResp>;
  public dsJumpForLastMonthByDz: MatTableDataSource<StatsByDzResp>;
  public dsJumpForLastMonthByJumpType: MatTableDataSource<StatsByJumpTypeResp>;

  public displayedColumns: Array<string> = ['label', 'nb'];
  private _shownStats: Map<StatType, boolean>;
  public StatType = StatType;

  public totalJumps: number;
  public totalCutaways: number;
  public lastJump: string;

  constructor(
    private _serviceApi: StatsService,
    private _serviceComm: ServiceComm
  ) { }

  ngOnInit() {
    this.initShownStats();

    this._serviceComm.UpdatedComponentTitle('Summary');

    const statsResult = this._serviceApi.getStatsOfJumps();

    statsResult.simpleSummary.subscribe(data => {
      this.totalJumps = data.totalJumps;
      this.totalCutaways = data.totalCutaways;
      this.lastJump = data.lastJump.jumpDate + ' (' + data.lastJump.dropZone.name + ')';
    });

    statsResult.statsByDz.subscribe(data => {
      this.dsNbJumpByDz = new MatTableDataSource(data);
    });
    statsResult.statsByAircraft.subscribe(data => {
      this.dsNbJumpByAircraft = new MatTableDataSource(data);
    });
    statsResult.statsByGear.subscribe(data => {
      this.dsNbJumpByGear = new MatTableDataSource(data);
    });
    statsResult.statsByJumpType.subscribe(data => {
      this.dsNbJumpByType = new MatTableDataSource(data);
    });
    statsResult.statsByYear.subscribe(data => {
      this.dsNbJumpByYear = new MatTableDataSource(data);
    });

    statsResult.statsForLastYear.subscribe(data => {
      this.dsJumpForLastYearByDz = new MatTableDataSource(data.byDz);
      this.dsJumpForLastYearByJumpType = new MatTableDataSource(
        data.byJumpType
      );
    });
    statsResult.statsForLastMonth.subscribe(data => {
      this.dsJumpForLastMonthByDz = new MatTableDataSource(data.byDz);
      this.dsJumpForLastMonthByJumpType = new MatTableDataSource(
        data.byJumpType
      );
    });
  }

  public showStats(statsToShow: StatType) {
    this._shownStats.set(StatType.ForLastMonth, false);
    this._shownStats.set(StatType.ForLastYear, false);
    this._shownStats.set(StatType.ByDz, false);
    this._shownStats.set(StatType.ByAircraft, false);
    this._shownStats.set(StatType.ByGear, false);
    this._shownStats.set(StatType.ByJumpType, false);
    this._shownStats.set(StatType.ByYear, false);

    this._shownStats.set(statsToShow, true);
  }

  private initShownStats() {
    this._shownStats = new Map<StatType, boolean>();

    this._shownStats.set(StatType.ForLastMonth, false);
    this._shownStats.set(StatType.ForLastYear, false);
    this._shownStats.set(StatType.ByDz, false);
    this._shownStats.set(StatType.ByAircraft, false);
    this._shownStats.set(StatType.ByGear, false);
    this._shownStats.set(StatType.ByJumpType, false);
    this._shownStats.set(StatType.ByYear, true);
  }

  public statsToShow(statsToShow: StatType): boolean {
    return this._shownStats.get(statsToShow);
  }
}
