import { Component, OnInit } from '@angular/core';
import { EventBody } from 'src/app/models/eventbody.model';
import { EventPerson } from 'src/app/models/eventperson.model';
import { ProcessesService } from 'src/app/services/processes.service';
import { ModalController } from '@ionic/angular';
import { ApproveEventModalPage } from './approve-event-modal/approve-event-modal.page';
import { NavigationStart, Router } from '@angular/router';
import { EventoAprovacaoStore } from 'src/app/store/eventoaprovacao-store.service';
import { Location } from '@angular/common';

@Component({
  selector: 'app-event-list',
  templateUrl: './event-list.page.html',
  styleUrls: ['./event-list.page.scss'],
})
export class EventListPage implements OnInit {
  // [desktop] event list to approve

  profile:string;
  segment:string;
  showLoader: boolean;
  eventsPRList: any = []
  eventsMDGPRList: any = []
  eventPerson: EventPerson;
  eventBody: EventBody;
  categories: string[];
  serialnumber:string;

  skeletonLoader = true
  eventaprovacaostore = EventoAprovacaoStore;


  constructor(
    private processes:ProcessesService,
    private modalController: ModalController,
    private router: Router,
    private location: Location
  ) { }

  ngOnInit() {
    this.segment =  'MDGPR';

    const location = window.location
    const pathname = location.pathname + location.search

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


    window.onresize = (event) => {
      // if not mobile remove all component
      if( window.innerWidth <= 800){
        this.modalController.dismiss();
      }
    };

  }

  segmentChanged(ev: any) {
    this.LoadToApproveEvents();
  }

  async LoadToApproveEvents(){
    this.showLoader = true;
    this.skeletonLoader = true

    console.log(this.segment);
    if(this.segment == 'MDGPR'){
      let mdEventsOficial = await this.processes.GetTasksList('Agenda Oficial MDGPR', false).toPromise();
      let mdEventsPessoal = await this.processes.GetTasksList('Agenda Pessoal MDGPR', false).toPromise();
      this.eventsMDGPRList = mdEventsOficial.concat(mdEventsPessoal);
      console.log(this.eventsMDGPRList);

      this.eventaprovacaostore.resetmd(this.eventsMDGPRList);
    }
    else if(this.segment == 'PR'){
      let prEventsOficial = await this.processes.GetTasksList('Agenda Oficial PR', false).toPromise();
      let prEventsPessoal = await this.processes.GetTasksList('Agenda Pessoal PR', false).toPromise();
      this.eventsPRList = prEventsOficial.concat(prEventsPessoal);
      this.eventaprovacaostore.resetpr(this.eventsPRList);
    }
    this.showLoader = false;
    this.skeletonLoader = false
  }

  async openApproveModal(eventSerialNumber, event){
    const modal = await this.modalController.create({
      component: ApproveEventModalPage,
      componentProps:{
        serialNumber: eventSerialNumber,
      },
      cssClass: 'event-list cal-modal  modal modal-desktop',
      backdropDismiss: false
    });
    await modal.present();
  }

  goToEventToApproveDetail(serialNumber:string){
    console.log(serialNumber);

    /* let navigationExtras: NavigationExtras = {
      queryParams: {
        "serialNumber": serialNumber,
      }
    }; */

    if(this.router.url == '/home/agenda/event-list'){
      this.router.navigate(['/home/agenda/event-list/approve-event',serialNumber, 'agenda'])
    }
    else if(this.router.url == '/home/gabinete-digital/event-list'){
      this.router.navigate(['/home/gabinete-digital/event-list/approve-event',serialNumber, 'gabinete-digital'])
    }

    //this.router.navigate(['/home/gabinete-digital/event-list/approve-event'], navigationExtras)
  }

  refreshing() {
    setTimeout(() => {
      this.LoadToApproveEvents();
    }, 1000);
  }

  doRefresh(event) {

    setTimeout(() => {
      this.LoadToApproveEvents();
      event.target.complete();
    }, 1000);
  }

  close(){
    this.modalController.dismiss(null);
  }
  goBack() {
    // if(this.router.url == '/home/agenda/event-list') {
    //   this.router.navigate(['/home/agenda']);
    // }
    // else if(this.router.url == '/home/gabinete-digital/event-list'){
    //   this.router.navigate(['/home/gabinete-digital']);
    // }

    this.location.back()
  }


}
