import { Component, EventEmitter, HostListener, Output } from '@angular/core';
import { IgenMenuDto } from 'src/app/_dto/igenMenu.dto';
import { MenuFormatDto } from 'src/app/_dto/menu-format.dto';
import { apiUrl } from 'src/app/_providers/_resources/api-url.properties';
import { MenuService } from 'src/app/_providers/_services/menu.service';
import { routes } from '../../consts/routes';


@Component({
  selector: 'app-sidebar',
  templateUrl: './sidebar.component.html',
  styleUrls: ['./sidebar.component.scss']
})
export class SidebarComponent {
  public routes: typeof routes = routes;
  public isOpenUiElements = false;
  menuData: any;
  model: any;
  seculevel: string = ''
  screenWidth: number;
  @Output() sideBarVisibility = new EventEmitter<boolean>();

  constructor(
    private _menuService: MenuService
  ) {
    this.getMenuData();
    this.getScreenSize();
  }

  async getMenuData() {

    await this._menuService.getJsonData("sales-menu").then((res) => {
      this.menuData = res;
      this.model = this.getMenuCompact(this.menuData, "root");
    },
      (err) => {
      //  console.log("===err====", err);
      });
    // const secuLevel = localStorage.getItem("seculevel");
    // if(secuLevel=="1" || secuLevel=="4"){
    //   await this._menuService.getMenuData(apiUrl.sisUsermenu).then((res) => {
    //     this.menuData = res;
    //     this._menuService.menuData = res;
    //     this.model = this.getMenuCompact(this.menuData, "root");
    //   },
    //     (err) => {
    //       console.log("===err====", err);
    //     });
    // }

  }
  public openUiElements() {
    this.isOpenUiElements = !this.isOpenUiElements;

  }

  @HostListener('window:resize', ['$event'])
  getScreenSize(event?) {
    this.screenWidth = window.innerWidth;
  }

  mediaScreenCheck() {
    if (this.screenWidth < 416) {
      this.sideBarVisibility.emit(true);
    }
  }


  getMenuCompact(menuList: IgenMenuDto[], parentMenuId: string, menuCompactData?: MenuFormatDto[]) {
    if (!parentMenuId)
      return [];
    if (!menuCompactData)
      menuCompactData = [];
    for (let menu of menuList) {
      if (menu.parentMenuId == parentMenuId && menu.menuEnabled == "Y") {
        let format: MenuFormatDto = { menuId: menu.menuId, icon: menu.icon, label: menu.description, langaugeLabelCode: menu.languageLabelCode, data: { description: menu.description, langaugeLabelCode: menu.languageLabelCode, menuId: menu.menuId, rightsGiven: menu.rightsGiven } };
        if (menu["isMegaMenu"]) format["isExpanded"] = true; else format["isExpanded"] = false;
        if (menu["isSideMenu"]) format["isSideMenu"] = true; else format["isSideMenu"] = false;
        if (menu["routerLink"]) format["routerLink"] = menu["routerLink"];
        // if (menu.menuType == "1") format["isExpanded"] = true;
        // if (menu.menuType != "1") format["isExpanded"] = false;
        // if (menu["routerLink"] === this.router.url) this.service.selectedMenu = format;
        if (menu["menuType"]) format["menuType"] = menu["menuType"];
        if (menu.menuType == "1") format["items"] = this.getMenuCompact(menuList, menu.menuId)
        if (menu["role"]) format["role"] = menu["role"];
        if (menu["project"]) format["project"] = menu["project"];
        if (this.seculevel == 'admin')
          menuCompactData.push(format);
        else {
          if (menu.role != 'admin') {
            menuCompactData.push(format);
          }
        }
      }
    }

    return menuCompactData;
  }
  expand(menu) {
    menu.isExpanded = !menu.isExpanded;
  }
}
