import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { NavigationStart, Router } from '@angular/router';
import { removeDuplicate } from 'src/plugin/removeDuplicate.js'
import { customTask} from '../../../models/dailyworktask.model';
import { ProcessesService } from 'src/app/services/processes.service';
import { AuthService } from 'src/app/services/auth.service';
import { PendentesStore } from 'src/app/store/pendestes-store.service';
import { LoginUserRespose } from 'src/app/models/user.model';
import { CustomTaskPipe } from 'src/app/pipes/custom-task.pipe';
import { SortService } from 'src/app/services/functions/sort.service';

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

  skeletonLoader: boolean = false;
  pendentesstore = PendentesStore;
  customTaskPipe = new CustomTaskPipe()
  loggeduser: LoginUserRespose;

  @Input() profile:string;
  segment:string;
  @Output() openExpedientDetail:EventEmitter<any> = new EventEmitter<any>();

  constructor(
    private processes:ProcessesService,
    private authService: AuthService,
    private router: Router,
    private sortService: SortService,
    ) {
      this.loggeduser = authService.ValidatedUser;
    }

  ngOnInit() {

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

    });

  }

  async LoadList(){
    this.skeletonLoader = true;

    let pendentes = await this.processes.GetPendingTasks(false).toPromise();
    let pendentesList = [];

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

    pendentesList = removeDuplicate( pendentesList)
    pendentesList = this.sortService.sortDate(pendentesList, 'CreateDate')

    this.pendentesstore.reset(pendentesList);
    this.skeletonLoader = false;
  }

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

  }

  async viewTaskDetails({ SerialNumber, WorkflowName, activityInstanceName }:customTask) {

    console.log(SerialNumber, WorkflowName, activityInstanceName)

    if(WorkflowName == 'Despacho') {
      this.router.navigate(['/home/gabinete-digital/despachos',SerialNumber,'gabinete-digital']);
    }
    else if (WorkflowName == 'Despacho do Presidente da República') {
      this.router.navigate(['/home/gabinete-digital/despachos', SerialNumber, 'gabinete-digital']);
    }
    else if(WorkflowName == 'Pedido de Parecer' || WorkflowName == 'Pedido de Deferimento' || WorkflowName == 'Pedido de Parecer do Presidente') {
      this.router.navigate(['/home/gabinete-digital/pedidos',SerialNumber,'gabinete-digital']);
    }
    else if(WorkflowName == 'Expediente') {
      this.router.navigate(['/home/gabinete-digital/expediente',SerialNumber,'gabinete-digital']);
    }
    else if(WorkflowName == 'Expediente' && this.loggeduser.Profile == 'PR') {
      this.router.navigate(['/home/gabinete-digital/expedientes-pr',SerialNumber,'gabinete-digital']);
    }
    else if (activityInstanceName == "Tarefa de Parecer") {
      this.router.navigate(['/home/gabinete-digital/pedidos',SerialNumber,'gabinete-digital']);
    }
    else if(activityInstanceName == "Tarefa de Despacho") {
      this.router.navigate(['/home/gabinete-digital/despachos',SerialNumber,'gabinete-digital']);
    }
    else  {
      console.log('cant find page for this task', WorkflowName, activityInstanceName)
    }

  }

 }
