import { DatePipe, NgClass, NgFor, NgIf } from '@angular/common';
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { api } from '@enums/api.enum';
import { GJainStorage } from '@enums/gjain-storage.enum';
import { Roots } from '@enums/root.enum';
import { UserData } from '@interfaces/common';
import { IonicModule, NavController } from '@ionic/angular';
import { HttpRequestService } from '@services/http-request.service';
import { StateManagementService } from '@services/state-management.service';
import { UtilityService } from '@services/utility.service';

@Component({
  selector: 'app-sant-bio',
  templateUrl: './sant-bio.page.html',
  styleUrls: ['./sant-bio.page.scss'],
  standalone: true,
  imports: [IonicModule, NgIf, NgFor, DatePipe, NgClass]
})
export class SantBioPage implements OnInit {
  santId = '';
  santDetails: any;
  age: number;
  userData: UserData;

  constructor(
    private activatedRoute: ActivatedRoute,
    private httpRequestService: HttpRequestService,
    private stateManagementService: StateManagementService,
    private navCtrl: NavController,
    private utilityService: UtilityService
  ) { }

  ngOnInit() {
    this.santId = this.activatedRoute.snapshot.paramMap.get('santId');
    if (this.stateManagementService.santDetails) {
      this.santDetails = this.stateManagementService.santDetails;
    } else {
      this.getSantDetails();
    }
  }

  async ionViewWillEnter() {
    this.userData = await this.stateManagementService.storage.get(GJainStorage.userData);
  }

  getSantDetails() {
    this.httpRequestService.get(api.santInfo + '/' + this.santId).then((res: any) => {
      this.santDetails = res.data;
      this.stateManagementService.santDetails = res.data;
    });
  }

  moveToSant(santId) {
    if (santId) {
      this.navCtrl.navigateForward(Roots.page + '/' + Roots.santViewProfile.replace(':santId', santId));
    }
  }

  moveToFamily(user) {
    if ((user.length > 0) && (user[0].relation_type === 1)) {
      const id = user[0].user.id;
      this.navCtrl.navigateForward([Roots.page + '/' + Roots.shravakOtherProfile, { id }]);
    } else if ((user.length > 0) && (user[0].relation_type === 2)) {
      this.navCtrl.navigateForward(Roots.page + '/' + Roots.santViewProfile.replace(':santId', user[0].sant.id));
    }
  }

  moveToProfile(id) {
    if (id !== this.userData.id && id !== 1) {
      this.navCtrl.navigateForward([Roots.page + '/' + Roots.shravakOtherProfile, { id }]);
    }
  }

  address(element) {
    this.utilityService.openGoogleMap(element.diksha_place_latitude, element.diksha_place_longitude);
  }

}
