import { SubmoduleFunctionsComponent } from './submodule-functions/submodule-functions.component';
import { MatDialog } from '@angular/material/dialog';
import { NavItem } from './../../../interfaces/nav-item.interface';
import { Component, OnInit, Input, ViewChild, ElementRef } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { Location } from '@angular/common';
import { MatSnackBar } from '@angular/material/snack-bar';
import { EncService } from 'src/app/services/enc/enc.service';

@Component({
  selector: 'app-menu-item',
  templateUrl: './menu-item.component.html',
  styleUrls: ['./menu-item.component.css']
})
export class MenuItemComponent implements OnInit {
  route: string;
  module: string;
  icon: string;
  hasSubmodules: boolean;
  submodules: any[];
  hasMenus: boolean;
  menus: any[];

  constructor(
    private _activeRoute: ActivatedRoute,
    private _location: Location,
    private _snackBar: MatSnackBar,
    private _encService: EncService,
    private router: Router,
    private _dialog: MatDialog,
  ) { 
    this.route = "";
    this.module = "";
    this.icon = "";
    this.hasSubmodules = false;
    this.hasMenus = false;

    this.submodules = [];
    this.menus = [];
  }

  ngOnInit(): void {
    this._activeRoute.queryParams.subscribe(res => {
      let infoEnc = res['info'];
      if(infoEnc == undefined || infoEnc == null || infoEnc == ""){
        this.openSnackBar('No se encontró información');
        this._location.back();
      }else{
        this.mapData(infoEnc);
      }
    })
  }

  openSnackBar(msg: string){
    this._snackBar.open(msg, 'Cerrar', {
      duration: 5000
    });
  }

  async mapData(data: string){
    this.menus = [];
    this.submodules = [];

    let infoDec = await this._encService.desencriptar(data);
    let infoObj = JSON.parse(infoDec);

    this.module = infoObj.module.NOMBREMODULO;
    this.icon = infoObj.module.ICONOMODULO;
    this.route = infoObj.route;
    this.hasSubmodules = infoObj.module.SUBMODULES.length > 0;
    this.hasMenus = infoObj.module.MENUS.length > 0;

    if(this.hasSubmodules){
      infoObj.module.SUBMODULES.forEach((element: any) => {
        this.submodules.push(element);
      });
    }

    if(this.hasMenus){
      infoObj.module.MENUS.forEach((element: any) => {
        this.menus.push(element);
      });
    }
  }

  updateAvailableFunctions(submodule: any){
    const dialogRef = this._dialog.open(SubmoduleFunctionsComponent, {
      data: submodule
    });

    dialogRef.afterClosed().subscribe(result => {
      if(result != "" && result != null && result != undefined){
        this.navigate(result.IDFUNCION, result, true, submodule, submodule.IDSUBMODULO)
      }
    });
  }
  
  async navigate(idMenu: string, menu: any, hasSubmodule: boolean, submodule?: any, idSubmodule?: string){
    if(hasSubmodule){
      let subRoute = idSubmodule?.substring(10, idMenu.length);
      let menuRoute = idMenu.substring(10, idMenu.length);
  
      let infoObj = {
        modIcon: this.icon,
        modName: this.module,
        modRoute: this.route,
        submodule: submodule,
        menu: menu
      };
  
      let infoStr = JSON.stringify(infoObj);
      let infoEnc = await this._encService.encriptar(infoStr);

      this.router.navigate([`sam/${this.route}/${subRoute}/${menuRoute}`], {
        queryParams: {
          info: infoEnc,
        }
      });
    }else{
      let menuRoute = idMenu.substring(10, idMenu.length);
  
      let infoObj = {
        modIcon: this.icon,
        modName: this.module,
        modRoute: this.route,
        submodule: submodule,
        menu: menu
      };
  
      let infoStr = JSON.stringify(infoObj);
      let infoEnc = await this._encService.encriptar(infoStr);
      
      this.router.navigate([`sam/${this.route}/${menuRoute}`], {
        queryParams: {
          info: infoEnc,
        }
      });
    }
  }
}
