import { Cliente } from './../../auth/interface/cliente';
import { Component } from '@angular/core';
import { Router } from  "@angular/router";
import { Events, MenuController, Platform } from '@ionic/angular';
import { AuthService } from '../../auth/service/auth.service';
import { UtilService } from '../../service/util.service';
import { Pair } from 'src/app/interface/pair';
import { TranslateService } from '@ngx-translate/core';


@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss']
})
export class HomePage {
  constructor(
    private  authService:  AuthService, 
    private  router:  Router, 
    public events: Events, 
    public menuCtrl: MenuController, 
    private utilService: UtilService, 
    public translate: TranslateService,
    public platform: Platform
    ) {
    this.clienti = [];
    this.clienteSelezionato = {
      denominazione: null,
      id: null
    };
    /*this.authService.isLoggedIn().then(state => {
      if (!state) {
        this.router.navigate(['login']);
      }
    });*/
    this.events.subscribe('functionCall:startup', eventData => {
      if(this.clienteSelezionato.id == null) {
        if(!authService.areClientiSalvati()) 
        {
          setTimeout(() => {
            this.loadClienti()
           }, 3000)
        }
        else this.loadClienti();
      }
    });
    this.events.subscribe('functionCall:linguaCambiata', eventData => {
      translate = utilService.translate;
    });
    this.events.subscribe('functionCall:parere', eventData => {
      this.router.navigateByUrl('home/parere');
    });
    this.events.subscribe('functionCall:interviste', eventData => {
      this.router.navigateByUrl('home/interviste');
    });
    this.lingue = utilService.getLanguageList();
    if(utilService.getLingua() == "") {
      this.linguaSel = this.lingue[0].key;
      utilService.setLingua(this.linguaSel);
      utilService.translate.setDefaultLang(this.linguaSel);
      utilService.translate.use(this.linguaSel);
    }
    else this.linguaSel = utilService.getLingua();
    translate = utilService.translate;
    this.platform.backButton.subscribeWithPriority(0, () => {
      let defaultUrl: string []=[
        "/home",
        "/login",
        "/reset",
        "/businesscall",
        "/privacy",
        "/home/index",
        "/home/documenti",
        "/home/change-pwd",
        "/home/segreteria"
      ];

      if(defaultUrl.indexOf(this.router.url) != -1)
      {
        switch(this.router.url)
        {
          case "/login":
          {
            navigator['app'].exitApp();
            break;
          }
          case "/home":
          case "/home/index":
          case "/reset":
          case "/businesscall":
          {
            this.router.navigateByUrl('login');
            break;
          }
          case "/privacy":
          {
            this.router.navigateByUrl('businesscall');
            break;
          }
          case "/home/documenti":
          case "/home/change-pwd":
          case "/home/segreteria":
          {
            this.router.navigateByUrl('home/index');
            break;
          }
        }
      }
      else this.events.publish('functionCall:manageBackButton', null);
    });
   }



  clienti: Cliente[];
  clienteSel;
  clienteSelezionato: Cliente;
  linguaSel: string;
  lingue : Array<Pair>;
  
  async loadClienti()
  {
    this.authService.loadClienti().then(state => {
      this.clienti = AuthService.clienti;
      this.clienteSelezionato = AuthService.clienteSelezionato;
      this.events.publish('functionCall:clienteCambiato', null);
    });
    return true;
  }

  cambiaCliente()
  {
    this.authService.setCliente(this.clienteSel);
    this.clienteSelezionato = AuthService.clienteSelezionato;
    this.events.publish('functionCall:clienteCambiato', null);
  }

  cambiaLingua()
  {
    this.utilService.setLingua(this.linguaSel);
    this.utilService.translate.setDefaultLang(this.linguaSel);
    this.utilService.translate.use(this.linguaSel);
    this.events.publish('functionCall:linguaCambiata', null);
  }

  logout(){
    //this.authService.logout();
    //this.router.navigateByUrl('login');
    this.utilService.logout();
  }

  ngOnInit() {
  }

  apriPagina(pagina:string)
  {
    this.utilService.apriPagina(pagina);
    this.closeSideMenu();
  }

  

  openSideBar()
  {
    this.menuCtrl.enable(false, 'pareri');
    this.menuCtrl.enable(false, 'topics');
    this.menuCtrl.enable(true, 'mainmenu');
    this.menuCtrl.toggle('mainmenu');
  }

  closeSideMenu() {
    this.menuCtrl.close("mainmenu");
  }
}
