import _ from 'lodash'

import { GET_RESOURCE_TO_VERIFY } from './action-types'
import { FORM_SUCCESSFULLY_SUBMITTED } from './action-types'
import { CLEAR_RESOURCE } from './action-types'
import { store } from '../../hah-app/index'
import { change } from 'redux-form'

async function getOneResource() {
	const response = await fetch(`/api/pregnancy-centers/verify`, {
		method: 'GET',
		credentials: 'include',
		headers: {
			Accept: 'application/json',
		},
	})
	return await response.json()
}

function getResource(resource) {
	return {
		type: GET_RESOURCE_TO_VERIFY,
		resource,
	}
}

function clearResource() {
	return {
		type: CLEAR_RESOURCE,
	}
}

export function setFormStatus(status) {
	return {
		type: FORM_SUCCESSFULLY_SUBMITTED,
		formStatus: status,
	}
}

function resetFormAndResource(dispatch) {
	_.forEach(
		store.getState().form.verificationPortal.registeredFields,
		field => {
			dispatch(change('verificationPortal', field, null))
		},
	)
	dispatch(clearResource())
}
export const getResourceToVerify = () => {
	return function(dispatch) {
		resetFormAndResource(dispatch)
		return getOneResource().then(result => dispatch(getResource(result)))
	}
}

const convertTimeToNumber = timeString => {
	if (!timeString) {
		return null
	}
	const hours = parseInt(timeString.slice(0, 2))
	const hoursIn24HourFormat =
		timeString.slice(5, timeString.length) === 'pm'
			? hours + 12
			: (hours < 10 ? '0' : '') + hours
	const minutes = parseInt(timeString.slice(2, 5))
	const minutesInTwoDesigt = (minutes < 10 ? '0' : '') + minutes
	return Number(`${hoursIn24HourFormat}${minutesInTwoDesigt}`)
}

export async function updateResource(updatedResource) {
	const convertedHours = _.mapValues(updatedResource.hours, dayOfWeek => {
		return _.omitBy(
			{
				open: convertTimeToNumber(_.get(dayOfWeek, 'open')),
				close: convertTimeToNumber(_.get(dayOfWeek, 'close')),
				closedAllDay: _.get(dayOfWeek, 'closedAllDay'),
			},
			_.isNull || _.isUndefined,
		)
	})

	const transformedResource = {
		...updatedResource,
		hours: convertedHours,
		services: _.mapValues(updatedResource.services, value => !!value),
	}
	// 5-12-18, I am commenting this out for now, as we want to keep the
	// data verification simple for now. We will eventually add this back.
	// primaryContactPerson: {
	// 	_id: _.get(store.getState().resource, 'primaryContactPerson._id'),
	// 	...updatedResource.primaryContactPerson
	// },

	// eslint-disable-next-line no-async-promise-executor
	return new Promise(async (resolve, reject) => {
		const response = await fetch(
			`/api/pregnancy-centers/${store.getState().resource._id}`,
			{
				method: 'PUT',
				credentials: 'include',
				headers: {
					Accept: 'application/json',
					'Content-Type': 'application/json',
				},
				body: JSON.stringify(transformedResource),
			},
		)

		const result = await response.json()

		if (response.status < 400) {
			store.dispatch(setFormStatus('Success'))
			resolve(result)
		} else {
			store.dispatch(setFormStatus('Failed'))
			reject(result.error)
		}
	})
}

@@ -1173,586 +1173,92 @@
 %0A%7D%0A%0A
-const convertTimeToNumber = timeString =%3E %7B%0A%09if (!timeString) %7B%0A%09%09return null%0A%09%7D%0A%09const hours = parseInt(timeString.slice(0, 2))%0A%09const hoursIn24HourFormat =%0A%09%09timeString.slice(5, timeString.length) === 'pm'%0A%09%09%09? hours + 12%0A%09%09%09: (hours %3C 10 ? '0' : '') + hours%0A%09const minutes = parseInt(timeString.slice(2, 5))%0A%09const minutesInTwoDesigt = (minutes %3C 10 ? '0' : '') + minutes%0A%09return Number(%60$%7BhoursIn24HourFormat%7D$%7BminutesInTwoDesigt%7D%60)%0A%7D%0A%0Aexport async function updateResource(updatedResource) %7B%0A%09const convertedHours = _.mapValues(updatedResource.hours, dayOfWeek =%3E %7B%0A%09%09return
+export async function updateResource(updatedResource) %7B%0A%09const transformedResource =
  _.o
@@ -1270,186 +1270,38 @@
 (%0A%09%09
-%09
 %7B%0A%09%09%09
-%09open: convertTimeToNumber(_.get(dayOfWeek, 'open')),%0A%09%09%09%09close: convertTimeToNumber(_.get(dayOfWeek, 'close')),%0A%09%09%09%09closedAllDay: _.get(dayOfWeek, 'closedAllDay')
+...updatedResource
 ,%0A
-%09
 %09%09%7D,%0A
-%09
 %09%09_.
@@ -1330,459 +1330,9 @@
 d,%0A%09
-%09)%0A%09%7D)%0A%0A%09const transformedResource = %7B%0A%09%09...updatedResource,%0A%09%09hours: convertedHours,%0A%09%09services: _.mapValues(updatedResource.services, value =%3E !!value),%0A%09%7D%0A%09// 5-12-18, I am commenting this out for now, as we want to keep the%0A%09// data verification simple for now. We will eventually add this back.%0A%09// primaryContactPerson: %7B%0A%09// %09_id: _.get(store.getState().resource, 'primaryContactPerson._id'),%0A%09// %09...updatedResource.primaryContactPerson%0A%09// %7D,
+)
 %0A%0A%09/

Remove unused code