import { DatePipe, NgFor, NgIf } from '@angular/common';
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { api } from '@enums/api.enum';
import { constants } from '@enums/constants.enum';
import { GJainStorage } from '@enums/gjain-storage.enum';
import { Roots } from '@enums/root.enum';
import { CommonResponse, Privacy, ProfileData, ProfileResponse } from '@interfaces/common';
import { IonicModule, NavController } from '@ionic/angular';
import { AlertService } from '@services/alert.service';
import { HttpRequestService } from '@services/http-request.service';
import { StateManagementService } from '@services/state-management.service';
import { UtilityService } from '@services/utility.service';

@Component({
  selector: 'app-shravak-about',
  templateUrl: './shravak-about.page.html',
  styleUrls: ['./shravak-about.page.scss'],
  standalone: true,
  imports: [IonicModule, NgIf, NgFor, DatePipe]
})
export class ShravakAboutPage implements OnInit {
  userProfileData: ProfileData;
  fromFlag = '';
  relationArray = ['', 'Son', 'Daughter'];
  privacyList: Privacy[] = constants.privacy;

  constructor(
    private router: Router,
    private navCtrl: NavController,
    private stateManagementService: StateManagementService,
    private httpRequestService: HttpRequestService,
    private alert: AlertService,
    private utilityService: UtilityService
  ) { }

  async ngOnInit() {
    if (this.router.getCurrentNavigation().extras.state.from) {
      this.fromFlag = this.router.getCurrentNavigation().extras.state.from;
    }
    this.userProfileData = await this.stateManagementService.storage.get(GJainStorage.profileData);
  }

  ionViewWillEnter() {
    this.getProfileInfo();
    this.stateManagementService.userInformation = {};
  }

  getProfileInfo() {
    this.httpRequestService.get(api.shravakProfile).then((res: ProfileResponse) => {
      this.userProfileData = res.data;
      this.stateManagementService.userProfileData = res.data;
      this.stateManagementService.storage.set(GJainStorage.profileData, res.data);
    });
  }

  editBasicInfo() {
    this.navCtrl.navigateForward(Roots.page + '/' + Roots.shravakEditProfile);
  }

  editLocationInfo() {
    this.navCtrl.navigateForward(Roots.page + '/' + Roots.shravakEditLocation);
  }

  editContactInfo() {
    this.navCtrl.navigateForward(Roots.page + '/' + Roots.shravakEditContactInfo);
  }

  editWorkInfo(data) {
    const navOption = {
      state: {
        workData: data,
      },
    };
    this.navCtrl.navigateForward(Roots.page + '/' + Roots.shravakEditWork, navOption);
  }

  addWorkInfo() {
    this.navCtrl.navigateForward(Roots.page + '/' + Roots.shravakEditWork);
  }

  addEducationInfo(type) {
    this.navCtrl.navigateForward([Roots.page + '/' + Roots.shravakEditEducation, {
      type,
    }]);
  }

  editQualificationInfo(data, type) {
    const navOption = {
      state: {
        qualificationData: data,
        type
      },
    };
    this.navCtrl.navigateForward(Roots.page + '/' + Roots.shravakEditEducation, navOption);
  }

  addFamilyMember() {
    this.navCtrl.navigateForward(Roots.page + '/' + Roots.shravakEditFamilyMembers);
  }

  addChild() {
    this.stateManagementService.userInformation.id = null;
    this.navCtrl.navigateForward(Roots.page + '/' + Roots.addChild);
  }

  editChild(childData) {
    const navOption = {
      state: {
        childData
      },
    };
    this.navCtrl.navigateForward(Roots.page + '/' + Roots.addChild, navOption);
  }

  async deleteFamilyMember(id: number, index: number) {
    const isYes = await this.utilityService.presentConfirm(
      'Are you sure about delete this member from your family?',
      'No', 'Yes', 'Delete Member'
    );
    if (isYes) {
      const params = {
        from_id: id,
        status: 2,
      };
      this.httpRequestService.post(api.relationshipRequestChange, params).then((res: any) => {
        this.userProfileData.family_members.splice(index, 1);
        this.alert.showToaster(res.message);
      });
    }
  }

  editFamilyMember(data: any) {
    const navOption = {
      state: {
        familyData: data,
      },
    };
    this.navCtrl.navigateForward(Roots.page + '/' + Roots.shravakEditFamilyMembers, navOption);
  }

  deleteQualification(id: number, index: number, type: number) {
    this.presentAlert(id, index, type, 1);
  }

  deleteWork(id: number, index: number) {
    this.presentAlert(id, index, '', 2);
  }

  deleteChild(id: number, index: number) {
    this.presentAlert(id, index, '', 3);
  }

  async presentAlert(id: number, index: number, type: any, status: number) {
    const message =
      status === 1 ? 'Are you sure about delete this qualification detail?' :
        status === 2 ? 'Are you sure about delete this work detail?' : 'Are you sure about delete this child?';

    const isYes = await this.utilityService.presentConfirm(message);
    if (isYes) {
      if (status === 1) {
        this.httpRequestService.post(api.deleteQualification, { qualification_id: id })
          .then((res: CommonResponse) => {
            if (type === 1) {
              this.userProfileData.qualificaion_details.colleges.splice(index, 1);
            } else {
              this.userProfileData.qualificaion_details.schools.splice(index, 1);
            }
            this.alert.showToaster(res.message);
          });
      } else if (status === 2) {
        this.httpRequestService.post(api.deleteWork, { work_id: id }).then((res: CommonResponse) => {
          this.userProfileData.company_details.splice(index, 1);
          this.alert.showToaster(res.message);
        });
      } else {
        this.httpRequestService.delete(api.deleteChild.replace('{id}', String(id))).then((res: CommonResponse) => {
          this.userProfileData.passive_users.splice(index, 1);
          this.alert.showToaster(res.message);
        });
      }
    }
  }

  address(type: string) {
    if (type === 'native') {
      this.utilityService.openGoogleMap(this.userProfileData.native_latitude, this.userProfileData.native_longitude);
    } else if (type === 'current') {
      this.utilityService.openGoogleMap(this.userProfileData.latitude, this.userProfileData.longitude);
    }
  }

}
