import { Component, Inject, OnInit } from '@angular/core';
import { MAT_DIALOG_DATA } from '@angular/material/dialog';
import { PaymentData } from 'src/app/interfaces/acquisition-management/provider/provider.interface';

@Component({
  selector: 'app-payment-methods',
  templateUrl: './payment-methods-provider.component.html',
  styleUrls: ['./payment-methods-provider.component.css']
})
export class PaymentMethodsProviderComponent implements OnInit {

  public payment:PaymentData;

  constructor(
    @Inject(MAT_DIALOG_DATA) public dataPayment: Array<PaymentData>,
  ) {
    this.payment = {
      name: 'Medios de Pago',
      completed: false,
      subtasks: this.orderPayment(this.dataPayment),
    }
  }

  ngOnInit(): void {
  }

  private orderPayment(dataPayment: Array<PaymentData>){
    let arrPayment: Array<any> = [];
    dataPayment.map((payment:PaymentData) => {
      let numPayment:number = parseInt(payment.name.split(':')[0]);
      arrPayment.push({
        no: numPayment,
        name: payment.name,
        completed: payment.completed,
      })
    });
    arrPayment.sort((a, b) => {
      return a.no - b.no;
    });
    return arrPayment;
  }

  public allComplete: boolean = false;

  public updateAllComplete() {
    this.allComplete = this.payment.subtasks != null && this.payment.subtasks.every(t => t.completed);
  }

  public someComplete(): boolean {
    if (this.payment.subtasks == null) {
      return false;
    }
    return this.payment.subtasks.filter(t => t.completed).length > 0 && !this.allComplete;
  }

  public setAll(completed: boolean) {
    this.allComplete = completed;
    if (this.payment.subtasks == null) {
      return;
    }
    this.payment.subtasks.forEach(t => (t.completed = completed));
  }

}
