import { NgClass, NgIf } from '@angular/common';
import { ChangeDetectorRef, Component, OnDestroy, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { Roots } from '@enums/root.enum';
import { GeneralData } from '@interfaces/common';
import { IonicModule, NavController } from '@ionic/angular';
import { StateManagementService } from '@services/state-management.service';
import { Subscription } from 'rxjs';

@Component({
  selector: 'app-footer',
  templateUrl: './footer.component.html',
  styleUrls: ['./footer.component.scss'],
  standalone: true,
  imports: [IonicModule, NgClass, NgIf]
})
export class FooterComponent implements OnInit, OnDestroy {
  activeTab: string;
  $subscription: Subscription;
  data: GeneralData;
  iconBasePath = '/assets/images/';

  constructor(
    private navCtrl: NavController,
    private router: Router,
    private stateManagementService: StateManagementService,
    private change: ChangeDetectorRef
  ) { }

  ngOnInit() {
    if (this.router.url.startsWith('/page/shravak-feeds')) {
      this.activeTab = 'feed';
    } else if (this.router.url.startsWith('/page/create-new-sant')) {
      this.activeTab = 'sant';
    } else if (this.router.url.startsWith('/page/create-new-sangh')) {
      this.activeTab = 'sangh';
    } else if (this.router.url.startsWith('/page/friend-request')) {
      this.activeTab = 'friendRequest';
    } else if (this.router.url.startsWith('/page/menu')) {
      this.activeTab = 'menu';
    }

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

  redirect(type: string) {
    this.activeTab = type;
    if (type === 'feed') {
      this.navCtrl.navigateRoot([Roots.page + '/' + Roots.shravakFeeds]);
    } else if (type === 'sangh') {
      this.stateManagementService.generalData.sangh_menu = 0;
      this.stateManagementService.pushNotyObservable.next(this.stateManagementService.generalData);
      this.navCtrl.navigateRoot([Roots.page + '/' + Roots.createNewSangh]);
    } else if (type === 'sant') {
      this.stateManagementService.generalData.sant_menu = 0;
      this.stateManagementService.pushNotyObservable.next(this.stateManagementService.generalData);
      this.navCtrl.navigateRoot([Roots.page + '/' + Roots.createNewSant]);
    } else if (type === 'menu') {
      this.navCtrl.navigateRoot([Roots.page + '/' + Roots.menu]);
    } else if (type === 'friendRequest') {
      this.stateManagementService.generalData.shravak_menu = 0;
      this.stateManagementService.pushNotyObservable.next(this.stateManagementService.generalData);
      this.navCtrl.navigateRoot([Roots.page + '/' + Roots.friendRequest]);
    }
  }

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

}
