import { Component, OnInit } from '@angular/core';
import { AuthService } from '../../../services/auth2.service';
import { Router } from '@angular/router';
import * as CryptoJS from 'crypto-js';

declare const $: any;
declare interface RouteInfo {
    path: string;
    title: string;
    icon: string;
    class: string;
    allowed_roles?:any;
}
export const ROUTES: RouteInfo[] = [
    { path: '/dashboard', title: 'Dashboard',  icon: 'dashboard', class: '' },
    { path: '/assets', title: 'Plantas',  icon: 'wb_sunny', class: '' },
    //{ path: '/profile', title: 'Perfil',  icon:'person', class: '' },
    /*{ path: '/table-list', title: 'Table List',  icon:'content_paste', class: '' },
    { path: '/typography', title: 'Typography',  icon:'library_books', class: '' },
    { path: '/icons', title: 'Icons',  icon:'bubble_chart', class: '' },
    { path: '/maps', title: 'Maps',  icon:'location_on', class: '' },
    { path: '/notifications', title: 'Notifications',  icon:'notifications', class: '' },
    { path: '/upgrade', title: 'Upgrade to PRO',  icon:'unarchive', class: 'active-pro' },*/
];

// Extra options to show to the admin
export const ADMIN_ROUTES: RouteInfo[] = [
    { path: '/organizations', title: 'Organizaciones',  icon:'location_city', class: '', allowed_roles: [2,3] },
    { path: '/plants', title: 'Plantas',  icon: 'poll', class: '', allowed_roles: [2, 3] },
    { path: '/users', title: 'Usuarios',  icon: 'people', class: '', allowed_roles: [3] },
];

@Component({
  selector: 'app-sidebar',
  templateUrl: './sidebar.component.html',
  styleUrls: ['./sidebar.component.scss']
})
export class SidebarComponent implements OnInit {
  menuItems: any[];
  adminMenuItems: any[];
  adminMenu:boolean = false;
  role_number:any;

  constructor(private auth: AuthService, private router: Router) {
    var bytes  = CryptoJS.AES.decrypt(localStorage.getItem("USER_MENU"), 'soma-inverlec-2019');
    this.role_number = bytes.toString(CryptoJS.enc.Utf8);
  }

  ngOnInit() {
    this.menuItems = ROUTES.filter(menuItem => menuItem);
    this.adminMenuItems = ADMIN_ROUTES.filter(menuItem => menuItem);

    // must be changed for the method that returns if it is an admin
    if (this.auth.isLoggedIn() == true && this.role_number > 1) {
      this.adminMenu = true;
    }

  }
  
  isMobileMenu() {
      if ($(window).width() > 991) {
          return false;
      }
      return true;
  };

  logout() {
    this.auth.logout();
    window.location.href="#/login";
    //this.router.navigateByUrl("login");
  };

}
