import { Injectable } from '@angular/core';
import { OrderItemListItemDTO, OrderDTO } from '@swagger/oms';
import { ShelfOrder } from '../models/shelf-order.model';
import { isNullOrUndefined } from 'util';
import { CollectingShelfOrder } from '../models/collecting-shelf-order.model';
import { CollectingShelfService } from '../services/collecting-shelf.service';

export const orderStatusMapper: { [id: number]: string } = {
  0: '-', // 'Not Set'
  1: 'bestellt', // 'neu' 'Ordering'
  2: '-', // 'Placed'
  4: '-', // 'Accepted'
  8: '-', // 'Parked'
  16: 'bestellt', // 'In Process'
  32: '-', // 'Preparation For Shipping'
  64: 'versendet', // 'Dispatched'
  128: 'eingetroffen', // 'Arrived'
  256: 'abgeholt', // 'Fetched'
  512: 'storniert (Kunde)', // 'Canceled By Buyer'
  1024: 'storniert', // 'Canceled By Retailer'
  2048: 'storniert (Lieferant)', // 'Canceled By Supplier'
  4096: 'nicht lieferbar', // 'Not Available'
  8192: 'nachbestellt', // 'ReOrdered'
  16384: '-', // 'Returned By Buyer'
  32768: '-', // 'Available For Download'
  65536: '-', // 'Downloaded'
  131072: '-', // 'Not Fetched'
  262144: 'ans Lager (nicht abgeholt)', // 'Back To Stock'
  524288: 'angefragt', // 'Requested'
  1048576: 'weitergeleitet intern', // 'Redirected Internally'
  2097152: '-', // 'Overdue'
  4194304: 'zugestellt', // 'Delivered'
  8388608: 'Lieferant wird ermittelt', // 'Determine Supplier'
  16777216: 'derzeit nicht lieferbar', // 'Supplier Temporarily OutOfStock'
  33554432: 'reserviert', // 'Reserved'
  67108864: '-', // 'Assembled'
  134217728: '-', // 'Packed'
};

export const activeOrderStatusMapper: { [id: number]: string } = {
  16: 'bestellt', // 'In Process'
  64: 'versendet', // 'Dispatched'
  128: 'eingetroffen', // 'Arrived'
  256: 'abgeholt', // 'Fetched'
  512: 'storniert (Kunde)', // 'Canceled By Buyer'
  1024: 'storniert', // 'Canceled By Retailer'
  2048: 'storniert (Lieferant)', // 'Canceled By Supplier'
  4096: 'nicht lieferbar', // 'Not Available'
  8192: 'nachbestellt', // 'ReOrdered'
  262144: 'ans Lager (nicht abgeholt)', // 'Back To Stock'
  524288: 'angefragt', // 'Requested'
  1048576: 'weitergeleitet intern', // 'Redirected Internally'
  4194304: 'zugestellt', // 'Delivered'
  8388608: 'Lieferant wird ermittelt', // 'Determine Supplier'
  16777216: 'derzeit nicht lieferbar', // 'Supplier Temporarily OutOfStock'
  33554432: 'reserviert', // 'Reserved'
};

export enum OrderStatus {
  InProcess = 16, // 'bestellt'
  Dispatched = 64, // 'versendet'
  Arrived = 128, // 'eingetroffen'
  Fetched = 256, // 'abgeholt'
  CanceledByBuyer = 512, // 'storniert (Kunde)'
  CanceledByRetailer = 1024, // 'storniert'
  CanceledBySupplier = 2048, // 'storniert (Lieferant)'
  NotAvailable = 4096, // 'nicht lieferbar'
  ReOrdered = 8192, // 'nachbestellt'
  BackToStock = 262144, // 'ans Lager (nicht abgeholt)'
  Requested = 524288, // 'angefragt'
  RedirectedInternally = 1048576, // 'weitergeleitet intern'
  SupplierTemporarilyOutOfStock = 16777216, // derzeit nicht lieferbar
}

export const orderDetailStatus: { [id: number]: string } = {
  0: '-',
  1: 'neu', // 'Ordering'
  2: 'bestellt', // 'InProcess'
  3: 'eingetroffen', // 'Arrived'
};

export const orderChannelMapper: { [id: number]: string } = {
  0: '-', // 'NotSet'
  1: '', // 'System'
  2: 'Filiale', // 'Branch'
  4: 'HSC', // 'CallCenter'
  8: 'Internet', // 'Online'
  16: 'Mobil', // 'Mobile'
  32: '', // 'BackOffice'
};

export const orderPaymentType: { [id: number]: string } = {
  0: 'NotSet',
  1: 'Bei Abholung', // 'WhenCollecting',
  2: 'Kostenfrei', // 'Free',
  4: 'Barzahlung', // 'Cash',
  8: 'Einzugsermächtigung', // 'DirectDebit',
  16: 'Dauerhafte Einzugsermächtigung', // 'DebitAdviceMandate',
  31: 'Debit Karte', // 'DebitCard',
  64: 'Kreditkarte', // 'CreditCard',
  128: 'Rechnung', // 'Invoice',
  256: 'Vorauskasse', // 'PrePayment',
  512: 'Gutschein', // 'Voucher',
  1024: 'Sammelrechnung', // 'CollectiveInvoice',
  2048: 'PayPal', // 'PayPal',
  4096: 'Sofortüberweisung', // 'InstantTransfer',
  8192: 'Nachnahme', // 'PayOnDelivery',
  16384: 'Kundenkarte', // 'BonusCard',
};

export const receiptType: { [id: number]: string } = {
  0: 'NotSet',
  1: 'Lieferschein', // 'ShippingNote',
  2: 'Gutschrift', // 'CreditNote',
  4: 'Sammellieferschein', // 'CollectiveShippingNote',
  8: 'Sammelgutschrift', // 'CollectiveCreditNote',
  16: 'Sammellieferschein für Kundenkartenkäufe', // 'BonusCardCollectiveShippingNote',
  31: 'Sammelgutschrift für Kundenkartenkäufe', // 'BonusCardCollectiveCreditNote',
  64: 'Zahlungsbeleg', // 'PaymentReceipt',
  128: 'Rechnung', // 'Invoice',
  256: 'Sammelrechnung', // 'CollectiveInvoice',
  512: 'Proformarechnung', // 'ProformaInvoice',
};

@Injectable({ providedIn: 'root' })
export class ShelfMapping {
  fromOrderItemListItemDTOArrayToCollectingShelfOrder(
    orders: OrderItemListItemDTO[],
    collectingShelfService: CollectingShelfService,
    firstBatch: boolean
  ): CollectingShelfOrder[] {
    if (isNullOrUndefined(orders) || orders.length < 1) {
      return [];
    }
    return orders.map((order, index) => {
      const hideCompartmentNumber = collectingShelfService.processOrderCompartmentNumber(orders, order, index, firstBatch);
      return <CollectingShelfOrder>{
        orderId: order.orderId,
        orderItemId: order.orderItemId,
        orderItemSubsetId: order.orderItemSubsetId,
        orderPId: order.orderPId,
        orderItemPId: order.orderItemPId,
        orderItemSubsetPId: order.orderItemSubsetPId,
        shopName: order.shopName,
        orderBranchId: order.orderBranchId,
        orderNumber: order.orderNumber,
        orderType: order.orderType,
        processingStatus: order.processingStatus,
        orderDate: order.orderDate,
        product: order.product,
        quantity: order.quantity,
        overallQuantity: order.overallQuantity,
        price: order.price,
        currency: order.currency,
        buyerNumber: order.buyerNumber,
        organisation: order.organisation,
        gender: order.gender,
        title: order.title,
        lastName: order.lastName,
        firstName: order.firstName,
        specialComment: order.specialComment,
        nextProcessingStatus: order.nextProcessingStatus,
        ssc: order.ssc,
        sscText: order.sscText,
        supplier: order.supplier,
        compartmentCode: order.compartmentCode,
        compartmentInfo: order.compartmentInfo,
        hideCompartmentNumber: hideCompartmentNumber,
        isPrebooked: order.isPrebooked,
      };
    });
  }

  fromOrderDTOtoShelfOrder(order: OrderDTO): ShelfOrder {
    if (isNullOrUndefined(order)) {
      return;
    }

    return <ShelfOrder>{
      id: order.id,
      created: order.created,
      changed: order.changed,
      version: order.version,
      status: order.status,
      pId: order.pId,
      orderType: order.orderType,
      clientChannel: order.clientChannel,
      orderNumber: order.orderNumber,
      orderDate: order.orderDate,
      acceptanceDate: order.acceptanceDate,
      orderBranch: order.orderBranch,
      processingStatus: order.processingStatus,
      completedDate: order.completedDate,
      items: order.items,
      buyer: order.buyer,
      buyerComment: order.buyerComment,
      notificationChannels: order.notificationChannels,
      shippingAddress: order.shippingAddress,
      targetBranch: order.targetBranch,
      logistician: order.logistician,
      payer: order.payer,
      paymentType: order.paymentType,
      paymentComment: order.paymentComment,
      payment: order.payment,
      invoiceText: order.invoiceText,
      paymentStatus: order.paymentStatus,
      paymentReferenceNumber: order.paymentReferenceNumber,
      agentComment: order.agentComment,
      specialAgreements: order.specialAgreements,
      validationStatus: order.validationStatus,
      termsOfDelivery: order.termsOfDelivery,
      campaignCode: order.campaignCode,
      orderValue: order.orderValue,
      orderValueCurrency: order.orderValueCurrency,
      itemsCount: order.itemsCount,
    };
  }
}
