import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { AggregateDescriptor, GroupDescriptor, GroupResult, groupBy } from '@progress/kendo-data-query';
import { map } from 'rxjs/operators';
import { SaleHistory } from 'src/app/models/sale-history.model';
import { OrderService } from 'src/app/services/order.service';

@Component({
  selector: 'app-dashboard-card',
  templateUrl: './dashboard-card.component.html',
  styleUrls: ['./dashboard-card.component.scss']
})
export class DashboardCardComponent implements OnInit {
  @Input() id: number;
  @Input() name: string;
  @Input() valueGoal: number;
  @Input() valueReal: number;
  @Input() valuePrevGoal: number;
  @Input() valuePrevReal: number;
  @Input() currency: boolean;
  @Input() monthDayPrevious: string = 'M';
  @Input() historySale: boolean = false;
  @Output() returnCard = new EventEmitter<boolean>();

  showHistorySale = false;
  saleHistory: SaleHistory[] | GroupResult[] = []

  public aggregates: AggregateDescriptor[] = [
    { field: "saleValue", aggregate: "sum" },
    { field: "devolucao", aggregate: "sum" },
    { field: "amount", aggregate: "sum" },
  ];

  public group: GroupDescriptor[] = [
    { field: "sellerId", aggregates: this.aggregates },
  ];

  constructor(
    private readonly orderService: OrderService
  ) { }

  ngOnInit(): void {
    if (this.historySale) {
      this.orderService.getSaleSeller(this.id)
        .pipe(
          map(result => this.saleHistory = groupBy(result, this.group)
        ))
        .subscribe();
    }
  }

  executeLink() {
    this.returnCard.emit(true);
  }

  previousMonth() {
    let month = new Date().getMonth() + 1;
    if (month === 1){
      month = 12;
    } else {
      month -= 1;
    }

    switch (month) {
      case 1:
        return 'Janeiro';
        break;
      case 2:
        return 'Fevereiro';
        break;
      case 3:
        return 'Março';
        break;
      case 4:
        return 'Abril';
        break;
      case 5:
        return 'maio';
        break;
      case 6:
        return 'Junho';
        break;
      case 7:
        return 'Julho';
        break;
      case 8:
        return 'Agosto';
        break;
      case 9:
        return 'Setembro';
        break;
      case 10:
        return 'Outubro';
        break;
      case 11:
        return 'Novembro';
        break;
      case 12:
        return 'Dezembro';
        break;
      default:
        return 'Não identificado';
        break;
    }
  }

  previousDay() {
    let dateNow = new Date();
    let today = dateNow.getDate();
    let yeasterday = dateNow.getDate() - 1;
    let month = dateNow.getMonth() + 1;
    let year = dateNow.getFullYear();
    if ( yeasterday > today ) {
      month = dateNow.getMonth();
    }
    const dateYeasterday = `${yeasterday}/${month}/${year}`;
    return dateYeasterday;
  }

  closeHistorySale(): void {
    this.showHistorySale = false;
  }

  openHistorySale(): void {
    this.showHistorySale = true;
  }

}
