import { HttpClient } from '@angular/common/http';
import { Component, Inject, OnInit } from '@angular/core';
import { MAT_DIALOG_DATA, MatDialogRef } from "@angular/material/dialog";
import { DatePipe } from "@angular/common";
import { ConfigService } from '@services/config.service';

@Component({
  selector: 'app-show-organizational-unit',
  templateUrl: './show-organizational-unit.component.html',
  styleUrl: './show-organizational-unit.component.css'
})
export class ShowOrganizationalUnitComponent implements OnInit {
  baseUrl: string;
  displayedColumns: string[] = ['property', 'value'];
  currentCalendar: any;
  ou: any;
  generalData: any[] = [];
  networkData: any[] = [];
  datePipe: DatePipe = new DatePipe('es-ES');
  typeTranslations: { [key: string]: string } = {
    'organizational-unit': 'Centro',
    'classrooms-group': 'Grupo de aulas',
    'classroom': 'Aula',
    'clients-group': 'Grupo de clientes'
  };

  constructor(
    @Inject(MAT_DIALOG_DATA) public data: any,
    private dialogRef: MatDialogRef<ShowOrganizationalUnitComponent>,
    private configService: ConfigService,
    private http: HttpClient
  ) {
    this.baseUrl = this.configService.apiUrl;
  }

  ngOnInit(): void {
    this.loadOrganizationalUnit(this.data.data['@id']);
  }

  isBoolean(value: any): boolean {
    return typeof value === 'boolean';
  }

  loadOrganizationalUnit(uuid: string): void {
    const apiUrl = `${this.baseUrl}${uuid}`;
    this.http.get<any>(apiUrl).subscribe(
      response => {
        this.ou = response;
        console.log('Organizational unit', this.ou);
        this.initializeData();
      },
      error => {
        console.error('Error loading organizational unit', error);
        this.initializeData();
      });
  }

  loadCurrentCalendar(uuid: string): void {
    const apiUrl = `${this.baseUrl}/remote-calendars/${uuid}`;
    this.http.get<any>(apiUrl).subscribe(
      response => {
        this.currentCalendar = response.name;
        this.initializeData();
      },
      error => {
        console.error('Error loading current calendar', error);
        this.initializeData();
      }
    );
  }

  initializeData(): void {
    this.generalData = [
      { property: 'Nombre', value: this.ou.name },
      { property: 'Uuid', value: this.ou.uuid },
      { property: 'Descripción', value: this.ou.description },
      { property: 'Comentarios', value: this.ou.comments },
      { property: 'Tipo', value: this.typeTranslations[this.ou.type] },
      { property: 'Unidad organizativa superior', value: this.ou.parent ? this.ou.parent.name : '-' },
      { property: 'Creado el', value: this.datePipe.transform(this.ou.createdAt, 'dd/MM/yyyy hh:mm:ss') },
      { property: 'Calendario asociado', value: this.ou.remoteCalendar ? this.currentCalendar : '-' },
      { property: 'Aforo', value: this.ou.capacity },
      { property: 'Localización', value: this.ou.location },
      { property: 'Calendario', value: this.ou.calendar ? this.ou.calendar.name : '-' },
      { property: 'Proyector', value: this.ou.projector },
      { property: 'Pizarra', value: this.ou.board },
    ];

    this.networkData = [
      { property: 'Proxy', value: this.ou.networkSettings.proxy },
      { property: 'DNS', value: this.ou.networkSettings.dns },
      { property: 'Router', value: this.ou.networkSettings.router },
      { property: 'NTP', value: this.ou.networkSettings.ntp },
      { property: 'Modo P2P', value: this.ou.networkSettings.p2pMode },
      { property: 'Tiempo P2P', value: this.ou.networkSettings.p2pTime },
      { property: 'Mcast IP', value: this.ou.networkSettings.mcastIp },
      { property: 'Mcast Speed', value: this.ou.networkSettings.mcastSpeed },
      { property: 'Mcast Port', value: this.ou.networkSettings.mcastPort },
      { property: 'Mcast Mode', value: this.ou.networkSettings.mcastMode },
      { property: 'Menú', value: this.ou.networkSettings.menu ? this.ou.networkSettings.menu.name : '-' },
      { property: 'OgLive', value: this.ou.networkSettings.ogLive ? this.ou.networkSettings.ogLive.name : '-' },
      { property: 'Repositorio', value: this.ou.networkSettings.repository ? this.ou.networkSettings.repository.name : '-' },
      { property: 'OgShare', value: this.ou.networkSettings.ogshare },
      { property: 'Perfil de hardware', value: this.ou.networkSettings.hardwareProfile ? this.ou.networkSettings.hardwareProfile.name : '-' },
      { property: 'Máscara de red', value: this.ou.networkSettings.netmask },
    ]
  }

  onNoClick(): void {
    this.dialogRef.close();
  }
}
