import {TargetConstants} from 'constants/TargetConstants'
import {genericWebAPICall} from 'utils/AppAPI'


export function createTargetAPI (requestObject) {
	return dispatch => {
		genericWebAPICall(
			'courses/createTargetAPI/v1/',
			requestObject,
			function (response) {
				if (response.hasOwnProperty('status') && response.status === 'NOT_OK') {
					return;
				}
				console.log('Success @createTargetAPI ', response);
			},
			function (error) {
				console.log('Error @createTargetAPI ', error);
			}
		)
	}
}

export function getTargetsAPI (requestObject) {
	return dispatch => {
		genericWebAPICall(
			'courses/getTargetsAPI/v1/',
			requestObject,
			function (response) {
				if (response.hasOwnProperty('status') && response.status === 'NOT_OK') {
					return;
				}
				console.log('Success @getTargetsAPI ', response);
			},
			function (error) {
				console.log('Error @getTargetsAPI ',error);
			}
		)
	}
}

export function getTargetTypesAPI (requestObject) {
	return dispatch => {
		genericWebAPICall(
			'courses/getTargetTypesAPI/v1/',
			requestObject,
			function (response) {
				if (response.hasOwnProperty('status') && response.status === 'NOT_OK') {
					return;
				}
				console.log('Success @getTargetTypesAPI ', response);
				dispatch({
					type : TargetConstants.GET_TARGET_TYPES,
					targetTypes : response
				})
			},
			function (error) {
				console.log('Error @getTargetTypesAPI ',error);
			}
		)
	}
}

export function getCourseList (requestObject) {
	return dispatch => {
		genericWebAPICall(
			'courses/getCourseDetailsAPI/v1/',
			requestObject,
			function (response) {
				if (response.hasOwnProperty('status') && response.status === 'NOT_OK') {
					return;
				}
				console.log('Success @getCourseList ', response);
				dispatch({
					type : TargetConstants.GET_COURSE_LIST,
					courseList : response
				})
			},
			function (error) {
				console.log('Error @getCourseList ', error);
			}
		)
	}
}

export function getTopicList (requestObject) {
	return dispatch => {
		genericWebAPICall(
			'courses/getTopicsInCourseForTraineeAPI/v1/',
			requestObject,
			function (response) {
				if (response.hasOwnProperty('status') && response.status === 'NOT_OK') {
					return;
				}
				console.log('Success @getTopicList ', response);
				dispatch({
					type : TargetConstants.GET_TOPIC_LIST,
					topicList : response
				})
			},
			function (error) {
				console.log('Error @getTopicList ', error);
			}
		)
	}
}

export function getExamList (requestObject) {
	return dispatch => {
		genericWebAPICall(
			'courses/getExamsForATopicAPI/v1/',
			requestObject,
			function (response) {
				if (response.hasOwnProperty('status') && response.status === 'NOT_OK') {
					return;
				}
				console.log('Success @getExamList ', response);
				dispatch({
					type : TargetConstants.GET_EXAM_LIST,
					examList : response
				})
			},
			function (error) {
				console.log('Error @getExamList ', error);
			}
		)
	}
}


export function getTargetTypesJSON () {
	return dispatch => {
		$.getJSON('/json/getTargetTypesAPI.json')
		.done(function (response) {
			console.log('Success @getTargetTypesJSON', response);
			dispatch({
				type : TargetConstants.GET_TARGET_TYPES,
				targetTypes : response
			})
		})
		.fail(function (error) {
			console.log('Error @getTargetTypesJSON', error);
		})
	}
}

export function getTargetsJSON () {
	return dispatch => {
		$.getJSON('/json/getTargetsAPI.json')
		.done(function (response) {
			console.log('Success @getTargetsJSON', response);
			dispatch({
				type : TargetConstants.GET_TARGET_LIST,
				targets : response
			})
		})
		.fail(function (error) {
			console.log('Error @getTargetsJSON', error);
		})
	}
}




export default {
	getTargetTypesAPI,
	getTargetsAPI,
	createTargetAPI,
	getCourseList,
	getTopicList,
	getExamList,
	getTargetTypesJSON,
	getTargetsJSON
}