import filter from 'lodash/filter';
import orderBy from 'lodash/orderBy';
import unionBy from 'lodash/unionBy';
import uniq from 'lodash/uniq';
import zipObject from 'lodash/zipObject';
import { ICaseEntitlement, ICaseType, IProductResponse } from './AppMetadataReducer';

export const getProcessedEntitlements = (caseEntitlements: ICaseEntitlement[]) => {
  return filter(unionBy(caseEntitlements, 'sla'), (e) => e.sla && e.sla !== 'UNKNOWN');
};

export const getProcessedCaseTypes = (caseTypes: ICaseType[]) => {
  // move 'Other' to the end
  caseTypes = caseTypes.filter((caseType: ICaseType) => {
    if (caseType.name !== 'Other') {
      return caseType;
    }
  });
  caseTypes.push({ name: 'Other' });

  return caseTypes;
};

export const handleProductSearchResponse = (response): IProductResponse => {
  const productsResult = response;

  // start of calculating feature products
  // also we use this object to show default version when user select
  // product from homepage and is redirected to SE app
  const productList = [];
  for (let j = 0, pmax = productsResult.length; j < pmax; j++) {
    if (productsResult[j][1] !== 'Other') {
      productList.push(productsResult[j][1]);
    }
  }
  const featuredProducts = uniq(productList);

  const obj = {};
  for (let i = 0, imax = featuredProducts.length; i < imax; i++) {
    for (let j = 0, jmax = productsResult.length; j < jmax; j++) {
      if (featuredProducts[i] === productsResult[j][1]) {

        obj['"' + productsResult[j][1] + '"'] = productsResult[j][2];
        break;
      }
    }
  }

  const featuredProductsObj = obj;
  // end of calculating feature products

  // group products based on version
  for (let i = 0, max = productsResult.length; i < max; i++) {
    if (productsResult[i][1] !== 'Other') {
      productsResult[i] = zipObject(['product_version', 'product_name', 'version'], productsResult[i]);
    }
  }

  const groups = {};
  // tslint:disable-next-line:prefer-for-of
  for (let i = 0; i < productsResult.length; i++) {
    if (productsResult[i].product_name) {
      const groupName = productsResult[i].product_name;
      if (!groups[groupName]) {
        groups[groupName] = [];
      }
      groups[groupName].push(productsResult[i].version);
    }
  }

  const pproductsResult = [];
  // tslint:disable-next-line:forin
  for (const groupName in groups) {

    // sort versions in descending order
    const versions = groups[groupName]
      .map(a => a.replace(/\d+/g, n => +n + 100000)).sort()
      .map(a => a.replace(/\d+/g, n => +n - 100000)).reverse();

    // handle special case
    // when we have unknown as the version, we remove it
    const index = versions.indexOf('Unknown');
    if (index !== -1) {
      versions.splice(index, 1);
    }

    // tslint:disable-next-line:max-line-length
    pproductsResult.push({
      product_name: groupName, versions, featuredVersion: featuredProductsObj['"' + groupName + '"'],
      selectedVersion: featuredProductsObj['"' + groupName + '"']
    });

  }
  // end of group products based on version

  // first top 5 products before sorting
  // we get products based on the highest number of support cases
  return {
    topProducts: pproductsResult.slice(0, 5),
    otherProducts: orderBy(pproductsResult.slice(5), ['product_name'], ['asc']),
    productsResult: pproductsResult
  };
};
