import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { AlertController, IonicModule, NavController } from '@ionic/angular';

import { NgFor, NgIf } from '@angular/common';
import { api } from '@enums/api.enum';
import { Roots } from '@enums/root.enum';
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-chaturmas-info',
  templateUrl: './chaturmas-info.page.html',
  styleUrls: ['./chaturmas-info.page.scss'],
  standalone: true,
  imports: [IonicModule, NgIf, NgFor]
})
export class ChaturmasInfoPage implements OnInit {

  santId = '1';
  chaturmasList = [];
  pageNumber = 1;
  totalPage = 1;
  noDataFlag: boolean;
  dharmaId: string;
  sampradayId: string;
  thanaMemberCount: string;

  constructor(
    private httpRequestService: HttpRequestService,
    private alert: AlertService,
    private activatedRoute: ActivatedRoute,
    private alertController: AlertController,
    private navCtrl: NavController,
    private stateManagementService: StateManagementService,
    private router: Router,
    private utilityService: UtilityService
  ) { }

  ngOnInit() {
    this.dharmaId = this.router.getCurrentNavigation().extras.state?.dharmaId;
    this.sampradayId = this.router.getCurrentNavigation().extras.state?.sampradayId;
    this.thanaMemberCount = this.router.getCurrentNavigation().extras.state?.thanaMemberCount;
    this.santId = this.activatedRoute.snapshot.paramMap.get('santId');
    this.getChaturmasYear();
  }

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

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

  getChaturmasYear() {
    this.httpRequestService.get(api.chaturmasYear).then((res: any) => {
      this.stateManagementService.chaturmasYear = res.data;
    });
  }

  addChaturmas() {
    const navOption = {
      state: {
        santId: this.santId,
        isUpdate: false,
        dharmaId: this.dharmaId,
        sampradayId: this.sampradayId,
        thanaMemberCount: this.thanaMemberCount
      },
    };
    this.navCtrl.navigateForward(Roots.page + '/' + Roots.addChaturmas, navOption);
  }

  getChaturmasList(isFirstLoad, event) {
    if (this.pageNumber <= this.totalPage) {
      this.httpRequestService.get(api.chaturmasList, {
        sant_id: this.santId,
        page: this.pageNumber
      }).then((res: any) => {
        this.totalPage = res.data.last_page;
        this.noDataFlag = true;
        this.chaturmasList.push(...res.data.data);
        if (isFirstLoad) { event.target.complete(); }
        this.pageNumber++;
      });
    } else {
      event.target.complete();
    }
  }

  async chaturmasReport(id: any) {
    const alert = await this.alertController.create({
      cssClass: 'chaturmas-report',
      header: 'Add Change Request',
      message: 'If you feel that Chaturmas is incorrect then please write to us so we can review and update it!',
      inputs: [
        {
          name: 'chaturmas',
          type: 'text',
          placeholder: 'Chaturmas detail'
        },
      ],
      buttons: [
        {
          text: 'Submit',
          handler: (data) => {
            const params = {
              comment: data.chaturmas,
              sant_id: this.santId
            };
            this.httpRequestService.post(api.reviewRequestChaturmas.replace('{chaturmas_id}', id), params)
              .then((res: any) => {
                this.alert.showToaster(res.message);
              });
          }
        }, {
          text: 'Cancel',
          role: 'cancel',
          cssClass: 'secondary',
        }
      ]
    });
    await alert.present();
  }

  address(chaturmas) {
    this.utilityService.openGoogleMap(chaturmas.latitude, chaturmas.longitude);
  }

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