import { NgIf } from '@angular/common';
import { Component, Input } from '@angular/core';
import { api } from '@enums/api.enum';
import { Roots } from '@enums/root.enum';
import { IonicModule, NavController, PopoverController } 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-message-popover',
  templateUrl: './message-popover.component.html',
  styleUrls: ['./message-popover.component.scss'],
  standalone: true,
  imports: [IonicModule, NgIf]
})
export class MessagePopoverComponent {
  @Input() shravakId: number;
  @Input() messageExist = Boolean(false);

  constructor(
    private httpService: HttpRequestService,
    private alert: AlertService,
    private stateManagementService: StateManagementService,
    private popoverController: PopoverController,
    private navCtrl: NavController,
    private utilityService: UtilityService
  ) { }

  goToShravakProfile() {
    this.dismiss();
    this.navCtrl.navigateForward([Roots.page + '/' + Roots.shravakOtherProfile, { id: this.shravakId }]);
  }

  async clearMessages() {
    const isYes = await this.utilityService.presentConfirm('Are you sure you want to clear this conversation?');
    if (isYes) {
      const res: any = await this.httpService.delete(api.clearMessage + '/' + this.shravakId.toString(), {});
      this.alert.showToaster(res.message);
      this.stateManagementService.messageClearObservable.next(+this.shravakId);
    }
  }

  async deleteThread() {
    const isYes = await this.utilityService.presentConfirm('Are you sure you want to remove this conversation?');
    if (isYes) {
      const res: any = await this.httpService.delete(api.deleteMessageThread + '/' + this.shravakId.toString(), {});
      this.alert.showToaster(res.message);
      this.stateManagementService.messageThreadRemoveObservable.next(this.shravakId);
      this.navCtrl.navigateForward([Roots.page + '/' + Roots.message]);
    }
  }

  async dismiss() {
    await this.popoverController.dismiss();
  }

}
