
import { NgFor, NgIf } from '@angular/common';
import { Component, NgZone } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { api } from '@enums/api.enum';
import { Roots } from '@enums/root.enum';
import { CommonResponse, FriendList } from '@interfaces/common';
import { IonicModule, NavController, PopoverController } 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-search-friend',
  templateUrl: './search-friend.page.html',
  styleUrls: ['./search-friend.page.scss'],
  standalone: true,
  imports: [IonicModule, FormsModule, NgIf, NgFor]
})
export class SearchFriendPage {
  friendList: Array<FriendList> = [];
  pageNumber = 1;
  totalPage = 1;
  totalCount: number;
  spinnerFlag = false;
  searchValue = '';
  prevValue: string;
  constructor(
    private httpRequestService: HttpRequestService,
    private alert: AlertService,
    private navCtrl: NavController,
    private popoverCtrl: PopoverController,
    private ngZone: NgZone,
    private utilityService: UtilityService,
  ) { }

  ionViewWillEnter() {
    this.getFriendList(false, '');
  }

  getFriendList(isFirstLoad, event) {
    this.spinnerFlag = false;
    if (this.pageNumber <= this.totalPage) {
      this.httpRequestService.get(api.myFriendList, {
        page: this.pageNumber,
        limit: 15,
        name: this.searchValue.trim()
      }).then((res: CommonResponse) => {
        if (isFirstLoad) { event.target.complete(); }
        if (!event) { this.friendList = []; }
        this.totalPage = res.data.last_page;
        this.totalCount = res.data.total;
        this.friendList.push(...res.data.data);
        this.spinnerFlag = true;
        this.pageNumber++;
      });
    } else {
      event.target.complete();
      this.spinnerFlag = false;
    }
  }

  loadFriendListData($event) {
    this.getFriendList(true, $event);
  }

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

  moveToMessage(data: FriendList) {
    this.popoverCtrl.dismiss().then(() => {
      this.ngZone.run(() => this.navCtrl.navigateForward([Roots.page + '/' + Roots.messageChat, {
        userId: data.id,
        name: data.name
      }])).then();
    });
  }

  async confirmationAlert(data: FriendList, index: number) {
    const message = 'Are you sure you want to remove User ' + data.name + ' from your connection?';
    const isYes = await this.utilityService.presentConfirm(message);
    if (isYes) {
      this.popoverCtrl.dismiss().then(() => {
        this.httpRequestService.post(api.connectionRequestStatusUpdate, { receiver_id: data.id, status: 2 })
          .then((res: CommonResponse) => {
            this.friendList.splice(index, 1);
            this.alert.showToaster(res.message);
          });
      });
    }
  }

  searchFriend() {
    const isSameStr = this.utilityService.isSameStr(this.searchValue, this.prevValue);
    this.prevValue = this.searchValue;
    if (!isSameStr) {
      this.friendList = [];
      this.pageNumber = 1;
      this.totalPage = 1;
      this.getFriendList(false, '');
    }
  }

}
