import {
  ChangeDetectionStrategy,
  Component,
  computed,
  inject,
  input,
  signal,
} from '@angular/core';
import { provideLoggerContext } from '@isa/core/logging';
import {
  canReturnReceiptItem,
  ProductCategory,
  ReceiptItem,
  ReturnDetailsService,
  ReturnDetailsStore,
} from '@isa/oms/data-access';
import { IconButtonComponent } from '@isa/ui/buttons';
import {
  CheckboxComponent,
  DropdownButtonComponent,
  DropdownOptionComponent,
} from '@isa/ui/input-controls';
import { FormsModule } from '@angular/forms';

@Component({
  selector: 'oms-feature-return-details-order-group-item-controls',
  templateUrl: './return-details-order-group-item-controls.component.html',
  styleUrls: ['./return-details-order-group-item-controls.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
  standalone: true,
  imports: [
    CheckboxComponent,
    IconButtonComponent,
    DropdownButtonComponent,
    DropdownOptionComponent,
    FormsModule,
  ],
  providers: [
    provideLoggerContext({
      component: 'ReturnDetailsOrderGroupItemControlsComponent',
    }),
  ],
})
export class ReturnDetailsOrderGroupItemControlsComponent {
  #returnDetailsService = inject(ReturnDetailsService);
  #store = inject(ReturnDetailsStore);

  item = input.required<ReceiptItem>();

  selected = this.#store.getItemSelected(this.item);

  productCategoryChanged = signal<ProductCategory>(ProductCategory.Unknown);

  canReturnRequest = computed(() => {
    const productCategory = this.productCategoryChanged();
    if (productCategory === ProductCategory.Unknown) {
      return undefined;
    }
    return this.item();
  });

  canReturnResource = this.#store.canReturnResource(this.canReturnRequest);

  availableCategories = this.#returnDetailsService.availableCategories();

  selectedQuantity = computed(() => {
    const item = this.item();
    return this.#store.selectedQuantityMap()[item.id];
  });

  /**
   * Generates the list of selectable quantities for the dropdown.
   * The values range from 1 up to the available quantity.
   *
   * @returns An array of selectable quantity values.
   */
  quantityDropdownValues = computed(() => {
    const item = this.item();
    const itemQuantity = this.#store.availableQuantityMap()[item.id];
    return Array.from({ length: itemQuantity }, (_, i) => i + 1);
  });

  productCategory = computed(() => {
    const item = this.item();
    return this.#store.itemCategoryMap()[item.id];
  });

  selectable = this.#store.isSelectable(this.item);

  canReturnReceiptItem = computed(() => canReturnReceiptItem(this.item()));

  dropdownLabel = computed(() => {
    const category = this.productCategory();
    return !!category && category !== ProductCategory.Unknown
      ? category
      : 'Produktart';
  });

  setProductCategory(category: ProductCategory | undefined) {
    if (!category) {
      category = ProductCategory.Unknown;
    }
    this.#store.setProductCategory(this.item().id, category);
    this.productCategoryChanged.set(category);
  }

  setQuantity(quantity: number | undefined) {
    const item = this.item();
    if (quantity === undefined) {
      quantity = this.#store.availableQuantityMap()[item.id];
    }
    this.#store.setQuantity(this.item().id, quantity);
  }

  setSelected(selected: boolean) {
    if (selected) {
      this.#store.addSelectedItems([this.item().id]);
    } else {
      this.#store.removeSelectedItems([this.item().id]);
    }
  }
}
