import { ChangeDetectionStrategy, Component, EventEmitter, Input, Output, inject } from '@angular/core';
import { PickupShelfDetailsStore } from '../../store';
import { Group, groupBy } from '@ui/common';
import { AsyncPipe } from '@angular/common';
import { ProductImageModule } from '@cdn/product-image';
import { UiSliderModule } from '@ui/slider';
import { UiSpinnerModule } from '@ui/spinner';
import { PickupShelfProcessingStatusPipe } from '../pipes/processing-status.pipe';
import { DBHOrderItemListItemDTO } from '@generated/swagger/oms-api';
import { PickupShelfCoverCompartmentCodePipe } from '../pipes/cover-compartment-code.pipe';
import { PickupShelfShowCoverCompartmentCodePipe } from '../pipes/show-cover-compartment-code.pipe';

@Component({
  selector: 'page-pickup-shelf-details-covers',
  templateUrl: 'pickup-shelf-details-covers.component.html',
  styleUrls: ['pickup-shelf-details-covers.component.scss'],
  host: { class: 'page-pickup-shelf-details-covers' },
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [
    AsyncPipe,
    ProductImageModule,
    UiSliderModule,
    UiSpinnerModule,
    PickupShelfProcessingStatusPipe,
    PickupShelfCoverCompartmentCodePipe,
    PickupShelfShowCoverCompartmentCodePipe
],
})
export class PickUpShelfDetailsCoversComponent {
  private _store = inject(PickupShelfDetailsStore);

  @Input() selectedOrderItem: DBHOrderItemListItemDTO;

  fetching$ = this._store.fetchingCoverOrderItems$;

  @Output()
  coverClick = new EventEmitter<DBHOrderItemListItemDTO[]>();

  @Input() coverItems: DBHOrderItemListItemDTO[];

  get groupedItems() {
    let groupedItems = groupBy(this.coverItems, this.groupByProcessingStatus);

    // Nach Gruppierung das Group property für die Gesamtsumme der Menge nehmen
    groupedItems.forEach((group) => (group.group = `${group.items.reduce((sum, item) => sum + item.quantity, 0)}`));

    // Sortierung nach der Menge
    groupedItems.sort(this.sortByQuantity);

    // Summe der overallQuantity hinzufügen
    groupedItems.forEach((group) => (group.group += ` / ${group.items[0]?.overallQuantity}`));

    // Items einer Gruppe anhand des readyForPickup Datums sortieren
    groupedItems.forEach(
      (group) =>
        (group.items = group.items.sort(
          (a, b) => new Date(b.readyForPickUp).getTime() - new Date(a.readyForPickUp).getTime(),
        )),
    );

    return groupedItems;
  }

  constructor() {}

  groupByProcessingStatus(item: DBHOrderItemListItemDTO) {
    // Eingetroffene anhand compartmentCode und EAN gruppieren
    if (item.processingStatus === 128) {
      return `${item.processingStatus}_${item.compartmentCode}_${item.product.ean}_${item.orderItemId}`;
    }

    if ([256, 512, 1024, 2048, 4096, 262144, 524288, 1048576].includes(item.processingStatus)) {
      return `${item.processingStatus}_${item.product.ean}_${item.orderItemId}`;
    }

    return `${item.orderItemSubsetId}_${item.product.ean}`;
  }

  sortByQuantity(a: Group<string, DBHOrderItemListItemDTO>, b: Group<string, DBHOrderItemListItemDTO>) {
    if (+a.group < +b.group) {
      return -1;
    }
    if (+a.group > +b.group) {
      return 1;
    }
    return 0;
  }
}
