import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
import { forkJoin, of } from 'rxjs';
import { CommonFunctionService } from 'src/app/core/common/common-function.service';
import { ValidationService } from 'src/app/core/common/validation.service';
import { API } from 'src/app/core/services/api.service';

@Component({
  selector: 'app-vehicle-master-language',
  templateUrl: './vehicle-master-language.component.html',
})
export class VehicleMasterLanguageComponent implements OnInit {
    Edit_Id: string;
    VehicleManageLangForm:FormGroup;
  AllLang: any;
  editResp: any;

  
    constructor(public fb: FormBuilder, public validServ: ValidationService, private apiserv: API, private commonMasterSer: CommonFunctionService, private router: Router, private activateRoute: ActivatedRoute) {
  
    }

    ngOnInit(): void {
      this.Edit_Id = this.activateRoute.snapshot.paramMap.get('RuleId') as string;
      this.initform();
      this.GetAll();
  }

  GetAll(){
    forkJoin(
      this.GetAllLAng(),
      this.GetById(this.Edit_Id)
    ).subscribe(res=>{
      if(res && res.length > 0){
        this.AllLang = res[0];
        if(res[1] && res[1].status){
          this.editResp = res[1].data;
          this.VehicleManageLangForm.get('defaultvehicleName').setValue(this.editResp.activityVehicleDetails.vehicleName);
          this.VehicleManageLangForm.get('defaultVehicleDescription').setValue(this.editResp.activityVehicleDetails.vehicleDescription);
          const index = this.AllLang.findIndex(lang => lang.LanguageCode === this.editResp.defaultLanguageCode);
          this.VehicleManageLangForm.get('defaultLanguageCode').setValue(this.AllLang[index].LanguageName);
          if(index !== -1){
              this.AllLang.splice(index, 1);
          }
        }
      }
    })
  }
  GetAllLAng() {
    return this.apiserv.AdminGet('/LanguageMapping/GetLanguagesByOrgId')
  }

  initform() {
    this.VehicleManageLangForm = this.fb.group({
      defaultvehicleName: '',
      defaultVehicleDescription: '',
      defaultLanguageCode: '',
      languageCode: this.validServ.required,
      vehicleName: this.validServ.required,
      vehicleDescription: '',
      activityVehicleDetailsId:''
    })
  } 

  GetById(Editid) {
    if (Editid)
      return this.apiserv.Get('/ActivityVehicle/GetById/' + Editid)
    else
      return of(null)
  };
    
  saveTranslation(){
    if (this.VehicleManageLangForm.status == "INVALID") {
      this.validServ.showValidationsMsg(this.VehicleManageLangForm);
      setTimeout(() => {
        this.validServ.scrollToError();
      }, 50);
    } else if (this.VehicleManageLangForm.status == "VALID") {
      let formVal = this.VehicleManageLangForm.getRawValue();
      let postData = {
        activityVehicleDetailsId:formVal.activityVehicleDetailsId ?? '',
        activityVehicleId:this.Edit_Id,
        languageCode:formVal.languageCode,
        vehicleName:formVal.vehicleName,
        VehicleDescription:formVal.VehicleDescription ?? '',
      }
      this.apiserv.Post('/ActivityVehicle/ManageTranslationActivityVehicleDetails', postData).subscribe(resp=>{
        this.commonMasterSer.showMessage(resp.status, resp.message)
        if(resp.status){
          this.BackButton();
        }
      })
    }
  }

  getDataByLang(){
    let langCode = this.VehicleManageLangForm.get('languageCode').value;
    this.apiserv.Get(`/ActivityVehicle/GetActivityVehicleDetailsByIdAndLangCode/${this.Edit_Id}/${langCode}`).subscribe(res=>{
      if(res && res.data){
        this.VehicleManageLangForm.get('vehicleName').setValue(res.data.vehicleName);
        this.VehicleManageLangForm.get('vehicleDescription').setValue(res.data.vehicleDescription);
        this.VehicleManageLangForm.get('activityVehicleDetailsId').setValue(res.data.activityVehicleDetailsId);
      }
    })
  }

  BackButton() {
    this.router.navigateByUrl(`ActivityConfiguration/VehicleMaster/Search`);
  }
}
