import { push } from 'react-router-redux';
import RepeatOnWeekDayTime from './RepeatOnWeekDayTime';
import RepeatByDayOfMonthOrWeek from './RepeatByDayOfMonthOrWeek';
import StartsOnDate from './StartsOnDate';
import TimeInput from './TimeInputComponent';
import ReactDatetime from 'react-datetime';
import { openToast } from '../ToastComponent/Toast.js';

export function sendFormDataToStore(type,value){
	return {
		type:'SEND_FORM_DATA_TO_STORE',
		changeType:type,
		data:value
	}
}

export function startScheduledFow(e){
	return dispatch => {
		dispatch({
			type:'START_SCHEDULED_FLOW',
			data:e.target.value
		})
		dispatch({
			type:'DISABLE_NEXT_BUTTON'
		})
	}
}

export function startNowFlow(e){
	return dispatch => {
		dispatch({
			type:'START_NOW_FLOW',
			data:e.target.value
		})
		dispatch({
			type:'ENABLE_NEXT_BUTTON'
		})
	}
}

export function startUserActionSelectionFlow(){
	return{
		type:'START_USER_ACTION_SELECTION_FLOW'
	}
}

export function startUserInActionSelectionFlow(){
	return{
		type:'START_USER_IN_ACTION_SELECTION_FLOW'
	}
}

export function startAllDaysNotificationFlow(){
	return{
		type:'START_ALL_DAY_NOTIFICATION_FLOW'
	}
}

export function startNotificationOnFrequencyFlow(e){
	return dispatch =>{
		// dispatch(Modalactions.openModal());
		dispatch({
			type:'START_NOTIFICATION_ON_FREQUENCY_FLOW',
			data:e.target.value
		})
		dispatch({
			type:'DISABLE_NEXT_BUTTON'
		})
	}
}
export function RepeatEveryDataChange(value) {
	return{
		type:'REPEAT_EVERY_DATA_CHANGE',
		data:value
	}
}
export function  handleRepeatFrequencyChange(type) {
	var RepeatEveryData =[];
	var secondaryView =null;
	var RepeatType = null;
	var RepeatEveryDataBypass = false;
	switch(type){
		case 'WEEK':
			RepeatEveryData = [
				{value:'EVERY_WEEK',label:'Every Week'}
				// {value:'1_WEEK',label:'1st Week'},
				// {value:'2_WEEK',label:'2nd Week'},
				// {value:'3_WEEK',label:'3rd Week'},
				// {value:'4_WEEK',label:'4th Week'}
			]
			secondaryView = RepeatOnWeekDayTime;
			RepeatType = type;
			break;
		case 'MONTH':
			RepeatEveryData = [
				{value:'EVERY_MONTH',label:'Every Month'}
				// {value:'1_MONTH',label:'1 Month'},
				// {value:'2_MONTH',label:'2 Month'},
				// {value:'3_MONTH',label:'3 Month'},
				// {value:'4_MONTH',label:'4 Month'},
				// {value:'5_MONTH',label:'5 Month'},
				// {value:'6_MONTH',label:'6 Month'},
				// {value:'7_MONTH',label:'7 Month'},
				// {value:'8_MONTH',label:'8 Month'},
				// {value:'9_MONTH',label:'9 Month'},
				// {value:'10_MONTH',label:'10 Month'},
				// {value:'11_MONTH',label:'11 Month'}
			]
			secondaryView = RepeatByDayOfMonthOrWeek;
			RepeatType = type;
			break;
		case 'YEAR':
			RepeatEveryData = [
				{value:'1_YEAR',label:'1 Year'},
				{value:'2_YEAR',label:'2 Year'},
				{value:'3_YEAR',label:'3 Year'},
				{value:'4_YEAR',label:'4 Year'},
				{value:'5_YEAR',label:'5 Year'}
			]
			secondaryView = StartsOnDate;
			RepeatType = type;
			break;
		case 'DAILY':
			RepeatEveryDataBypass = true;
			secondaryView = TimeInput;
			RepeatType = type;
			break;
		default:
	}
	return dispatch => {
		dispatch({
			type:'REPEAT_FREQUENCY_CHANGE',
			RepeatEveryData:RepeatEveryData,
			secondaryView:secondaryView,
			RepeatType:RepeatType,
			RepeatEveryDataBypass:RepeatEveryDataBypass
		})
		dispatch({
			type:'DISABLE_NEXT_BUTTON'
		})
	}
}

export function HandleChangeRepeatOnWeekDays(data){
	return{
		type:'UPDATE_REPEAT_ON_WEEK_DAY_DATA',
		data:data
	}
}

export function HandleChangeNotificationEnds(data){
	
	return{
		type:'CHANGE_NOTIFICATIONS_ENDS',
		data:data
	}
}

export function HandleChangeRepeatByDayOfMonthOrWeek(event,data){
	var selectedValue = event.target.value;
	data.map((el)=>{
		el.selected = (el.value == selectedValue)?true:false;
	})

	return{
		type:'CHANGE_REPEAT_BY_DAY_OF_MONTH_OR_WEEK_ENDS',
		data:data
	}
}

export function handleChange(data,type) {
	if(type=='DAY_OF_MONTH'){
		return{
			type:'HANDLE_CHANGE_DAY_OF_MONTH',
			data:data[0]
		}
	}
	else if(type=='DAY_OF_WEEK'){
		return{
			type:'HANDLE_CHANGE_DAY_OF_WEEK',
			data:data[0]
		}
	}
}

export function setScheduleTime(data){
	return dispatch => {
		dispatch({
			type:'SET_SCHEDULE_TIME',
			data:data
		})
		dispatch({
			type:'ENABLE_NEXT_BUTTON'
		})
	}
}

export function extractCronExpression(){
	return{
		type: 'EXTRACT_CRON_EXPRESSION'
	}
}

export function handleEndsOnOccurenceInput(e) {
	return{
		type: 'HANDLE_CHANGE_ON_ENDS_OCCURENCE_INPUT',
		data: e.target.value
	}
}

export function handleChangeEndsOnParticularDateTime(momentObj){
	return{
		type: 'ENDS_ON_PARTICULAR_DATE',
		data: momentObj
	}
}

export function validateCronExpression(data,closeFnc,callBack) {
	var error = false;
	return dispatch => {
		console.log(data);
		if(!data.RepeatType){
			openToast({
				message:'The schedule is incomplete , Kindly fill in the frequency type to proceed further .'
			});
			error = true;
		}
		if(!data.RepeatEveryDataBypass){
			if(!data.RepeatEverySelectedData){
				openToast({
					message:'Kindly fill the sub-frequency on the notification to proceed further .'
				});
				error = true;
			}
		}
		if(!data.formData.schedule){
			openToast({
				message:'The schedule is incomplete , Kindly fill all the fields to complete it .'
			});
			error = true;
		}
		if(!(data.formData.endTimestamp)){
			openToast({
				message:'The notification ending details are not present , kindly fill them .'
			});
			error = true;
		}
		if(!data.formData.startTimestamp){
			openToast({
				message:'The notification start time details are not present , kindly fill them .'
			});
			error = true;
		}
		if(!error){
			closeFnc('indentModal');
			callBack(data.formData);
			dispatch({
				type:'CRON_EXPRESSION_VALID'
			})
		}
		else{
			dispatch({
				type:'DO_NOTHING'
			})
		}
	}
}
