import { Component } from '@angular/core';
import { Router } from '@angular/router';

import { AuthService } from '../../services/auth/auth.service';

import { UserDispatchers } from '../../stores/index.dispatchers';

import { IconName } from 'src/app/shared/components/icon/icon.component';

interface Link {
  label: string;
  path: string;
  icon: IconName;
}

@Component({
  selector: 'app-aside',
  templateUrl: './aside.component.html',
  styleUrls: ['./aside.component.scss'],
})
export class AsideComponent {
  confirmLogoutIsOpen = false;
  confirmedLogout = false;

  links: Link[] = [
    {
      label: 'Home',
      path: '/app/home',
      icon: 'home',
    },
    {
      label: 'Refeições',
      path: '/app/meals',
      icon: 'cutlery',
    },
    {
      label: 'Receitas',
      path: '/app/foods',
      icon: 'food',
    },
    {
      label: 'Perfil',
      path: '/app/profile',
      icon: 'person',
    },
  ];

  constructor(
    private route: Router,
    private authService: AuthService,
    private userDispatchers: UserDispatchers,
    private router: Router,
  ) {}

  isFocused(path: string): boolean {
    const currentPath = this.route.url;

    if (currentPath === '/') {
      return path === '/home';
    }
    return currentPath.startsWith(path);
  }

  openLogoutDialog(ev: Event) {
    ev.preventDefault();

    this.confirmLogoutIsOpen = true;
  }

  onConfirmLogout() {
    this.authService.clearAuthorizationToken();
    this.userDispatchers.clearUser();

    this.confirmedLogout = true;
  }

  onCloseAlert() {
    this.router.navigate(['/app/auth/login']);
  }
}
