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

@Component({
  selector: 'app-friends-suggestions-view-all',
  templateUrl: './friends-suggestions-view-all.page.html',
  styleUrls: ['./friends-suggestions-view-all.page.scss'],
  standalone: true,
  imports: [IonicModule, NgFor, NgIf]
})
export class FriendsSuggestionsViewAllPage {
  pageNumber = 1;
  totalPage = 1;
  totalCount: number;
  suggestedFriendList = [];
  constructor(
    private httpRequestService: HttpRequestService,
    private alert: AlertService,
    private navCtrl: NavController
  ) { }

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

  getSuggestionFriendList(isFirstLoad, event) {
    if (this.pageNumber <= this.totalPage) {
      this.httpRequestService.post(api.suggestionShravakList, {
        page: this.pageNumber,
        limit: 10
      }).then((res: any) => {
        this.totalPage = res.data.last_page;
        this.totalCount = res.data.total;
        this.suggestedFriendList.push(...res.data.data);
        if (isFirstLoad) { event.target.complete(); }
        this.pageNumber++;
      });
    } else {
      event.target.complete();
    }
  }

  loadData($event) {
    this.getSuggestionFriendList(true, $event);
  }

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

  sendFriendRequest(id: number, data: any, index: number) {
    this.httpRequestService.post(api.connectionRequestSend, { receiver_id: id })
      .then((res: CommonResponse) => {
        data.is_friends = 2;
        this.suggestedFriendList.splice(index, 1);
        this.alert.showToaster(res.message);
      });
  }

}
