import { RemissionModuleOptions } from './remission-module.options';
import { NgModule, ModuleWithProviders } from '@angular/core';
import { RestRemissionService } from './services/rest-remission.service';
import { MockRemissionService } from './mock/mock-remission.service';
import { Mapper } from './mappings/mapper';
import { ReturnSuggestionDtoToRemissionProductMapping } from './mappings/return-suggestion-dto-to-remission-product.mapping';
import { ItemDtoToProductMapping } from './mappings/item-dto-to-product.mapping';
import { ProductToPriceDTOMapping } from './mappings/product-to-price-dto';
import { ProductToProductDTOMapping } from './mappings/product-to-product-dto';
import { ReturnItemDtoToRemissionProductMapping } from './mappings/return-item-dto-to-remission-product.mapping';
import { ReceiptItemDtoToRemissionProductMapping } from './mappings/receipt-item-dto-to-remission-product.mapping';
import { ReceiptDTOToShippingDocumentMapping } from './mappings/receipt-dto-to-shipping-document.mapping';
import { RemissionService } from './services/remission.service';
import { FeaturesToAssortmentMapping } from './mappings/features-to-assortment.mapping';
import { SupplierDtoToRemissionSupplier } from './mappings/supplier-dto-to-remssion-supplier.mapping';

export function remissionServiceFactory(options: RemissionModuleOptions, rest: RestRemissionService, mock: MockRemissionService) {
  return options.useMock ? mock : rest;
}

@NgModule({
  declarations: [],
  imports: [],
  exports: [],
})
export class RemissionModule {
  static forRoot(options: RemissionModuleOptions): ModuleWithProviders<RemissionModule> {
    return {
      ngModule: RemissionModule,
      providers: [
        {
          provide: RemissionModuleOptions,
          useValue: options,
        },
        {
          provide: Mapper,
          useClass: ReturnSuggestionDtoToRemissionProductMapping,
          multi: true,
        },
        {
          provide: Mapper,
          useClass: ItemDtoToProductMapping,
          multi: true,
        },
        {
          provide: Mapper,
          useClass: ProductToPriceDTOMapping,
          multi: true,
        },
        {
          provide: Mapper,
          useClass: ProductToProductDTOMapping,
          multi: true,
        },
        {
          provide: Mapper,
          useClass: ReturnItemDtoToRemissionProductMapping,
          multi: true,
        },
        {
          provide: Mapper,
          useClass: ReceiptItemDtoToRemissionProductMapping,
          multi: true,
        },
        {
          provide: Mapper,
          useClass: ReceiptDTOToShippingDocumentMapping,
          multi: true,
        },
        {
          provide: Mapper,
          useClass: FeaturesToAssortmentMapping,
          multi: true,
        },
        {
          provide: Mapper,
          useClass: SupplierDtoToRemissionSupplier,
          multi: true,
        },
        {
          provide: RemissionService,
          useFactory: remissionServiceFactory,
          deps: [RemissionModuleOptions, RestRemissionService, MockRemissionService],
        },
      ],
    };
  }
}
