/* eslint camelcase: 0 */
const Q = require('q')
const winston = require('winston')
const adviserRepository = require('../repos/adviser.repo')
const companyRepository = require('../repos/company.repo')
const metadataRepository = require('../repos/metadata.repo')
const serviceDeliveryRepository = require('../repos/service-delivery.repo')
const interactionDataService = require('./interaction-data.service')
const { getFormattedAddress } = require('../lib/address')

function getContactInCompanyObject (company, contactId) {
  for (const contact of company.contacts) {
    if (contact.id === contactId) return contact
  }
  return contactId
}

function getInflatedDitCompany (token, id) {
  return new Promise((resolve, reject) => {
    Q.spawn(function * () {
      try {
        const adviserHash = {}
        const company = yield companyRepository.getDitCompany(token, id)
        const serviceDeliveries = yield serviceDeliveryRepository.getServiceDeliveriesForCompany(token, company.id)

        // Build a list of advisers to lookup
        for (const interaction of company.interactions) {
          adviserHash[interaction.dit_adviser] = true
        }
        for (const serviceDelivery of serviceDeliveries) {
          adviserHash[serviceDelivery.relationships.dit_adviser.data.id] = true
        }

        // get the related adviors
        for (const adviserId of Object.keys(adviserHash)) {
          const adviser = yield adviserRepository.getAdviser(token, adviserId)
          adviserHash[adviser.id] = adviser
        }

        const serviceOffers = yield metadataRepository.getServiceOffers(token)

        // Parse the service delivery results to expand some of the properties
        const parsedServiceDeliveries = serviceDeliveries.map((serviceDelivery) => {
          return Object.assign({}, { id: serviceDelivery.id }, serviceDelivery.attributes, {
            contact: getContactInCompanyObject(company, serviceDelivery.relationships.contact.data.id),
            interaction_type: { id: null, name: 'Service delivery' },
            dit_adviser: adviserHash[serviceDelivery.relationships.dit_adviser.data.id],
            service: serviceOffers.find((option) => option.id === serviceDelivery.relationships.service.data.id),
            dit_team: metadataRepository.teams.find((option) => option.id === serviceDelivery.relationships.dit_team.data.id),
          })
        })

        // Parse the interaction results to expand some of the properties
        const parsedInteractions = company.interactions.map((interaction) => {
          return Object.assign({}, interaction, {
            contact: getContactInCompanyObject(company, interaction.contact),
            interaction_type: interactionDataService.getInteractionType(interaction.interaction_type),
            dit_adviser: adviserHash[interaction.dit_adviser],
            service: serviceOffers.find((option) => option.id === interaction.service),
            dit_team: metadataRepository.teams.find((option) => option.id === interaction.dit_team),
          })
        })

        const combinedIteractions = [...parsedInteractions, ...parsedServiceDeliveries]

        company.interactions = combinedIteractions
        resolve(company)
      } catch (error) {
        winston.error(error)
        reject(error)
      }
    })
  })
}

/**
 * Pass an API formatted company record in and return a path to view that company depending on company type
 *
 * @param {Object} company
 *
 * @returns {string} urlPath
 */
function buildCompanyUrl (company) {
  const companyPath = '/company/view'
  const businessType = company.business_type && (company.business_type.name && company.business_type.name.toLowerCase())
  let urlPath

  if (!company.uk_based) {
    urlPath = `${companyPath}/foreign/${company.id}`
  } else if (businessType.includes('limited company')) {
    urlPath = `${companyPath}/ltd/${company.id}`
  } else {
    urlPath = `${companyPath}/ukother/${company.id}`
  }

  return urlPath
}

function getHeadingAddress (company) {
  // If this is a CDMS company
  const cdmsTradingAddress = getFormattedAddress(company, 'trading')
  if (cdmsTradingAddress) {
    return cdmsTradingAddress
  }

  if (company.companies_house_data && company.companies_house_data !== null) {
    return getFormattedAddress(company.companies_house_data, 'registered')
  }

  return getFormattedAddress(company, 'registered')
}

function getHeadingName (company) {
  if (company.id) {
    if (company.alias && company.alias.length > 0) {
      return company.alias
    }
    return company.name
  } else {
    return company.companies_house_data.name
  }
}

function getCommonTitlesAndlinks (req, res, company) {
  res.locals.headingName = getHeadingName(company)
  res.locals.headingAddress = getHeadingAddress(company)
  res.locals.companyUrl = buildCompanyUrl(company)
}

module.exports = {
  getInflatedDitCompany,
  buildCompanyUrl,
  getCommonTitlesAndlinks,
  getHeadingAddress,
}

@@ -42,16 +42,50 @@
 re('q')%0A
+const %7B get %7D = require('lodash')%0A
 const wi
@@ -3638,41 +3638,19 @@
 e = 
-company.business_type && 
+get
 (company
 .bus
@@ -3641,25 +3641,27 @@
  get(company
-.
+, '
 business_typ
@@ -3670,38 +3670,14 @@
 name
- && company.business_type.name
+', '')
 .toL
@@ -3686,17 +3686,16 @@
 erCase()
-)
 %0A  let u

fix for buisness_type error not being there