import { NgFor } from '@angular/common';
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormsModule, ReactiveFormsModule } from '@angular/forms';
import { api } from '@enums/api.enum';
import { Roots } from '@enums/root.enum';
import { SansarikJeevanInformationForm } from '@interfaces/form';
import { IonicModule, NavController } from '@ionic/angular';
import { HttpRequestService } from '@services/http-request.service';
import { SantInfoService } from '@services/sant-info.service';
import { UtilityService } from '@services/utility.service';

@Component({
  selector: 'app-about-sansarik-jeevan',
  templateUrl: './about-sansarik-jeevan.page.html',
  styleUrls: ['./about-sansarik-jeevan.page.scss'],
  standalone: true,
  imports: [IonicModule, FormsModule, ReactiveFormsModule, NgFor]
})
export class AboutSansarikJeevanPage implements OnInit {
  sansarikJeevanInformationForm: FormGroup<SansarikJeevanInformationForm>;
  fatherList = [];
  motherList = [];
  fatherPrevValue: string;
  motherPrevValue: string;

  constructor(
    private navCtrl: NavController,
    private httpRequestService: HttpRequestService,
    public santService: SantInfoService,
    private utilityService: UtilityService
  ) { }

  ngOnInit() {
    this.sansarikJeevanInformationForm = this.santService.sansarikJeevanInfoForm;
  }

  moveToNext(form) {
    if (!form.valid) {
      this.sansarikJeevanInformationForm.markAllAsTouched();
      return;
    }
    this.navCtrl.navigateForward(Roots.page + '/' + Roots.santProfileImage);
  }

  getFatherList(event) {
    this.motherList = [];
    if (!event.value.trim()) {
      this.sansarikJeevanInformationForm.controls.father_name.setValue(null);
      this.sansarikJeevanInformationForm.controls.father_id.setValue(null);
      this.sansarikJeevanInformationForm.controls.father_type.setValue(null);
      return this.fatherList = [];
    }
    const params = {
      name: event.value.trim()
    };

    const isSameStr = this.utilityService.isSameStr(event.value, this.fatherPrevValue, true);
    this.fatherPrevValue = event.value.trim();
    if (!isSameStr) {
      this.httpRequestService.get(api.fatherList, params).then((res: any) => {
        if (event.value) {
          this.fatherList = res.data;
        }
      });
    }

  }

  addFather(father) {
    this.sansarikJeevanInformationForm.controls.father_name.setValue(father.name);
    this.sansarikJeevanInformationForm.controls.father_id.setValue(father.id);
    this.sansarikJeevanInformationForm.controls.father_type.setValue(father.type);
    this.fatherList = [];
  }

  getMotherList(event) {
    this.fatherList = [];
    if (!event.value.trim()) {
      this.sansarikJeevanInformationForm.controls.mother_name.setValue(null);
      this.sansarikJeevanInformationForm.controls.mother_id.setValue(null);
      this.sansarikJeevanInformationForm.controls.mother_type.setValue(null);
      return this.motherList = [];
    }
    const params = {
      name: event.value.trim()
    };
    const isSameStr = this.utilityService.isSameStr(event.value, this.motherPrevValue, true);
    this.motherPrevValue = event.value.trim();
    if (!isSameStr) {
      this.httpRequestService.get(api.motherList, params).then((res: any) => {
        if (event.value) {
          this.motherList = res.data;
        }
      });
    }
  }

  addMother(mother) {
    this.sansarikJeevanInformationForm.controls.mother_name.setValue(mother.name);
    this.sansarikJeevanInformationForm.controls.mother_id.setValue(mother.id);
    this.sansarikJeevanInformationForm.controls.mother_type.setValue(mother.type);
    this.motherList = [];
  }

  moveToBack() {
    this.navCtrl.navigateBack('/' + Roots.page + '/' + Roots.aboutDiksha);
  }

}
