import { NgFor, NgIf } from '@angular/common';
import { ChangeDetectorRef, Component, OnDestroy, OnInit } from '@angular/core';
import { AvatarPercentageComponent } from '@components/avatar-percentage/avatar-percentage.component';
import { FooterComponent } from '@components/footer/footer.component';
import { api } from '@enums/api.enum';
import { Roots } from '@enums/root.enum';
import { IonicModule, NavController } from '@ionic/angular';
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-sant',
  templateUrl: './create-new-sant.page.html',
  styleUrls: ['./create-new-sant.page.scss'],
  standalone: true,
  imports: [IonicModule, NgIf, NgFor, AvatarPercentageComponent, FooterComponent]
})
export class CreateNewSantPage implements OnInit, OnDestroy {
  suggestionSantList = [];
  createdSantList = [];
  followingSantList = [];
  addSantFlag: boolean;
  santListTotalCount: number;
  suggestedSantListTotalCount: number;
  followingSantListTotalCount: number;
  $subscription: Subscription;
  hasNewReview = 0;

  constructor(
    private navController: NavController,
    private httpRequestService: HttpRequestService,
    private santService: SantInfoService,
    private stateManagementService: StateManagementService,
    private utilityService: UtilityService,
    private change: ChangeDetectorRef
  ) { }

  ngOnInit() {
    this.$subscription = this.stateManagementService.pushNotyObservable.subscribe(res => {
      this.hasNewReview = res.pending_sant_action;
      this.change.detectChanges();
    });
  }

  ionViewWillEnter() {
    this.getCreatedSantList();
    this.getFollowingSantList();
    this.getSuggestionSantList();
    this.santService.updateSant = false;
    this.santService.reviewed_fields = null;
    this.santService.santInformationForm.reset();
  }

  getFollowingSantList() {
    const params = {
      page: 1,
      limit: 3
    };
    this.httpRequestService.post(api.followingSant, params).then((res: any) => {
      this.followingSantList = res.data.data;
      this.followingSantListTotalCount = res.data.total;
    });
  }

  getCreatedSantList() {
    const params = {
      page: 1,
      limit: 3
    };
    this.httpRequestService.post(api.createdByMeSant, params).then((res: any) => {
      this.createdSantList = res.data.data;
      this.santListTotalCount = res.data.total;
      this.addSantFlag = true;
    });
  }

  getSuggestionSantList() {
    const params = {
      page: 1,
      limit: 5
    };
    this.httpRequestService.post(api.suggestionSant, params).then((res: any) => {
      this.suggestionSantList = res.data.data;
      this.suggestedSantListTotalCount = res.data.total;
    });
  }

  santProfile(santId) {
    this.navController.navigateForward(Roots.page + '/' + Roots.santViewProfile.replace(':santId', santId));
  }

  addSant() {
    this.navController.navigateForward(Roots.page + '/' + Roots.santInfo);
  }

  async followSant(event, santData, index) {

    const followSant = () => {
      event.stopPropagation();
      const params = {
        sant_id: santData.id,
      };
      this.httpRequestService.post(api.followSant, params).then(() => {
        this.suggestionSantList.splice(index, 1);
        if (this.suggestionSantList.length === 0) {
          this.getSuggestionSantList();
        }
      });
    };

    if (santData.is_user_following) {
      const isYes = await this.utilityService.presentConfirm(
        'Are you sure you want to unfollow?',
        'No', 'Yes', 'Unfollow'
      );
      if (isYes) {
        followSant();
      }
    } else {
      followSant();
    }
  }

  searchSant() {
    this.navController.navigateForward(Roots.page + '/' + Roots.searchSant);
  }

  viewAllSant(santListType) {
    if (santListType === 1) {
      this.stateManagementService.generalData.pending_sant_action = 0;
      this.stateManagementService.pushNotyObservable.next(this.stateManagementService.generalData);
    }
    this.navController.navigateForward(Roots.page + '/' + Roots.viewAll.replace(':santListType', santListType));
  }

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

}
