import { Component } from '@angular/core';
import { Router } from '@angular/router';
import { Store } from '@ngrx/store';
import { Observable } from 'rxjs';
import { UserStore } from 'src/app/core/interfaces/user.interface';

import { ButtonType } from 'src/app/shared/components/button/button.component';

interface Link {
  label: string;
  redirectTo: string;
  buttonType: ButtonType;
}

export const links: Link[] = [
  {
    label: 'Entrar',
    redirectTo: '/app/auth/login',
    buttonType: 'primary',
  },
  {
    label: 'Criar conta',
    redirectTo: '/app/auth/signup',
    buttonType: 'ghost',
  },
];

export const loggedLinks: Link[] = [
  {
    label: 'Abrir',
    redirectTo: '/app',
    buttonType: 'primary',
  },
];

@Component({
  selector: 'app-header',
  templateUrl: './header.component.html',
  styleUrls: ['./header.component.scss'],
})
export class HeaderComponent {
  user$: Observable<UserStore>;

  links = links;
  isMobileMenuVisible = false;

  constructor(
    private router: Router,
    private store: Store<{ user: UserStore }>,
  ) {
    this.user$ = this.store.select('user');
  }

  ngOnInit() {
    this.user$.subscribe({
      next: (data) => (this.links = data.id ? loggedLinks : links),
    });
  }

  handleMobileMenu() {
    this.isMobileMenuVisible = !this.isMobileMenuVisible;
  }

  redirectUser(url: string) {
    this.router.navigate([url]);
  }
}
