import { Component, EventEmitter, Input, Output } from '@angular/core';
import { Observable, of } from 'rxjs';
import { Router } from '@angular/router';

import { Email, User } from '../../../../pages/auth/models';
import { EmailService } from '../../../../pages/auth/services';
import { routes } from '../../../../consts';
import { AuthService } from 'src/app/_providers/_services/auth.service';

@Component({
  selector: 'app-header',
  templateUrl: './header.component.html',
  styleUrls: ['./header.component.scss']
})
export class HeaderComponent {
  @Input() isMenuOpened: boolean;
  @Output() isShowSidebar = new EventEmitter<boolean>();
  public user$: Observable<User>
  public emails$: Observable<Email[]>
  public routers: typeof routes = routes;
  schoolLogoPath: string;

  constructor(
    private emailService: EmailService,
    public router: Router,
    private authenticationService: AuthService
  ) {
     this.user$ = this.getUser();
    this.emails$ = this.emailService.loadEmails();
    this.getSchoolLogo();
  }

  public getSchoolLogo(){
    let institute = localStorage.getItem('institute');
    if(institute){
      this.schoolLogoPath = "./../../../../../assets/accurate/images/"+institute+".png";
    }else{
      this.schoolLogoPath = "./../../../../../assets/accurate/images/institute.png";
    }
  }

  public openMenu(): void {
    this.isMenuOpened = !this.isMenuOpened;

    this.isShowSidebar.emit(this.isMenuOpened);
  }

  public signOut(): void {
   // console.log('hwwwwww');
    
    this.authenticationService.logOut();
    
  }

  goToWebsite(){
    this.authenticationService.logOut().then(res=>{
      window.location.href = 'https://www.thrickodithanamscb.in/#/auth/website'

    });
    // window.location.href = 'https://www.thrickodithanamscb.in/index.html#hero';

  }

  goToHome(currentLoc){
   // console.log('current Location = ',currentLoc);
    if(currentLoc.includes('auth')){
      this.router.navigate(['/auth/admin-home'])
    }
    else{
      this.router.navigate(['/dashboard'])

    
    }
   
  }
  goToSignIn(){
    this.router.navigate(['/auth/login'])
  }

  public getUser(): Observable<User> {
    return of({
       email: localStorage.getItem('emailId'),
       name: localStorage.getItem('username'),

    });
  }
}
