import { NgFor, NgIf } from '@angular/common';
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { api } from '@enums/api.enum';
import { GJainStorage } from '@enums/gjain-storage.enum';
import { Roots } from '@enums/root.enum';
import { CommonResponse, GlobalSearchDataShravak, UserData } 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-friend-list',
  templateUrl: './friend-list.page.html',
  styleUrls: ['./friend-list.page.scss'],
  standalone: true,
  imports: [IonicModule, NgFor, NgIf]
})
export class FriendListPage implements OnInit {
  friendList = [];
  pageNumber = 1;
  totalPage = 1;
  noDataFlag: boolean;
  userData: UserData;

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

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

  async ionViewWillEnter() {
    this.userData = await this.stateManagementService.storage.get(GJainStorage.userData);
  }

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

  getFriendList(isFirstLoad, event) {
    if (this.pageNumber <= this.totalPage) {
      this.httpRequestService.get(
        api.userFriendList.replace('{id}', this.cRoute.snapshot.paramMap.get('id')), {
        page: this.pageNumber,
        limit: 15
      }).then((res: any) => {
        this.totalPage = res.data.last_page;
        this.noDataFlag = true;
        this.friendList.push(...res.data.data);
        if (isFirstLoad) { event.target.complete(); }
        this.pageNumber++;
      });
    } else {
      event.target.complete();
    }
  }

  sendConnectionRequest(id: number, data: GlobalSearchDataShravak) {
    this.httpRequestService.post(api.connectionRequestSend, { receiver_id: id })
      .then((res: CommonResponse) => {
        data.is_friends = 2;
        this.alert.showToaster(res.message);
      });
  }

  acceptDeclineRequest(id: number, data: GlobalSearchDataShravak, status: number, type?: number) {
    if (status === 1) {
      this.httpRequestService.post(api.connectionRequestStatusUpdate, { receiver_id: id, status })
        .then((res: CommonResponse) => {
          data.is_friends = 1;
          this.alert.showToaster(res.message);
        });
    } else {
      this.confirmationAlert(id, data, type);
    }
  }

  async confirmationAlert(id, data: GlobalSearchDataShravak, type: number) {
    let message = '';
    if (type === 2) {
      message = 'Are you sure about to cancel this request?';
    } else if (type === 1) {
      message = 'Are you sure you want to remove User ' + data.name + ' from your connection?';
    }
    const isYes = await this.utilityService.presentConfirm(message);
    if (isYes) {
      this.httpRequestService.post(api.connectionRequestStatusUpdate, { receiver_id: id, status: 2 })
        .then((res: CommonResponse) => {
          data.is_friends = 0;
          this.alert.showToaster(res.message);
        });
    }
  }

  moveToProfile(id) {
    if (id === this.userData.id) {
      this.navCtrl.navigateForward(Roots.page + '/' + Roots.shravakViewProfile);
    } else {
      this.navCtrl.navigateForward([Roots.page + '/' + Roots.shravakOtherProfile, { id }]);
    }
  }

}
