import { ToastService } from '@/core/services/toast.service';
import { AbstractDialog } from '@/shared/abstractClasses/abstract-dialog';
import { Component, computed, inject } from '@angular/core';
import { RouterLink } from '@angular/router';
import { Button } from 'primeng/button';
import { Drawer } from 'primeng/drawer';
import { Ripple } from 'primeng/ripple';
import { posAboutNamedRoutes } from '../../modules/about/constants';
import { posConfigNamedRoutes } from '../../modules/configs/constants';
import { PosGoodsManagementRoutes } from '../../modules/goodsManagement/constants';
import { posSaleInvoicesNamedRoutes } from '../../modules/saleInvoices/constants';
import { PosShopNamedRoutes } from '../../modules/shop/constants/routes';
import { StatisticsRoutes } from '../../modules/statistics/constants';
import { posSupportNamedRoutes } from '../../modules/support/constants';
import { PosInfoStore } from '../../store';

@Component({
  selector: 'pos-main-menu-sidebar',
  templateUrl: './main-menu-sidebar.component.html',
  imports: [Drawer, Button, Ripple, RouterLink],
})
export class PosMainMenuSidebarComponent extends AbstractDialog {
  private readonly posInfoStore = inject(PosInfoStore);
  // private readonly swUpdate = inject(SwUpdate);
  private readonly toastService = inject(ToastService);

  readonly posInfo = computed(() => this.posInfoStore.entity());
  appVersion = '0.0.0';
  isPwaBuild = false;

  readonly posName = computed(() => {
    if (this.posInfo()) {
      const { name, businessActivity, complex } = this.posInfo()!;
      return `${name} (${businessActivity.name} - ${complex.name})`;
    }
    return '';
  });

  readonly menuItems = [
    {
      label: 'صفحه اصلی',
      routerLink: StatisticsRoutes.statistics.meta.pagePath!(),
      icon: 'pi pi-fw pi-home',
    },
    {
      label: 'فروش',
      routerLink: PosShopNamedRoutes.shop.meta.pagePath!(),
      icon: 'pi pi-fw pi-cart-arrow-down',
    },
    {
      label: 'فاکتورها',
      routerLink: posSaleInvoicesNamedRoutes.saleInvoices.meta.pagePath!(),
      icon: 'pi pi-fw pi-receipt',
    },
    {
      label: 'کالاها',
      routerLink: PosGoodsManagementRoutes.goods.meta.pagePath!(),
      icon: 'pi pi-fw pi-objects-column',
    },
    {
      label: 'تنظیمات',
      routerLink: posConfigNamedRoutes.config.meta.pagePath!(),
      icon: 'pi pi-fw pi-cog',
    },
    {
      label: 'درباره نرم‌افزار',
      routerLink: posAboutNamedRoutes.about.meta.pagePath!(),
      icon: 'pi pi-fw pi-info-circle',
    },
    {
      label: 'پشتیبانی',
      routerLink: posSupportNamedRoutes.support.meta.pagePath!(),
      icon: 'pi pi-fw pi-phone',
    },
  ];

  ngOnInit() {
    // this.isPwaBuild = this.swUpdate.isEnabled;

    if (!this.isPwaBuild) return;

    fetch('/ngsw.json')
      .then((res) => res.json())
      .then((data: { appData?: { appVersion?: string } }) => {
        this.appVersion = data?.appData?.appVersion || this.appVersion;
        this.toastService.info({ text: this.appVersion });
      })
      .catch((err) => {
        console.log('err', err);
      });

    // this.swUpdate.versionUpdates
    //   .pipe(filter((event): event is VersionReadyEvent => event.type === 'VERSION_READY'))
    //   .subscribe((event) => {
    //     const appData = event.latestVersion.appData as { appVersion?: string } | undefined;
    //     this.appVersion = appData?.appVersion || this.appVersion;
    //   });

    // this.swUpdate.checkForUpdate().catch(() => {});
  }
}
