import { NgIf } from '@angular/common';
import { ChangeDetectorRef, Component, Input, OnInit } from '@angular/core';
import { Roots } from '@enums/root.enum';
import { IonicModule, NavController } from '@ionic/angular';
import { StateManagementService } from '@services/state-management.service';

@Component({
  selector: 'app-message-notification-badge',
  templateUrl: './message-notification-badge.component.html',
  styleUrls: ['./message-notification-badge.component.scss'],
  standalone: true,
  imports: [IonicModule, NgIf]
})
export class MessageNotificationBadgeComponent implements OnInit {
  @Input() type: string;
  messageIndObservable = 0;
  pushNotyObservable = 0;

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

  ngOnInit() {
    this.pushNotyObservable = this.stateManagementService.generalData.unread_notification_count;
    this.messageIndObservable = this.stateManagementService.generalData.unread_message_count;
    this.stateManagementService.pushNotyObservable.subscribe(res => {
      this.pushNotyObservable = res.unread_notification_count;
      this.messageIndObservable = res.unread_message_count;
      this.change.detectChanges();
    });
  }

  redirect(type: string) {
    this.navCtrl.navigateForward([`${Roots.page}/${type === 'messages' ? Roots.message : type === 'search' ?
      Roots.globalSearch : Roots.notification}`]);
  }

}
