// import { CustomersSelectComponent } from '@/modules/customers/components/select/select.component';
// import { ICustomerResponse } from '@/modules/customers/models';
import { KeyValueComponent } from '@/shared/components';
import { ChangeDetectionStrategy, Component, computed, EventEmitter, inject, Output, signal } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { ButtonDirective } from 'primeng/button';
import { Card } from 'primeng/card';
import images from 'src/assets/images';
import { IPosOrderResponse } from '../../models';
import { PosLandingStore } from '../../store/main.store';
import { PosOrderCustomerDialogComponent } from '../customers/customer-dialog.component';
import { GoldPayloadOrderCardComponent } from '../payloads/gold/order-card.component';
import { StandardPayloadOrderCardComponent } from '../payloads/standard/order-card.component';
import { PosPaymentFormDialogComponent } from '../payment/form-dialog.component';
import { POSOrderPriceInfoCardComponent } from './price-info-card.component';

@Component({
  selector: 'pos-order-section',
  templateUrl: './order-section.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [
    ButtonDirective,
    POSOrderPriceInfoCardComponent,
    FormsModule,
    GoldPayloadOrderCardComponent,
    StandardPayloadOrderCardComponent,
    PosOrderCustomerDialogComponent,
    KeyValueComponent,
    PosPaymentFormDialogComponent,
    Card,
  ],
})
export class PosOrderSectionComponent {
  private readonly store = inject(PosLandingStore);
  @Output() onAddMoreGoods = new EventEmitter<void>();
  @Output() onSuccess = new EventEmitter<IPosOrderResponse>();

  placeholderImage = images.placeholders.default;

  isVisibleCustomerForm = signal(false);
  isVisiblePaymentForm = signal(false);
  isVisiblePreInvoiceForm = signal(false);

  inOrderGoods = computed(() => this.store.inOrderGoods());
  customer = computed(() => this.store.customer());
  customerNameToShow = computed(() => {
    const type = this.store.customer().type;
    const info = this.store.customer().info;
    let customerNameToShow = 'نامشخص';

    switch (type) {
      case 'UNKNOWN':
        customerNameToShow = `${info?.customer_unknown?.name || 'نامشخص'} (نوع دوم)`;
        break;
      case 'INDIVIDUAL':
        customerNameToShow = `${info?.customer_individual ? info?.customer_individual.first_name + ' ' + info?.customer_individual.last_name : 'نامشخص'} (نوع اول)`;
        break;
      case 'LEGAL':
        customerNameToShow = `${info?.customer_legal?.company_name || 'نامشخص'} (نوع اول)`;
        break;
    }

    return customerNameToShow;
  });

  clearOrderList() {
    this.store.resetInOrderGoods();
  }

  submit() {
    this.store.submitOrder();
  }

  openCustomerDialog() {
    this.isVisibleCustomerForm.set(false);
    queueMicrotask(() => this.isVisibleCustomerForm.set(true));
  }

  submitAndPay() {
    this.isVisiblePaymentForm.set(false);
    queueMicrotask(() => this.isVisiblePaymentForm.set(true));
  }

  addMoreGoods() {
    this.onAddMoreGoods.emit();
  }

  submitCustomer() {}
  submitPayment(invoice: IPosOrderResponse) {
    this.onSuccess.emit(invoice);
  }
}
