import { NgFor, NgIf } from '@angular/common';
import { Component, OnDestroy, OnInit } from '@angular/core';
import { FooterComponent } from '@components/footer/footer.component';
import { JoinSanghButtonComponent } from '@components/join-sangh-button/join-sangh-button.component';
import { api } from '@enums/api.enum';
import { GJainStorage } from '@enums/gjain-storage.enum';
import { Roots } from '@enums/root.enum';
import { SanghData, UserData } from '@interfaces/common';
import { IonicModule, NavController } from '@ionic/angular';
import { AlertService } from '@services/alert.service';
import { HttpRequestService } from '@services/http-request.service';
import { SantInfoService } from '@services/sant-info.service';
import { StateManagementService } from '@services/state-management.service';
import { UtilityService } from '@services/utility.service';
import { Subscription } from 'rxjs';

@Component({
  selector: 'app-create-new-sangh',
  templateUrl: './create-new-sangh.page.html',
  styleUrls: ['./create-new-sangh.page.scss'],
  standalone: true,
  imports: [IonicModule, NgIf, NgFor, JoinSanghButtonComponent, FooterComponent]
})
export class CreateNewSanghPage implements OnInit, OnDestroy {
  sanghList: Array<SanghData> = [];
  inReviewSangh: Array<SanghData> = [];
  suggestedSanghList: Array<SanghData> = [];
  followedSanghList: Array<SanghData> = [];
  noSanghFlag: boolean;
  userData: UserData;
  sanghListTotalCount: number;
  inReviewSanghTotalCount: number;
  suggestedSanghListTotalCount: number;
  followedSanghListTotalCount: number;
  $subscription: Subscription;
  hasNewInvite = 0;

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

  ngOnInit() {
    this.$subscription = this.stateManagementService.pushNotyObservable.subscribe(res => {
      this.hasNewInvite = res.pending_sangh_invitation;
    });
  }

  async ionViewWillEnter() {
    this.userData = await this.stateManagementService.storage.get(GJainStorage.userData);
    this.santService.sanghInformationForm.reset();
    this.getMemberSanghList();
    this.getFollowedSanghList();
    this.getSuggestedSanghList();
    this.getInReviewSangh();
  }

  getMemberSanghList() {
    const params = {
      page: 1,
      limit: 3
    };
    this.httpRequestService.post(api.sanghList, params).then((res: any) => {
      this.sanghList = res.data.data;
      this.sanghListTotalCount = res.data.total;
      this.noSanghFlag = true;
    });
  }

  getInReviewSangh() {
    const params = {
      page: 1,
      limit: 3
    };
    this.httpRequestService.post(api.inReviewUserSanghList, params).then((res: any) => {
      this.inReviewSangh = res.data.data;
      this.inReviewSanghTotalCount = res.data.total;
    });
  }

  getFollowedSanghList() {
    const params = {
      page: 1,
      limit: 3
    };
    this.httpRequestService.post(api.followedSanghList, params).then((res: any) => {
      this.followedSanghList = res.data.data;
      this.followedSanghListTotalCount = res.data.total;
      this.noSanghFlag = true;
    });
  }

  getSuggestedSanghList() {
    const params = {
      page: 1,
      limit: 5
    };
    this.httpRequestService.post(api.suggestedSanghList, params).then((res: any) => {
      this.suggestedSanghList = res.data.data;
      this.suggestedSanghListTotalCount = res.data.total;
    });
  }

  addSangh() {
    this.navCtrl.navigateForward(Roots.page + '/' + Roots.sanghTermPolicy);
  }

  sanghProfile(sanghId: number) {
    this.navCtrl.navigateForward(Roots.page + '/' + Roots.sanghProfile.replace(':sanghId', String(sanghId)));
  }

  viewAllSangh(sanghListType) {
    this.navCtrl.navigateForward(Roots.page + '/' + Roots.viewAllSangh.replace(':sanghListType', sanghListType));
  }

  sanghSearch() {
    this.navCtrl.navigateForward(Roots.page + '/' + Roots.sanghSearchMember);
  }

  invitations() {
    this.stateManagementService.generalData.pending_sangh_invitation = 0;
    this.stateManagementService.pushNotyObservable.next(this.stateManagementService.generalData);
    this.navCtrl.navigateForward(Roots.page + '/' + Roots.invitations);
  }

  async deleteSangh(id: number) {
    const isYes = await this.utilityService.presentConfirm(
      `Are you sure want to delete Sangh?`,
      'No', 'Yes'
    );
    if (isYes) {
      this.httpRequestService.delete(api.deleteSangh + id).then(() => {
        this.alert.showToaster('Sangh deleted successfully');
        this.getInReviewSangh();
      });
    }
  }

  ngOnDestroy() {
    this.$subscription?.unsubscribe();
  }

}
