import { AuthService } from '@/core';
import { PageLoadingComponent } from '@/shared/components/page-loading.component';
import { JalaliDateDirective } from '@/shared/directives';
import { Component, computed, inject } from '@angular/core';
import { RouterOutlet } from '@angular/router';
import { MenuItem } from 'primeng/api';
import { ButtonDirective } from 'primeng/button';
import { Card } from 'primeng/card';
import { Menu } from 'primeng/menu';
import images from 'src/assets/images';
import { PosInfoStore, PosProfileStore } from '../store';
import { PosChooseCardsComponent } from './choose-pos.component';

@Component({
  selector: 'pos-layout',
  templateUrl: './layout.component.html',
  imports: [
    PageLoadingComponent,
    RouterOutlet,
    JalaliDateDirective,
    Menu,
    ButtonDirective,
    Card,
    PosChooseCardsComponent,
  ],
})
export class PosLayoutComponent {
  constructor() {}

  private readonly posProfileStore = inject(PosProfileStore);
  private readonly posInfoStore = inject(PosInfoStore);
  private readonly authService = inject(AuthService);

  readonly posProfileLoading = computed(() => this.posProfileStore.loading());
  readonly posProfile = computed(() => this.posProfileStore.entity());
  readonly posProfileError = computed(() => this.posProfileStore.error());

  readonly posInfoLoading = computed(() => this.posInfoStore.loading());
  readonly posInfo = computed(() => this.posInfoStore.entity());
  readonly posInfoError = computed(() => this.posInfoStore.error());

  readonly loading = computed(() => this.posProfileLoading());
  readonly error = computed(() => this.posProfileError());

  logout = () => {
    this.authService.logout();
  };

  profileMenuItems: MenuItem[] = [
    {
      label: 'راهنمای سامانه',
      icon: 'pi pi-question-circle',
      disabled: true,
    },
    {
      label: 'خروج',
      icon: 'pi pi-sign-out',
      command: this.logout,
    },
  ];

  placeholderLogo = images.placeholders.logo;

  now = new Date();

  getData() {
    this.posProfileStore.getData().subscribe({
      next: () => {
        this.posInfoStore.getData().subscribe();
      },
    });
  }

  onChoosePos() {
    this.getData();
  }

  ngOnInit() {
    this.getData();
  }
}
