import * as util from '../utils'

/* eslint-disable no-await-in-loop */

const clickSaveContinue = async () => {
  await util.clickButton('Save and continue')
}

const createBrief = async () => {
  await util.clickLink('/2/outcome-choice', true)
  await util.clickLink('/2/buyer-atm/create', true)
  await util.clickLink('Create and publish request')
  await util.clickButton('Start now')
}

const fillWhoCanRespond = async () => {
  await clickSaveContinue()
  await util.matchText('li', 'You must select who can respond')
  await util.selectRadio('category')
  await clickSaveContinue()
  await util.clickButton('Save and continue')
  await util.matchText('li', 'You must select a panel category')
  await util.selectRadio('all')
  await clickSaveContinue()
}

const fillAbout = async (title, locations) => {
  await util.typeInReactInput('title', { value: title })
  await util.typeInReactInput('organisation', { numberOfCharacters: 100 })
  await util.typeInReactInput('summary', { numberOfWords: 150 })

  locations.forEach(async location => {
    await util.selectCheck(location)
  })

  await clickSaveContinue()
}

const fillResponseFormats = async () => {
  await clickSaveContinue()
  await util.matchText('li', 'You must specify if you need sellers to supply any other information')
  await util.selectRadio('yes')
  await clickSaveContinue()
  await util.matchText('li', 'You must select at least one response format')
  await util.selectCheck('Case study')
  await util.selectCheck('References')
  await util.selectCheck('Résumés')
  await util.selectCheck('Presentation')
  await util.selectCheck('Prototype')
  await clickSaveContinue()
}

const fillObjectives = async () => {
  await clickSaveContinue()
  await util.matchText('li', 'Enter the reason the work is being done')
  await util.matchText('li', 'Enter the key problem to be solved')
  await util.matchText('li', 'Enter the user needs for your opportunity')
  await util.matchText('li', 'Enter the work already done for your opportunity')

  await util.typeInReactInput('backgroundInformation', { numberOfWords: 500 })
  await util.typeInReactInput('outcome', { numberOfWords: 500 })
  await util.typeInReactInput('endUsers', { numberOfWords: 500 })
  await util.typeInReactInput('workAlreadyDone', { numberOfWords: 500 })
  await util.typeInReactInput('industryBriefing', { numberOfWords: 500 })

  /*
  
  const fields = [
    { id: 'backgroundInformation', options: { numberOfWords: 500 } },
    { id: 'outcome', options: { numberOfWords: 500 } },
    { id: 'endUsers', options: { numberOfWords: 500 } },
    { id: 'workAlreadyDone', options: { numberOfWords: 500 } },
    { id: 'industryBriefing', options: { numberOfWords: 150 } }
  ]
  fields.forEach(async field => {
    await util.typeInReactInput(field.id, field.options)
  })


  */

  await util.upload('file_0', 'document.pdf')
  await clickSaveContinue()
}

const fillTimeframes = async () => {
  await clickSaveContinue()
  await util.matchText('li', 'Enter an estimated start date for the brief')

  await util.typeInReactInput('start_date', { numberOfWords: 10 })
  await util.typeInReactInput('timeframeConstraints', { numberOfWords: 150 })

  /* skips the first field 
  const fields = [
    { id: 'start_date', options: { numberOfWords: 10 } },
    { id: 'timeframeConstraints', options: { numberOfWords: 150 } }
  ]
  fields.forEach(async field => {
    await util.typeInReactInput(field.id, field.options)
  })

  */

  await clickSaveContinue()
}

const fillResponseCriteria = async numberOfCriterias => {
  await clickSaveContinue()
  await util.matchText('li', 'You must not have any empty criteria.')
  await util.selectCheck('yes')
  const criterias = []
  for (let i = 0; i < numberOfCriterias; i += 1) {
    if (i > 0) {
      await util.clickLink('Add another criteria')
    }
    const criteria = await util.typeInReactInput(`criteria_${i}`, { numberOfWords: 50 })
    const weighting = await util.typeInReactInput(`weighting_${i}`, { value: '50' })
    criterias.push({
      criteria,
      weighting
    })
  }
  await clickSaveContinue()
  return criterias
}

const fillClosingDate = async () => {
  await clickSaveContinue()
  await util.matchText('li', 'You must add a closing date at least 2 days from now')
  await util.matchText('li', 'You must add a contact number')
  const now = new Date()
  const future = new Date(now.setDate(now.getDate() + 14))
  await util.typeInReactInput('day', { value: `${future.getDate()}` })
  await util.typeInReactInput('month', { value: `${future.getMonth() + 1}` })
  await util.typeInReactInput('year', { value: `${future.getFullYear()}` })
  await util.typeInReactInput('contactNumber', { value: '0123456789' })
  await clickSaveContinue()
}

const publishBrief = async () => {
  await util.clickButton('Publish')
  await util.matchText('h1', 'Your opportunity is now live.')
}

const create = async params => {
  console.log('Starting to create atm brief')
  await createBrief()
  await fillWhoCanRespond()
  await fillAbout(params.title, params.locations)
  await fillResponseFormats()
  await fillObjectives()
  await fillTimeframes()
  const criterias = await fillResponseCriteria(params.numberOfCriterias ? params.numberOfCriterias : 2)
  await fillClosingDate()
  await publishBrief()
  return {
    criterias
  }
}

export default create

@@ -2369,455 +2369,8 @@
  %7D)%0A
-%0A  /*%0A  %0A  const fields = %5B%0A    %7B id: 'backgroundInformation', options: %7B numberOfWords: 500 %7D %7D,%0A    %7B id: 'outcome', options: %7B numberOfWords: 500 %7D %7D,%0A    %7B id: 'endUsers', options: %7B numberOfWords: 500 %7D %7D,%0A    %7B id: 'workAlreadyDone', options: %7B numberOfWords: 500 %7D %7D,%0A    %7B id: 'industryBriefing', options: %7B numberOfWords: 150 %7D %7D%0A  %5D%0A  fields.forEach(async field =%3E %7B%0A    await util.typeInReactInput(field.id, field.options)%0A  %7D)%0A%0A%0A  */%0A%0A
   aw

Update atm.js