import { Component, Inject, OnInit } from '@angular/core';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { EncService } from '../../../../services/enc.service';
import { ResourcesService } from '../../../../services/resources.service';

@Component({
    selector: 'app-locate-car-area',
    templateUrl: './locate-car-area.component.html',
    styleUrl: './locate-car-area.component.css',
    standalone: false
})
export class LocateCarAreaComponent implements OnInit{
  viewAreas = ['Vista lateral', 'Vista superior', 'Vista inferior'];
  viewIndex = 0;
  carType = '';
  areaIndicator = '';

  constructor(
    @Inject(MAT_DIALOG_DATA) private _data: any,
    private _dialogRef: MatDialogRef<LocateCarAreaComponent>,
    private _encService: EncService,
    private _resourcesService: ResourcesService,
  ) { }

  ngOnInit(): void {
    let trainOccupationArr = this._data.trainOccupation.split('-');
    if(trainOccupationArr[0] == 'RC1' || trainOccupationArr[0] == 'RC2'){
      this.carType = 'RC';
    }else if(trainOccupationArr[0] == 'N1' || trainOccupationArr[0] == 'N2' || trainOccupationArr[0] == 'N3' 
    || trainOccupationArr[0] == 'N4' || trainOccupationArr[0] == 'N5' || trainOccupationArr[0] == 'N6'){
      this.carType = 'N';
      let trainNumberStr = trainOccupationArr[0].replace('N', '');
      if(trainNumberStr == '1' || trainNumberStr == '6'){
        this.areaIndicator = '2';
      }else if(trainNumberStr == '2' || trainNumberStr == '5'){
        this.areaIndicator = '3';
      }else if(trainNumberStr == '3' || trainNumberStr == '4'){
        this.areaIndicator = '4';
      }
    }else if(trainOccupationArr[0] == 'R'){
      this.carType = 'R';
    }else{
      this._resourcesService.openSnackBar(`La ocupación de tren ${this._data.trainOccupation} es inválida.`);
      this._dialogRef.close(false);;
    }
  }

  goForward(){
    let indTmp = this.viewIndex + 1;
    if(indTmp >= this.viewAreas.length){
      this.viewIndex = 0;
    }else{
      this.viewIndex++;
    }
  }

  goBackward(){
    let indTmp = this.viewIndex - 1;
    if(indTmp < 0){
      this.viewIndex = this.viewAreas.length - 1;
    }else{
      this.viewIndex--;
    }
  }

  async returnTrainArea(areaStr: string){
    let areaEnc = await this._encService.encrypt(areaStr);
    this._dialogRef.close(areaEnc);
  }
}
