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

import { FormGroup } from '@angular/forms';
import {
  MgFormControlsAccessor,
  MgControlName,
  MgCustomProperties,
} from './MainRouting.mg.controls.g';

import { TaskBaseMagicComponent, magicProviders } from '@magic-xpa/angular';

import {
  MagicServices, // Add
} from '@magic-xpa/angular';
import {
  ChangeDetectorRef, // Add
  ViewContainerRef,
} from '@angular/core';
import { Router } from '@angular/router';
import { Input } from '@angular/core';

import { IconSetService } from '@coreui/icons-angular';
import { cilListNumbered, cilPaperPlane, brandSet,cilMenu } from '@coreui/icons';

import { FileDownloaderService } from "../../../msj/file-downloader.service";
import { FilePreviewService } from "../../../msj/file-preview.service";

@Component({
  selector: 'mga-MainRouting',
  providers: [...magicProviders],
  templateUrl: './MainRouting.component.html',
})
export class MainRouting extends TaskBaseMagicComponent {
  mgc = MgControlName;
  mgcp = MgCustomProperties;
  mgfc!: MgFormControlsAccessor;
  override createFormControlsAccessor(formGroup: FormGroup) {
    this.mgfc = new MgFormControlsAccessor(formGroup, this.magicServices);
  }


  @Input() sidebarId: string = 'sidebar';
  constructor(
    ref: ChangeDetectorRef,
    magicServices: MagicServices,
    protected vcRef: ViewContainerRef,
    public router: Router,
    public iconSet: IconSetService
  ) {
    super(ref, magicServices);
    iconSet.icons = { cilListNumbered, cilPaperPlane, ...brandSet ,cilMenu};
  }

  FileDownload=(base64:string,FileName:string)=>{
    FileDownloaderService.FileDownload(base64,FileName);
  }


  PDFPreview=(base64:string)=>{
    FilePreviewService.PreviewPDF(base64);
  }


  
  MenuItems: any;
  loginParam: boolean;
  Menu = (strBLOB: any) => {
    this.MenuItems = JSON.parse(strBLOB).Menus;
    this.loginParam = location.pathname != '/';
  };

  MenuChange = (MenuItem: string, event) => {
    var sidebarNode = document.getElementsByClassName(
      'header-toggler'
    )[0] as HTMLElement;
    sidebarNode.click();

    console.log(MenuItem.replace(/^\/+/, ''));
    // this.router.navigate([MenuItem]);
    this.mg.setValueToControl(this.mgc.V_RoutingProgramName,MenuItem.replace(/^\/+/, ''))
    this.mg.simulateClick(this.mgc.V_RotuingButton);
  };


  Login = (param: String) => {
    if (param != undefined && param != '') {
      this.router.navigate(['/' + param]);
    }
  };

  URL = (): string => {
    return window.location.pathname.trim();
  };

  visible: any = [];

  toggleCollapse(i): void {
    this.visible[i] = !this.visible[i];
  }

  
}
