import {Component, OnDestroy, OnInit} from '@angular/core';
import {MenuItem, MessageService, PrimeNGConfig} from "primeng/api";
import {Observable, Subject} from "rxjs";
import {User} from "../../model/user";
import {LogoutAction, UserState} from "../../states/user.state";
import {Select, Store} from "@ngxs/store";
import {NavigationEnd, Router, RouterEvent} from "@angular/router";
import {filter, takeUntil} from "rxjs/operators";

@Component({
  selector: 'app-header-menu',
  templateUrl: './header-menu.component.html',
  styleUrls: ['./header-menu.component.css']
})
export class HeaderMenuComponent implements OnInit,OnDestroy{

  items: MenuItem[] = [] ;
  public destroyed = new Subject<any>();
  // @ts-ignore
  public loggeduser: Observable<User> = this.store.select(UserState.userDetails);
  logged?:boolean

  constructor(private primengConfig: PrimeNGConfig, private store: Store, private router: Router,private messageService: MessageService) {}
  initialise() {
    this.logged = this.isLogged();
    // @ts-ignore
    this.items = [
      {
        label: 'Home',
        routerLink:'/home',
        styleClass:"pr-2 ",
      },
      {
        label: 'Add resource',
        url:'#/resource/add',
        styleClass:"pr-2",
        target:"_blank"
      },
      {
        label: 'Approve',
        routerLink:'/resource/approve',
        styleClass:"pr-2",
        visible:this.isUserAdmin()
      },
      {
        label: 'IPMWORKS Project',
        url:'http://ipmworks.net',
        styleClass:"pr-2",
        target:"_blank"
      },
      {
        label: 'Help and Support',
        routerLink:'/help',
        styleClass:"pr-2",
        target:"_blank"
      },
      {
        label: 'Logout',
        command:(event) => {this.logout()},
        styleClass:"pr-2",
        visible:this.logged
      },
      {
        label: 'Login',
        routerLink:'/login',
        styleClass:"pr-2",
        visible:!this.logged
      }
    ]
  }
  ngOnInit(): void {
    this.router.events.pipe(filter((event) => event instanceof NavigationEnd),
      takeUntil(this.destroyed)
    ).subscribe(() => {
      this.router.routeReuseStrategy.shouldReuseRoute = function() { return false; };
      this.initialise();
    });
  }
  public isUserAdmin() {
    var admin = false;
    this.loggeduser?.subscribe(user => {
      if(user == undefined || user.token == undefined){
        admin=false;
      }else{
        // @ts-ignore
        admin=user?.roles.includes("ADMIN")
      }
    });
    return admin;
  }
  public isLogged() {
    var logged = false;
    this.loggeduser?.subscribe(user => {
      if(user == undefined || user.token == undefined){
        logged=false;
      }else{
        logged = true;
      }
    });
    return logged;
  }
  ngOnDestroy(): void {
    this.destroyed.next();
    this.destroyed.complete();
  }


  public logout(): void {
    this.store.dispatch(new LogoutAction());
    this.router.navigate(['/home']);
    this.messageService.add({severity:"success", detail:"Goodbye!"})
  }
  }

























































