import { NgClass, NgFor, NgIf } from '@angular/common';
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
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 { UtilityService } from '@services/utility.service';

@Component({
  selector: 'app-thana-list',
  templateUrl: './thana-list.page.html',
  styleUrls: ['./thana-list.page.scss'],
  standalone: true,
  imports: [IonicModule, NgIf, NgFor, NgClass]
})
export class ThanaListPage implements OnInit {
  santId: number;
  pageNumber = 1;
  totalPage = 1;
  thanaList: any[] = [];
  dharmaId = '1';
  is_leader_exist = false;
  constructor(
    private httpRequestService: HttpRequestService,
    private activatedRoute: ActivatedRoute,
    private navCtrl: NavController,
    private router: Router,
    private utilityService: UtilityService
  ) {
    this.dharmaId = this.router.getCurrentNavigation().extras.state?.dharmaId;
  }

  ngOnInit() {
    this.santId = Number(this.activatedRoute.snapshot.paramMap.get('santId'));
  }

  ionViewWillEnter() {
    this.pageNumber = 1;
    this.totalPage = 1;
    this.thanaList = [];
    this.getThanaList(false, '');
  }

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

  getThanaList(isFirstLoad, event) {
    if (this.pageNumber <= this.totalPage) {
      this.httpRequestService.post(api.santThanaList, {
        sant_id: Number(this.santId),
        page: this.pageNumber
      }).then((res: any) => {
        this.totalPage = res.data.last_page;
        this.is_leader_exist = res.is_leader_exist;
        this.thanaList.push(...res.data.data);
        if (isFirstLoad) { event.target.complete(); }
        this.pageNumber++;
      });
    } else {
      event.target.complete();
    }
  }

  addThana() {
    const navOption = {
      state: {
        santId: this.santId,
        dharmaId: this.dharmaId
      },
    };
    this.navCtrl.navigateForward(Roots.page + '/' + Roots.addThana, navOption);
  }

  makeLeader(i, thanaSantId) {
    const params = {
      sant_id: Number(this.santId),
      thana_sant_id: thanaSantId
    };
    this.httpRequestService.post(api.makeLeader, params).then(() => {
      this.thanaList[i].pivot.is_leader = 1;
      this.is_leader_exist = true;
    });
  }

  async deleteThana(i, thana) {
    const isYes = await this.utilityService.presentConfirm(
      'Are you sure you want to delete?',
      'No', 'Yes', 'Delete Thana'
    );
    if (isYes) {
      const params = {
        sant_id: Number(this.santId),
        thana_sant_id: thana?.id
      };
      this.httpRequestService.delete(api.deleteThana, params).then(() => {
        this.thanaList.splice(i, 1);
        if (thana.pivot.is_leader === 1) {
          this.is_leader_exist = false;
        }
      });
    }
  }

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