import {IoTyConstants} from 'constants/IoTy'
import {genericWebAPICall} from 'utils/AppAPI'

function setExistedCircuitData (response) {
	                                                            return {
		                                                            type: IoTyConstants.SET_EXISTED_CIRCUIT_DATA,
		                                                            existedCircuitData: response.components,
		                                                            existedCodeData: response.code
	}
}

function setNewCircuitData (response) {
	                                                            let actionObject = {
		                                                            type: IoTyConstants.SET_NEW_CIRCUIT_DATA,
		                                                            steps: response.steps
	}
	                                                            if (response.hasOwnProperty('output')) {
		                                                            actionObject.output = response.output
	}
	                                                            return actionObject
}

function setSteps (steps) {
	                                                            return {
		                                                            type: IoTyConstants.SET_STEPS,
		                                                            steps: steps
	}
}

function setPossibleOutputs (outputs) {
	                                                            return {
		                                                            type: IoTyConstants.SET_POSSIBLE_OUTPUTS,
		                                                            output: outputs
	}
}

function setOutputPatterns (outputPatterns) {
	                                                            return {
		                                                            type: IoTyConstants.SET_OUTPUT_PATTERNS,
		                                                            outputPatterns: outputPatterns
	}
}

function setNextStep () {
	                                                            return {
		                                                            type: IoTyConstants.GO_TO_NEXT_STEP
	}
}

function setPrevStep () {
	                                                            return {
		                                                            type: IoTyConstants.GO_TO_PREV_STEP
	}
}

function setStep (step) {
	                                                            return {
		                                                            type: IoTyConstants.GO_TO_STEP,
		                                                            step: step
	}
}

function setSelectedComponentId (selectedComponentId) {
	                                                            return {
		                                                            type: IoTyConstants.SELECT_COMPONENT,
		                                                            selectedComponentId: selectedComponentId
	}
}

function setSelectedCircuitOptionId (selectedOptionId) {
	                                                            return {
		                                                            type: IoTyConstants.SELECT_CIRCUIT_OPTION,
		                                                            selectedCircuitOptionId: selectedOptionId
	}
}

function setSelectedCodeOptionId (selectedOptionId) {
	                                                            return {
		                                                            type: IoTyConstants.SELECT_CODE_OPTION,
		                                                            selectedCodeOptionId: selectedOptionId
	}
}

function stepCompleted (isLocked) {
	                                                            return {
		                                                            type: IoTyConstants.STEP_COMPLETED,
		                                                            isLocked: isLocked
	}
}

function setThresholdValue (thresholdValue) {
	                                                            return {
		                                                            type: IoTyConstants.CHANGE_THRESHOLD_VALUE,
		                                                            thresholdValue: thresholdValue
	}
}

function getLocalCircuitData () {
	                                                            return dispatch => {
		                                                            $.getJSON('json/IoTyVLabSampleBlink.json')	// http://demo3295838.mockable.io/load
		.done(function (response) {
			                                                            console.log('Success @http://demo3295838.mockable.io/load', response)
			                                                            let metaData = response
			                                                            dispatch(setExistedCircuitData(metaData.existedCircuitData))
			                                                            dispatch(setNewCircuitData(metaData.newCircuitData))
			/* dispatch(setSteps(metaData.newCircuitData.steps))
			dispatch(setPossibleOutputs(metaData.newCircuitData.output))*/
			                                                            $('.ioty-circuit-loader').hide()
		})
		.fail(function (error) {
			                                                            console.log('Error @http://demo3295838.mockable.io/load', error)
			                                                            $('.ioty-circuit-loader').hide()
		})
	}
}

function getCircuitData (vlqIdObject) {
	                                                            return dispatch => {
		                                                            const requestObject = vlqIdObject
		// console.log('inside api requestObject', requestObject);
		                                                            genericWebAPICall(
			'vlabs/getVLQDetailsAPI/v1/',
			requestObject,
			function (response) {
				                                                            if (response.hasOwnProperty('status') && response.status === 'NOT_OK') {
					                                                            $('.ioty-circuit-loader').hide()
					                                                            return
				}
				                                                            console.log('Success @getVLQDetails response >> ', response)
				                                                            let metaData = JSON.parse(response.metaData)
				                                                            dispatch(setExistedCircuitData(metaData.existedCircuitData))
				                                                            dispatch(setNewCircuitData(metaData.newCircuitData))
				/* dispatch(setSteps(metaData.newCircuitData.steps))
				dispatch(setPossibleOutputs(metaData.newCircuitData.output))*/
				                                                            $('.ioty-circuit-loader').hide()
			},
			function (response) {
				                                                            console.log('ERROR @getVLQDetails response >> ', response)
				                                                            $('.ioty-circuit-loader').hide()
			}
		)
	}
}

function evaluateCircuitOutput (outputObject) {
	                                                            return dispatch => {
		                                                            const requestObject = outputObject
		                                                            genericWebAPICall(
			'vlabs/evaluateVLQAPI/v1/',
      		requestObject,
      		function (response) {
      			                                                            if (response.hasOwnProperty('status') && response.status === 'NOT_OK') {
					                                                                                                                                                                                                                                                $('.ioty-circuit-loader').hide()
					                                                                                                                                                                                                                                                return
				                                                                                                                                                                                    }
				                                                                                                                                                                                                                                                console.log('Success evaluateCircuitOutput response', response)
      		},
      		function (error) {
      			                                                            console.log('Error evaluateCircuitOutput error', error)
      		}
		)
	}
}

function goToNextStep () {
	                                                            return dispatch => {
		                                                            dispatch(setNextStep())
	}
}

function goToPrevStep () {
	                                                            return dispatch => {
		                                                            dispatch(setPrevStep())
	}
}

function goToStep (step) {
	                                                            return dispatch => {
		                                                            dispatch(setStep(step))
	}
}

function selectComponent (selectedComponentId) {
	                                                            return dispatch => {
		                                                            dispatch(setSelectedComponentId(selectedComponentId))
	}
}

function selectCircuitOption (selectedOptionId) {
	                                                            return dispatch => {
		                                                            dispatch(setSelectedCircuitOptionId(selectedOptionId))
		                                                            dispatch(stepCompleted(selectedOptionId === -1))
	}
}

function selectCodeOption (selectedOptionId) {
	                                                            return dispatch => {
		                                                            dispatch(setSelectedCodeOptionId(selectedOptionId))
		                                                            dispatch(stepCompleted(selectedOptionId === -1))
	}
}

function setCodeAnswer (answer) {
	                                                            return dispatch => {
		                                                            dispatch({
			                                                            type: IoTyConstants.SET_CODE_ANSWER,
			                                                            codeAnswer: answer
		})
		                                                            dispatch(stepCompleted(answer.length === 0))
	}
}

function changeThresholdValue (thresholdValue) {
	                                                            return dispatch => {
		                                                            dispatch(setThresholdValue(thresholdValue))
	}
}

function setPushButtonState (pushButtonState) {
	                                                            return dispatch => {
		                                                            dispatch({
			                                                            type: IoTyConstants.SET_PUSH_BUTTON_STATE,
			                                                            pushButtonState: pushButtonState
		})
	}
}

function configWiFi (wiFiSettingsObject) {
	                                                            return dispatch => {
		                                                            dispatch({
			                                                            type: IoTyConstants.CONFIG_WIFI,
			                                                            wifiSettings: wiFiSettingsObject
		})
		                                                            dispatch(stepCompleted(false))
	}
}

function arduinoLoaded (loaded) {
	                                                            return dispatch => {
		                                                            dispatch({
			                                                            type: IoTyConstants.ARDUINO_LOADED,
			                                                            arduinoLoaded: loaded
		})
	}
}
export const IoTyActions = {
	                                                            getCircuitData,
	                                                            getLocalCircuitData,
	                                                            goToStep,
	                                                            goToPrevStep,
	                                                            goToNextStep,
	                                                            selectComponent,
	                                                            selectCircuitOption,
	                                                            selectCodeOption,
	                                                            setCodeAnswer,
	                                                            changeThresholdValue,
	                                                            setPushButtonState,
	                                                            evaluateCircuitOutput,
	                                                            configWiFi,
	                                                            arduinoLoaded
}
