import {
  Component,
  OnInit,
  Output,
  EventEmitter,
  AfterViewInit,
  ViewChild,
  OnDestroy
} from "@angular/core";
import { TranslatorService } from "../../services/translator/translator.service";
import { CommonService } from "../../services/common/common.service";
import { AuthenticationService } from "../../services/authentication/authentication.service";
import { ConfirmLoginComponent } from 'src/app/authentication/confirm-login/confirm-login.component';

import * as moment from 'moment'
import { ButtonSettings } from '../button/models/button-settingsl';
import { Events } from '@ionic/angular';

@Component({
  selector: "welcome-login",
  templateUrl: "./welcome.component.html",
  styleUrls: ["./welcome.component.scss"]
})
export class WelcomeComponent implements OnInit, AfterViewInit, OnDestroy {
  @ViewChild(ConfirmLoginComponent, {read: ConfirmLoginComponent }) confimLogin: ConfirmLoginComponent;

  // @ViewChild(ConfirmLoginComponent, { static: false, read: ConfirmLoginComponent }) confimLogin: ConfirmLoginComponent;
  public logo = "assets/imgs/bell.png";
  public direction: any;
  public user: any;
  public count: number = 0;
  public count2: number = 0;
  public txt_account: any;
  public isLastLogin: any;
  public welcome: string;
  public userName: string;
  public lastLoginTime: any;
  public welcome_txt: any;

  // public loginTypeDef = {
  //   1: this.ts.trPK("sms", "title"),
  //   2: this.ts.trPK("sms", "fingerprint"),
  //   3: this.ts.trPK("sms", "face-recognition"),
  //   4: this.ts.trPK("sms", "whatsapp")
  // };
  public loginTypeDef = {
    1: this.ts.trPK("sms", "title"),
    2: this.ts.trPK("sms", "fingerprint"),
    3: this.ts.trPK("sms", "face-recognition"),
    4: this.ts.trPK("sms", "whatsapp")
  };
  @Output() onLogin = new EventEmitter();
  @Output() loginWithUser = new EventEmitter();

  public button =
    {
      title: "login, more-verify",
      url: null,
      icon:  "assets/imgs/more.png",
      settings: new ButtonSettings(true, true, true, true),
      value: 2,
      txt: 'More',
      disabled: false,
      buttonClass: 'buttonClassWhite',
      pClass: 'pClassMyFileIconBlack',
      class: 'nationalIdIcon',
      visible: true
    };
  password: string;
  empname: string;
  logintype: any;
  lastLoginDate: string;
  user_name: string;

  constructor(
    public ts: TranslatorService,
    public cs: CommonService,
    public authservice: AuthenticationService,
    public events: Events
  ) { }

  ngOnInit() {
    // retrive user info ** 

    this.user = this.cs.sharedService.getSharedData(
      AuthenticationService.IMEI_USER_DATA,
      false
    );

   console.log( this.user);

    this.userName = localStorage.getItem("user");
    this.password = localStorage.getItem("password");
    let remember = localStorage.getItem("remember");
    this.empname=  localStorage.getItem("emp-name");
    this.logintype= localStorage.getItem("login-type");
    this.lastLoginDate= localStorage.getItem("login-at");



    // this.lastLoginDate=  new Date(this.lastLoginDate).toISOString()


      // let day = this.lastLoginDate.getDate();
      // let month = this.lastLoginDate.getMonth() + 1;
      // let year = this.lastLoginDate.getFullYear();
      // let todayDate = month + '/' + day + '/' + year;
    // this.lastLoginTime = moment(this.cs.evaluteDateAsObject(this.lastLoginDate)).format("HH:mm");

    this.logintype = parseInt(this.logintype)
     console.log("  this.lastLoginTime" +  this.lastLoginTime);
     console.log("  this.lastLoginDate" +  this.lastLoginDate);

    this.welcome_txt = this.ts.trPK("home", "welcome-txt");

    this.txt_account = this.user
      ? this.ts.trPK("home", "account-info")
      : "";

   

    this.setWelcomeMessage();
    this.setDescription();
        //later to fix below code
    if (this.user) {
      this.lastLoginTime = moment(this.cs.evaluteDateAsObject(this.user.EditedOn)).format("HH:mm");
    }
  }

  ngOnDestroy(): void {

  }

  private setWelcomeMessage1() {
    console.log(this.userName + "this.userName");

    if (this.userName) {
      this.welcome = this.ts.trPK("home", "welcome-back");
      console.log(this.welcome + "this.welcome");

      this.welcome += " " + this.empname || '';
      this.isLastLogin = true;
      console.log(this.isLastLogin + "this.isLastLogin");


    } else {
      this.welcome = this.ts.trPK("home", "welcome-head");


    }
  }


  private setWelcomeMessage() {
    console.log(" setWelcomeMessage ");

    if (this.user) {
      console.log(" this.user.EmployeeName: "+  this.user.EmployeeName);
      if(this.user.EmployeeName != null && this.user.EmployeeName !=''){
        this.user_name=this.user.EmployeeName; 
      }
      else{
        this.user_name="";
      }
      this.welcome = this.ts.trPK("home", "welcome-back");
      this.welcome += " " + this.user_name || '';

      this.isLastLogin = true;
    } else {
      this.welcome = this.ts.trPK("home", "welcome-head");
    }
  }



  private setDescription() {
    this.txt_account = this.userName ?
      this.ts.trPK("home", "account-info") :"";
  }

  ngAfterViewInit() {
    console.log("this.confimLogin.buttons[0]" + this.confimLogin.buttons[0]);
    setTimeout(() => {
      if (this.userName && this.confimLogin.buttons[0]) {
        
        console.log(this.userName );
        console.log(this.confimLogin.buttons[0]);

        this.confimLogin.buttons[0].forEach(element => {
          if (this.user.LoginType === element.value) {
            console.log(this.logintype );
            console.log(element.value);
            console.log("this.logintype === element.value");

            element.visible = true;
          } else {
            console.log("this.logintype != element.value");

            element.visible = false;
          }

        });
      }
    }, 100);
  }

  public moreLoginOptions() {}


 
  login() {
    this.onLogin.emit();
  }
  loginwithuser() {
    this.loginWithUser.emit();
  }



  // getDate(date: string) {
  //   return this.cs.getDateTimeISOFromString(date);
  // }
  getLoginType(type: any) {
    return this.loginTypeDef[type];
  }

  convertTime(time) {
    // alert(time)
    return time;
  }

}
