import { Component, OnInit } from "@angular/core";
import { CommonService } from "src/app/hmg-common/services/common/common.service";
import { TranslatorService } from "src/app/hmg-common/services/translator/translator.service";
import { AuthenticationService } from "src/app/hmg-common/services/authentication/authentication.service";
import { AuthenticatedUser } from "src/app/hmg-common/services/authentication/models/authenticated-user";
import {  Events } from "@ionic/angular";
import { DomSanitizer } from '@angular/platform-browser';
@Component({
  selector: "app-home",
  templateUrl: "./home.component.html",
  styleUrls: ["./home.component.scss"]
})
export class HomeComponent implements OnInit {
  personalInfo: any;
  setImage:any;
  imageSrc: any = "../assets/imgs/profile.png";
  // user_image: any = "../assets/imgs/profile.png";
  constructor(
    public ts: TranslatorService,
    public cs: CommonService,
    public authService: AuthenticationService,
    private events: Events,
    private sanitizer: DomSanitizer,


  ) {
   
    this.events.subscribe("img-change", displayImg => {
     // alert("1 - profile home : "+displayImg);
      //this.user_image = "data:image/png;base64"+displayImg;
      this.setImage = this.sanitizer.bypassSecurityTrustUrl("data:Image/*;base64,"+displayImg);
      this.imageSrc = this.sanitizer.bypassSecurityTrustUrl("data:Image/*;base64,"+displayImg);
       
    });

 
  }

  ngOnInit() {
    this.getProfile();
  }
  getProfile() {
    console.log("getProfile");
    this.authService
      .loadAuthenticatedUser()
      .subscribe((user: AuthenticatedUser) => {
        if (user) {
          this.personalInfo = user;
          console.log("name: "+ this.personalInfo.EMPLOYEE_NAME);
          console.log("user name: "+ user.EMPLOYEE_NAME);

          if(this.cs.getUpdateImage().status){
           // this.imageSrc = this.sanitizer.bypassSecurityTrustUrl("data:Image/*;base64,"+this.cs.getUpdateImage().img);
           this.imageSrc =this.sanitizer.bypassSecurityTrustUrl("data:image/png;base64,"+this.cs.getUpdateImage().img);
          }else{
          this.imageSrc = user.EMPLOYEE_IMAGE
            ? "data:image/png;base64," + user.EMPLOYEE_IMAGE
            : this.imageSrc;
            console.log("2-"+user);
        } 
      }
        else {
          console.log("3-"+user);
        }
      });
  }


  private ChangeImage() {
    this.cs.openChangeImagePage();
  }
}

