import { DatePipe, NgFor, NgIf } from '@angular/common';
import { Component, ElementRef, OnInit, ViewChild } from '@angular/core';
import { FormGroup, FormsModule, ReactiveFormsModule } from '@angular/forms';
import { GjDatetimeDirective } from '@directives/gj-datetime.directive';
import { api } from '@enums/api.enum';
import { Roots } from '@enums/root.enum';
import { DikshaInformationForm } from '@interfaces/form';
import { IonicModule, NavController } from '@ionic/angular';
import { AlertService } from '@services/alert.service';
import { HttpRequestService } from '@services/http-request.service';
import { SantInfoService } from '@services/sant-info.service';
import { UtilityService } from '@services/utility.service';
import { format, parseISO } from 'date-fns';

@Component({
  selector: 'app-about-diksha',
  templateUrl: './about-diksha.page.html',
  styleUrls: ['./about-diksha.page.scss'],
  standalone: true,
  imports: [IonicModule, FormsModule, ReactiveFormsModule, GjDatetimeDirective, NgIf, NgFor, DatePipe]
})
export class AboutDikshaPage implements OnInit {
  @ViewChild('personalLocation') personalLocation: ElementRef;
  dikshaInformationForm: FormGroup<DikshaInformationForm>;
  santList = [];
  currentDate = this.formatDate((new Date()).toISOString());
  dikshaDate = '';
  minDate: any;
  prevValue: string;

  constructor(
    private navCtrl: NavController,
    private httpRequestService: HttpRequestService,
    public santService: SantInfoService,
    private alert: AlertService,
    public utilityService: UtilityService
  ) {
    if (this.santService.santInfoForm.controls.birth_date.value) {
      this.minDate = this.santService.santInfoForm.controls.birth_date.value;
    }
  }

  ngOnInit() {
    this.dikshaInformationForm = this.santService.dikshaInfoForm;
    if (this.dikshaInformationForm) {
      this.dikshaDate = this.dikshaInformationForm.controls.diksha_date.value;
    }
  }

  formatDate(value) {
    return format(parseISO(value), 'yyyy-MM-dd');
  }

  moveToNext(form) {
    if (form.value.diksha_date) {
      this.dikshaInformationForm.controls.diksha_date.setValue(this.formatDate(form.value.diksha_date));
    }
    if (this.dikshaInformationForm.controls.guru.value && !this.dikshaInformationForm.controls.guru_id.value) {
      this.alert.showToaster('Please select guru from existing sant list.');
      return;
    }
    if (!form.valid) {
      this.dikshaInformationForm.markAllAsTouched();
      return;
    }
    this.navCtrl.navigateForward(Roots.page + '/' + Roots.aboutSansarikJeevan);
  }
  moveToBack() {
    this.navCtrl.navigateBack(Roots.page + '/' + Roots.santInfo);
  }

  getSantList(event) {
    if (!event.value.trim()) {
      return this.santList = [];
    }
    const isSameStr = this.utilityService.isSameStr(event.value, this.prevValue, true);
    this.prevValue = event.value;
    if (!isSameStr) {
      const params = {
        name: event.value.trim(),
        page: 1,
        limit: 10,
        dharma_id: this.santService.santInfoForm.controls.dharma_id.value,
        sampraday_id: this.santService.santInfoForm.controls.sampraday_id.value.id,
        sant_id: this.santService.updateSantId
      };
      this.httpRequestService.get(api.santList, params).then((res: any) => {
        if (event.value) {
          this.santList = res.data.data;
          if (res.data.data.length === 0) {
            this.dikshaInformationForm.controls.guru_id.setValue('');
          }
        }
      });
    }
  }

  addSant(sant) {
    this.dikshaInformationForm.controls.guru.setValue(sant.name);
    this.dikshaInformationForm.controls.guru_id.setValue(sant.id);
    this.santList = [];
  }

  initAutocomplete(): void {
    this.utilityService.createAutocomplete(this.personalLocation.nativeElement).subscribe((details) => {
      this.dikshaInformationForm.controls.diksha_place.setValue(this.personalLocation.nativeElement.value);
      this.dikshaInformationForm.controls.diksha_place_latitude.setValue(details.geometry.location.lat());
      this.dikshaInformationForm.controls.diksha_place_longitude.setValue(details.geometry.location.lng());
    });
  }

  removeLatAndLong() {
    this.dikshaInformationForm.controls.diksha_place_latitude.reset();
    this.dikshaInformationForm.controls.diksha_place_longitude.reset();
  }

}
