import { Component } from '@angular/core';
import { api } from '@enums/api.enum';
import { Roots } from '@enums/root.enum';
import { CommonResponse } from '@interfaces/common';
import { AlertController, IonicModule, NavController } from '@ionic/angular';
import { AlertService } from '@services/alert.service';
import { HttpRequestService } from '@services/http-request.service';
import { UtilityService } from '@services/utility.service';

@Component({
  selector: 'app-settings',
  templateUrl: './settings.page.html',
  styleUrls: ['./settings.page.scss'],
  standalone: true,
  imports: [IonicModule]
})
export class SettingsPage {

  constructor(
    private navController: NavController,
    private httpRequestService: HttpRequestService,
    private alert: AlertService,
    private alertController: AlertController,
    private utilityService: UtilityService
  ) { }

  goToBlockedUsers() {
    this.navController.navigateForward([Roots.page + '/' + Roots.settings + '/' + Roots.blockedUsers]);
  }

  async deleteAccount() {
    const header = 'Delete User Account';
    const message = `Once you delete your account, all of your information on Global Jain will be lost forever.
     We will not be able to restore your account.
     Are you sure you want to proceed?`;
    const isYes = await this.utilityService.presentConfirm(
      message,
      'No', 'Yes', header
    );
    if (isYes) {
      this.deleteConfirm();
    }
  }

  async deleteConfirm() {
    const header = 'Delete User Account';
    const message = 'Are you sure about delete your account?';
    const isYes = await this.utilityService.presentConfirm(
      message,
      'No', 'Yes', header
    );
    if (isYes) {
      this.alert.showLoader();
      this.httpRequestService.post(api.deleteUserAccount + '?t=1', {}).then((res: CommonResponse) => {
        this.utilityService.logout();
        this.alert.showToaster(res.message);
        this.alert.hideLoader();
      }).catch((err) => {
        this.alert.hideLoader();
        if (err.error && err.error.status === 403) {
          this.deleteInfoPopup(err.error.message);
        }
      });
    }
  }

  async deleteInfoPopup(message) {
    const alert = await this.alertController.create({
      cssClass: 'chaturmas-approval',
      backdropDismiss: false,
      message,
      buttons: [{
        text: 'Ok',
        role: 'cancel'
      }]
    });
    await alert.present();
  }

}
