import { Component, OnInit } from '@angular/core';
import { NavigationStart, Router } from '@angular/router';
import { customTask, DailyWorkTask } from '../../../models/dailyworktask.model';
import { ProcessesService } from 'src/app/services/processes.service';
import { ModalController } from '@ionic/angular';
import { AlertService } from 'src/app/services/alert.service';
import { DeplomasStore } from 'src/app/store/deplomas.service';
import { CustomTaskPipe } from 'src/app/pipes/custom-task.pipe';
import { SortService } from 'src/app/services/functions/sort.service';
import { ThemeService } from 'src/app/services/theme.service'


@Component({
  selector: 'app-diplomas-assinar',
  templateUrl: './diplomas-assinar.page.html',
  styleUrls: ['./diplomas-assinar.page.scss'],
})
export class DiplomasAssinarPage implements OnInit {

  diplomasList:DailyWorkTask[] = [];
  serialNumber:string;
  skeletonLoader = true

  deplomasStore = DeplomasStore
  customTaskPipe = new CustomTaskPipe()

  constructor(
    private processes:ProcessesService,
    private modalController: ModalController,
    private alertService: AlertService,
    private router: Router,
    private sortService: SortService,
    public ThemeService: ThemeService
    ) {}

  ngOnInit() {

    this.LoadList()

    this.router.events.forEach((event) => {
      if (event instanceof NavigationStart && '/home/gabinete-digital?diplomasassinar=true'.startsWith(event.url)) {
        if(window.location.pathname.split('/').length >= 4 && window.location.pathname.startsWith('/home/gabinete-digital')) {
          this.doRefresh()
        } else {
          this.LoadList()
        }
      }
    });

  }

  goToDiploma(serialNumber:any){
    this.router.navigate(['/home/gabinete-digital/diplomas-assinar',serialNumber,'gabinete-digital']);
  }

  doRefresh() {
    setTimeout(() => {
      this.LoadList();
    }, 1000);
  }

  async LoadList() {



    this.skeletonLoader = true

    let diplomas = await this.processes.GetTasksList("Despacho do Presidente da República", false).toPromise();
    this.diplomasList = new Array();
    this.skeletonLoader = false

    let diplomasAssinar = diplomas.reverse().filter(data => data.activityInstanceName == "Assinar Diploma");

    diplomasAssinar.forEach(element => {
      let task: customTask = this.customTaskPipe.transform(element)
      this.diplomasList.push(task);
    });

    this.diplomasList = this.sortService.sortDate(this.diplomasList, 'CreateDate')


    this.deplomasStore.resetDiplomasList(this.diplomasList);
  }

}
