import { config, constants } from 'config/general';
import currency from 'config/data/currency';
import countryModel from 'config/data/countryModel';
import { defaultModel, contentModule, addressModel } from './adminGeneral';
import { adminConstants } from './adminConfig';

const productFormat = [
  { value: 'default', label: 'Default' },
  { value: 'wallet', label: 'Wallet' },
];

const list = { ...adminConstants, ...constants };

const orderStatus = [
  { value: 'processing', label: 'Processing' },
  { value: 'completed', label: 'Completed' },
  { value: 'refund', label: 'Refund' },
  { value: 'canceled', label: 'Canceled' },
];

function constructValuesTVA() {
  const res = [];

  for (let index = 0; index < Object.keys(countryModel).length; index++) {
    const element = Object.keys(countryModel)[index];
    res.push({ type: 'string', inputType: 'number', label: element, name: `values.${element}` });
  }

  return res;
}

const promoModel = [
  {
    type: 'section',
    label: 'Promo',
    fields: [
      ...defaultModel.fields,
      { type: 'string', label: 'Code', name: 'code' },
      { type: 'select', label: 'type', name: 'type', list: list.promoType },
      { type: 'multiarea', label: 'description', name: 'description' },
    ],
  },
  {
    type: 'section',
    label: 'Settings',
    side: true,
    fields: [
      { type: 'string', inputType: 'number', label: 'Value', name: 'value' },
      { type: 'number', label: 'usage Limit', name: 'usageLimit' },
      { type: 'bool', label: 'Cummulable', name: 'cummulable' },
      { type: 'date', label: 'validity', name: 'validity' },
    ],
  },
];

const productModel = [
  {
    type: 'section',
    label: 'Product',
    side: true,
    fields: [
      {
        type: 'select',
        list: constants.productType,
        label: 'type',
        name: 'productType',
        noUpdate: true,
        required: true,
      },
      {
        type: 'text',
        name: 'productType',
        label: 'type',
        noCreate: true,
        noMutation: true,
        required: true,
      },
      {
        type: 'select',
        list: productFormat,
        label: 'format',
        name: 'productFormat',
        noUpdate: true,
        required: true,
      },
      {
        type: 'text',
        name: 'productFormat',
        label: 'format',
        noCreate: true,
        noMutation: true,
        required: true,
      },
      { type: 'price', label: 'price' },
      {
        type: 'string',
        inputType: 'number',
        label: 'Credit Amount',
        name: 'walletCredit',
        displayIf: { productFormat: 'wallet' },
        required: true,
      },
      {
        type: 'number',
        label: 'Stock',
        name: 'stock',
        hideIf: { productFormat: 'wallet' },
        required: true,
      },
      {
        type: 'string',
        label: 'Final price',
        name: 'finalPrice',
        noCreate: true,
        noMutation: true,
        noUpdate: true,
      },
      {
        type: 'datetime',
        dateFormat: 'DD/MM/YYYY',
        timeFormat: 'HH:mm',
        name: 'validity',
        label: 'Validity',
      },
    ],
  },
  ...contentModule,
  defaultModel,
];

export const orderModel = [
  defaultModel,
  {
    type: 'section',
    label: 'Info',
    fields: [
      { type: 'string', name: 'paymentStatus' },
      { type: 'string', name: 'orderStatus' },
    ],
  },
  {
    type: 'section',
    label: 'Articles',
    fields: [
      { type: 'multi', name: `lines.getProduct.title` },
      { type: 'multi', name: `lines.getProduct.teaser` },
      { type: 'string', name: `lines.getProduct.cover.large` },
      { type: 'string', name: `lines.getProduct.price` },
      { type: 'string', name: 'lines.getProduct.finalPrice' },
      { type: 'string', name: 'lines.getProduct.vatClassId' },
      { type: 'string', name: `lines.getProduct.currency` },
      { type: 'string', name: 'lines.quantity' },
      { type: 'string', name: 'subTotalPrice' },
      { type: 'string', name: 'vatPrice' },
      { type: 'string', name: 'finalPrice' },
      { type: 'string', name: 'promo.type' },
      { type: 'string', name: 'promo.code' },
      { type: 'string', name: 'promo.value' },
    ],
  },
  {
    type: 'section',
    label: 'Billing Info',
    fields: [
      { type: 'string', name: 'billingInfo.email' },
      { type: 'string', name: 'billingInfo.firstName' },
      { type: 'string', name: 'billingInfo.lastName' },
      { type: 'string', name: 'billingInfo.company' },
      { type: 'string', name: 'billingInfo.vatNumber' },
      ...addressModel('billingInfo'),
    ],
  },
  {
    type: 'section',
    label: 'Contact Info',
    fields: [
      { type: 'string', name: 'contactInfo.email' },
      { type: 'string', name: 'contactInfo.firstName' },
      { type: 'string', name: 'contactInfo.lastName' },
      { type: 'string', name: 'contactInfo.company' },
      { type: 'string', name: 'contactInfo.vatNumber' },
      ...addressModel('contactInfo'),
    ],
  },
];

const vatModel = [
  {
    type: 'section',
    label: 'General',
    side: true,
    fields: [...defaultModel.fields, { type: 'string', label: 'title', name: 'title' }],
  },
  {
    type: 'section',
    label: 'Values',
    fields: constructValuesTVA(),
  },
];

export default {
  nav: [
    {
      title: 'Promos',
      link: '/shop/promos',
    },
    {
      title: 'Products',
      link: '/shop/products',
    },
    {
      title: 'Orders',
      link: '/shop/orders',
    },
    {
      title: 'VAT',
      link: '/shop/vat',
    },
  ],
  orderStatus,
  ressource: {
    promos: {
      listMethod: 'promosGetMany',
      detailMethod: 'promosGetOne',
      addMethod: 'promosAddOne',
      deleteMethod: 'promosDeleteOne',
      editMethod: 'promosEditOne',
      titleList: 'Promos',
      treatData: data => {
        if (data.value) data.value = parseFloat(data.value, 10);
        if (data.usageLimit) data.usageLimit = parseInt(data.usageLimit, 10);
        return data;
      },
      headers: [{ title: 'code', key: 'code' }],
      model: promoModel,
    },
    products: {
      listMethod: 'productsGetMany',
      detailMethod: 'productsGetOne',
      addMethod: 'productsAddOne',
      deleteMethod: 'productsDeleteOne',
      editMethod: 'productsEditOne',
      addLabel: 'New product',
      titleList: 'Products',
      titleDetail: 'Product',
      titleEdit: 'Edit Product',
      titleAdd: 'Add Product',
      sort: 'createdAt DESC',
      subNav: {
        path: 'products',
        params: 'productType',
        list: constants.productType,
      },
      listMode: 'card',
      treatData: data => {
        if (data.price) data.price = parseFloat(data.price, 10);
        if (data.walletCredit) data.walletCredit = parseFloat(data.walletCredit, 10);
        if (data.stock || data.stock === '') {
          data.stock = isNaN(parseFloat(data.stock, 10)) ? null : parseFloat(data.stock, 10);
        }
        return data;
      },
      headers: [
        { title: 'title', key: `title.${config.defaultLanguage}` },
        { key: 'price' },
        { key: 'currency' },
        { key: `teaser.${config.defaultLanguage}` },
        { key: 'cover.large' },
        { key: 'thumbnail.large' },
      ],
      model: productModel,
    },
    orders: {
      listMethod: 'adminsOrdersGetMany',
      detailMethod: 'adminsOrdersGetOne',
      resourceName: 'adminsOrders',
      titleList: 'Orders',
      titleDetail: 'Order',
      noCreate: true,
      noUpdate: true,
      noSearch: true,
      headers: [
        { title: 'Payment', key: 'paymentStatus' },
        { title: 'First Name', key: 'billingInfo.firstName' },
        { title: 'Last Name', key: 'billingInfo.lastName' },
        { title: 'Promo', key: 'promo.code' },
        { title: 'Created At', type: 'date', key: 'createdAt' },
      ],
      subNav: {
        path: 'orders',
        params: 'orderStatus',
        list: orderStatus,
      },
      model: orderModel,
      // Exemple How To Display like Richi Design
      // transformFunction: obj => ({
      //   infos: {
      //     info1: { label: 'Processing', active: obj.orderStatus === 'processing' },
      //     info2: { label: 'Canceled', active: obj.orderStatus === 'canceled' },
      //     info3: { label: 'Refund', active: obj.orderStatus === 'refund' },
      //     info4: { label: 'Delivered', active: obj.orderStatus === 'delivered' },
      //   },
      // }),
    },
    vatClass: {
      titleList: 'VAT Class',
      name: 'vatClass',
      noSearch: true,
      headers: [{ title: 'Title', key: 'title' }],
      model: vatModel,
      treatData: data => {
        if (data.values) {
          for (let index = 0; index < Object.entries(data.values).length; index++) {
            const element = Object.entries(data.values)[index];
            if (typeof element[1] === 'string' && element[0] !== '__typename') {
              data.values[element[0]] = parseInt(element[1], 10);
            }
          }
        }
        return data;
      },
    },
  },
};
