/**
 * add message to dialog queue
 * @param {string} msg 
 * @param {function} act 
 */
export const add_dialog_msg =(msg, act) => ({
	type: 'dialog_addmsg',
	msg: msg,
	act: act || null
});

/**
 * remove dialog queue first message
 */
export const remove_dialog_msg = () => ({
	type: 'dialog_remove_first'
});

/**
 * set i18next object
 * @param {object} i18n i18next object
 */
export const set_i18n = (i18n) => ({
	type: 'i18n_set_ctx',
	i18n: i18n
});

/**
 * Toggle Menu open/close
 * @param {bool} flag switch menu open/close
 */
export const toggle_menu = (flag) => ({
	type: flag ? 'ui_show_menu' : 'ui_hide_menu'
});

/**
 * ToGGle Loading open/close
 * @param {bool} flag 
 */
export const toggle_loading = (flag) => ({
	type: flag ? 'ui_show_loading' : 'ui_hide_loading'
});

/**
 * 
 * @param {object} data 
 */
export const getRequest = (data = {}) => {
	let json = {
		method: 'post',
		headers: {
			'Accept': 'application/json',
			'Content-Type': 'application/json'
		},
		mode:'cors'
	};
	let token = sessionStorage.getItem('token');
	if(token) json.headers['x-auth-token'] = token;

	json.body = JSON.stringify({data});

	return json;
}

export const get_network_info = () => dispatch => {
	dispatch(toggle_loading(1));
	return fetch('/api/system/getnetwork', getRequest())
		.then(response => response.json())
		.then(json => {
			dispatch(toggle_loading(0));
			if(json.status != 1){
				return dispatch(add_dialog_msg(json.message));
			}
			let j = {};
			if('data' in json && 'record' in json.data && json.data.record.length > 0) j = json.data.record[0];
			return dispatch(network_info(j));
		});
}

const network_info = (json) => ({
	type: 'network_info',
	network: json
});

export const set_network_info = (dhcpMode, ip, netmask, gateway, dns) => dispatch => {
	dispatch(toggle_loading(1));
	let type = dhcpMode ? 'dhcp' : 'manual';
	let req = getRequest();
	let json = {
		data:{
			type, 
			ip,
			netmask,
			gateway,
			dns
		}
	}
	req.body = JSON.stringify(json);
	return fetch('/api/system/updatenetwork', req)
		.then(response => response.json())
		.then(json => {
			dispatch(toggle_loading(0));
			if(json.status != 1) return dispatch(add_dialog_msg(json.message));
			return dispatch(get_network_info());
		});
}

export const get_system_time = () => dispatch => {
	dispatch(toggle_loading(1));
	return fetch('/api/system/gettime', getRequest())
		.then(response => response.json())
		.then(json => {
			dispatch(toggle_loading(0));
			if(json.status != 1){
				return dispatch(add_dialog_msg(json.message));
			}
			let t = '';
			if('data' in json && 'record' in json.data && json.data.record.length > 0) t = json.data.record[0].time;
			return dispatch(system_time(t));
		});
}

const system_time = (time) => ({
	type: 'system_time',
	time
});

export const set_system_time = time => dispatch => {
	dispatch(toggle_loading(1));
	let req = getRequest({time});
	return fetch('/api/system/updatetime', req)
		.then(response => response.json())
		.then(json => {
			dispatch(toggle_loading(0));
			if(json.status != 1) return dispatch(add_dialog_msg(json.message));
			return dispatch(get_system_time());
		});
}

export const get_user_list = () => dispatch => {
	dispatch(toggle_loading(1));
	let req = getRequest();
	return fetch('/api/system/getuserlist', req)
		.then(response => response.json())
		.then(json => {
			dispatch(toggle_loading(0));
			if(json.status != 1) return dispatch(add_dialog_msg(json.message));
			return dispatch(user_list(json.data.record));
		});
}

const user_list = (user) => ({
	type: 'user_list',
	user
})

const userApi = (url, data = {}) => dispatch => {
	dispatch(toggle_loading(1));
	let req = getRequest(data);
	return fetch(url, req)
		.then(response => response.json())
		.then(json => {
			dispatch(toggle_loading(0));
			if(json.status != 1) return dispatch(add_dialog_msg(json.message));
			return dispatch(get_user_list());
		});
}

export const edit_user = (data) => dispatch => {
	return dispatch(userApi('/api/system/edituser', data));
}

export const add_user = (data) => dispatch => {
	return dispatch(userApi('/api/system/adduser', data));
}

export const del_user = (data) => dispatch => {
	return dispatch(userApi('/api/system/deluser', data));
}

export const get_dio_info = () => dispatch => {
	dispatch(toggle_loading(1));
	return fetch('/api/dio/getdioinfo', getRequest())
		.then(response => response.json())
		.then(json => {
			dispatch(toggle_loading(0));
			if(json.status != 1) return dispatch(add_dialog_msg(json.message));
			return dispatch(dioinfo(json.data.rt.do || [], json.data.rt.di || []));
		});
}

export const get_dio_status = () => dispatch => {
	return fetch('/api/dio/getio', getRequest())
		.then(response => response.json())
		.then(json => {
			if(json.status != 1) return dispatch(add_dialog_msg(json.message));
			return dispatch(diostatus(json.data.rt.do, json.data.rt.di));
		});
}

export const set_do_status = (data) => dispatch => {
	return fetch('/api/dio/dotrun', getRequest(data))
		.then(response => response.json())
		.then(json => {
			if(json.status != 1) return dispatch(add_dialog_msg(json.message));
		})
}

export const set_dio_info = (data) => dispatch => {
	dispatch(toggle_loading(1));
	return fetch('/api/dio/setdioinfo', getRequest(data))
		.then(response => response.json())
		.then(json => {
			dispatch(toggle_loading(0));
			if(json.status != 1) return dispatch(add_dialog_msg(json.message));
			return dispatch(get_dio_info());
		})
}
const diostatus = (doSt, diSt) => ({
	type: 'dio_status',
	doSt,
	diSt
})
const dioinfo = (dod, did) => ({
	type: 'dio_list',
	do: dod,
	di: did
});

export const get_log_list = (p = 1) => dispatch => {
	dispatch(toggle_loading(1));
	return fetch('/api/log/getlog', getRequest({p}))
		.then(response => response.json())
		.then(json => {
			dispatch(toggle_loading(0));
			if(json.status != 1) return dispatch(add_dialog_msg(json.message));
			return dispatch(loglist(json.data.record, json.data.page));
		});
}

const loglist = (list, page) => ({
	type: 'log_list',
	list,
	page
});

export const scan_leone = (data) => (dispatch, getState) => {
	dispatch(toggle_loading(1));
	dispatch(set_scanning());
	return fetch('/api/leone/scanleone', getRequest(data))
		.then(response => response.json())
		.then(json => {
			dispatch(toggle_loading(0));
			if(json.status != 1) return dispatch(add_dialog_msg(json.message));
			let {i18n} = getState();
			if(json.data.record.length == 0) return dispatch(add_dialog_msg(i18n&&i18n.t? i18n.t('tip.scan_empty') : ''));
			return dispatch(show_scan_leone(json.data.record));
		})
}

export const clear_scan_leone = (clrRemote = false) => dispatch => {
	dispatch(clear_scan());
	
	return fetch('/api/leone/clearscanleone', getRequest())
		.then(response => response.json())
		.then(json => {
			return ;
		});
}

export const add_scan_leone = (data) => dispatch => {
	dispatch(toggle_loading(1));

	return fetch('/api/leone/addscanleone', getRequest(data))
		.then(response => response.json())
		.then(json => {
			dispatch(toggle_loading(0));
			dispatch(clear_scan());
			if(json.status != 1) return dispatch(add_dialog_msg(json.message));
			return dispatch(get_leone_list());
		})
}

export const get_leone_list = (showLoading = true) => dispatch => {
	if(showLoading) dispatch(toggle_loading(1));
	return fetch('/api/leone/getleonelist', getRequest())
		.then(response => response.json())
		.then(json => {
			dispatch(toggle_loading(0));
			if(json.status != 1) return dispatch(add_dialog_msg(json.message));
			return dispatch(leone_list(json.data.record, json.data.rt.status));
		});
}

export const del_leone = (data) => dispatch => {
	dispatch(leone_api('/api/leone/delleone', data));
}

export const add_leone = data => dispatch => {
	dispatch(leone_api('/api/leone/addleone', data));
}

export const edit_leone = data => dispatch => {
	dispatch(leone_api('/api/leone/editleone', data));
}

const leone_api = (url, data) => dispatch => {
	dispatch(toggle_loading(1));
	return fetch(url, getRequest(data))
		.then(response => response.json())
		.then(json => {
			dispatch(toggle_loading(0));
			if(json.status != 1) return dispatch(add_dialog_msg(json.message));
			return dispatch(get_leone_list());
		});
}

const clear_scan = () => ({
	type: 'leone_clear_scan'
})

const set_scanning = () => ({
	type: 'leone_scanning'
})

const show_scan_leone = (list) => ({
	type: 'leone_scan',
	list
});

const leone_list = (list, status) => ({
	type: 'leone_list',
	list,
	status
})

export const io_cmd = (data, cb = null) => dispatch => {
	dispatch(toggle_loading(1));
	return fetch('/api/iocmd/iocmd', getRequest(data))
		.then(response => response.json())
		.then(json => {
			dispatch(toggle_loading(0));
			if(json.status != 1) return dispatch(add_dialog_msg(json.message));
			if(cb && typeof cb == 'function') return cb();
		});
}

export const get_iogroup_list = () => dispatch => {
	dispatch(toggle_loading(1));
	return fetch('/api/iogroup/getiogrouplist', getRequest())
		.then(response => response.json())
		.then(json => {
			dispatch(toggle_loading(0));
			if(json.status != 1) return dispatch(add_dialog_msg(json.message));
			return dispatch(iogroup_list(json.data.record, json.data.rt.do, json.data.rt.leone));
		});
}

const iogroup_list = (list, doa, leone) => ({
	type: 'iogroup_list',
	list,
	do: doa,
	leone
});

const group_api = (url, data) => dispatch => {
	dispatch(toggle_loading(1));
	return fetch(url, getRequest(data))
		.then(response => response.json())
		.then(json => {
			dispatch(toggle_loading(0));
			if(json.status != 1) return dispatch(add_dialog_msg(json.message));
			return dispatch(get_iogroup_list());
		});
}

export const del_iogroup = (data) => dispatch => {
	dispatch(group_api('/api/iogroup/deliogroup', data));
}

export const add_iogroup = (data) => dispatch => {
	dispatch(group_api('/api/iogroup/addiogroup', data));
}

export const edit_iogroup = (data) => dispatch => {
	dispatch(group_api('/api/iogroup/editiogroup', data));
}

export const get_select_list = (data) => dispatch => {
	return fetch('/api/system/getselectlist', getRequest(data))
		.then(response => response.json())
		.then(json => {
			if(json.status != 1) return dispatch(add_dialog_msg(json.message));
			return dispatch(select_list(json.data.record || []));
		})
}

export const clear_select_list = () => ({
	type: 'clear_select_list'
})
const select_list = (list) => ({
	type: 'select_list',
	list
})

const schedule_api = (url, data) => dispatch => {
	dispatch(toggle_loading(1));
	return fetch(url, getRequest(data))
		.then(response => response.json())
		.then(json => {
			dispatch(toggle_loading(0));
			if(json.status != 1) return dispatch(add_dialog_msg(json.message));
			return dispatch(get_schedule_list());
		})
}

const schedule_list = (list, dos, les, ios) => ({
	type: 'schedule_list',
	list,
	dos,
	les,
	ios
});

export const get_schedule_list = () => dispatch => {
	dispatch(toggle_loading(1));
	return fetch('/api/schedule/getschedulelist', getRequest())
		.then(response => response.json())
		.then(json => {
			dispatch(toggle_loading(0));
			if(json.status != 1) return dispatch(add_dialog_msg(json.message));
			return dispatch(schedule_list(json.data.record || [], json.data.rt.do || [], json.data.rt.leone || [], json.data.rt.iogroup || []));
		})
}

export const add_schedule = (data) => dispatch => {
	dispatch(schedule_api('/api/schedule/addschedule', data));
}
export const del_schedule = (data) => dispatch => {
	dispatch(schedule_api('/api/schedule/delschedule', data));
}
export const edit_schedule = (data) => dispatch => {
	dispatch(schedule_api('/api/schedule/editschedule', data));
}
export const sw_schedule = (data) => dispatch => {
	dispatch(schedule_api('/api/schedule/swschedule', data));
}

export const clear_userlist = () => ({
	type: 'clear_userlist'
});
export const clear_dio = () => ({
	type: 'clear_dio'
});
export const clear_leone =() => ({
	type: 'clear_leone'
});
export const clear_log = () => ({
	type: 'clear_log'
});
export const clear_iogroup = () => ({
	type: 'clear_iogroup'
});
export const clear_schedule = () => ({
	type: 'clear_schedule'
});

export const get_modbus_list = () => dispatch => {
	dispatch(toggle_loading(1));
	return fetch('/api/modbus/getmodbuslist', getRequest())
		.then(response => response.json())
		.then(json => {
			dispatch(toggle_loading(0));
			if(json.status != 1 ) return dispatch(add_dialog_msg(json.message));
			dispatch(modbus_list(json.data.record || []));
		});
}

const modbus_list = list => ({
	type: 'modbus_list',
	list
});

export const clear_modbus = () => ({
	type: 'clear_modbus'
})

const modbus_api = (url, data) => dispatch => {
	dispatch(toggle_loading(1));
	return fetch(url, getRequest(data))
		.then(response => response.json())
		.then(json => {
			dispatch(toggle_loading(0));
			if(json.status != 1) return dispatch(add_dialog_msg(json.message));
			dispatch(get_modbus_list());
		})
}

export const del_modbus = (data) => dispatch => {
	dispatch(modbus_api('/api/modbus/delmodbus', data));
}

export const add_modbus = data => dispatch => {
	dispatch(modbus_api('/api/modbus/addmodbus', data));
}

export const edit_modbus = data => dispatch => {
	dispatch(modbus_api('/api/modbus/editmodbus', data));
}

export const get_mb_data = data => dispatch => {
	dispatch(toggle_loading(1));
	return fetch('/api/modbus/getmodbus', getRequest(data))
		.then(response => response.json())
		.then(json => {
			dispatch(toggle_loading(0));
			if(json.status != 1) return dispatch(add_dialog_msg(json.message));
			dispatch(mb_data(data.id, json.data.rt.iolist || [], json.data.rt.aioset || []));
		})
}

const mb_data = (id, iolist, aioset) => ({
	type: 'mb_data',
	id,
	iolist,
	aioset
})

export const get_mb_iostatus = data => dispatch => {
	return fetch('/api/modbus/getiostatus', getRequest(data))
		.then(response => response.json())
		.then(json => {
			if(json.status != 1) return dispatch(add_dialog_msg(json.message));
			dispatch(mb_iostatus(data.id, data.type, json.data.record || []));		
		})
}

const mb_iostatus = (id, iotype, list) => ({
	type: 'mb_iostatus',
	id,
	iotype,
	list
});

const mb_iolist_api = (url, data) => dispatch => {
	dispatch(toggle_loading(1))
	return fetch(url, getRequest(data))
		.then(response => response.json())
		.then(json => {
			dispatch(toggle_loading(0));
			if(json.status != 1) return dispatch(add_dialog_msg(json.message));
			dispatch(get_mb_data({id: data.devuid}));
		});
}

const mb_aio_api = (url, data) => dispatch => {
	dispatch(toggle_loading(1))
	return fetch(url, getRequest(data))
		.then(response => response.json())
		.then(json => {
			dispatch(toggle_loading(0));
			if(json.status != 1) return dispatch(add_dialog_msg(json.message));
			dispatch(get_mb_data({id: data.devuid}));
		});
}

export const edit_mb_iolist = data => dispatch => {
	return dispatch(mb_iolist_api('/api/modbus/editiolist', data));
}
export const add_mb_iolist = data => dispatch => {
	return dispatch(mb_iolist_api('/api/modbus/addiolist', data));
}
export const del_mb_iolist = data => dispatch => {
	return dispatch(mb_iolist_api('/api/modbus/deliolist', data));
}

export const add_mb_aio = data => dispatch => {
	return dispatch(mb_aio_api('/api/modbus/addaioset', data));
}
export const edit_mb_aio = data => dispatch => {
	return dispatch(mb_aio_api('/api/modbus/editaioset', data));
}
export const del_mb_aio = data => dispatch => {
	return dispatch(mb_aio_api('/api/modbus/delaioset', data));
}

export const get_link_list = () => dispatch => {
	dispatch(toggle_loading(1));
	return fetch('/api/link/getlinklist', getRequest())
		.then(response => response.json())
		.then(json => {
			dispatch(toggle_loading(0));
			if(json.status != 1) return dispatch(add_dialog_msg(json.message));
			dispatch(link_list(json.data.record || []))
		})
}

const link_list = (list) => ({
	type: 'link_list',
	list
})
export const clear_link = () => ({
	type: 'clear_link'
})

const link_api = (url, data) => dispatch => {
	return fetch(url, getRequest(data))
		.then(response => response.json())
		.then(json => {
			if(json.status != 1) return dispatch(add_dialog_msg(json.message));
			dispatch(get_link_list());
		})
}

export const add_link = (data) => dispatch => {
	dispatch(link_api('/api/link/addlink', data));
}
export const del_link = (data) => dispatch => {
	dispatch(link_api('/api/link/dellink', data));
}
export const sw_link_active = (data) => dispatch => {
	dispatch(link_api('/api/link/swlinkactive', data));
}

export const get_dashboard = () => dispatch => {
	return fetch('/api/system/dashboard', getRequest())
		.then(response => response.json())
		.then(json => {
			if(json.status != 1) return dispatch(add_dialog_msg(json.message));
			let rt = json.data.rt;
			dispatch(dashboard(rt.ip[0]? rt.ip[0].ip : '', rt.version[0]? rt.version[0].version : '', rt.di, rt.leone))
		})
}

const dashboard = (ip, version, di, leone) => ({
	type: 'dashboard',
	ip,
	version,
	di,
	leone
})

export const toggle_dashboard = (flag) => ({
	type: flag ? 'ui_show_dashboard' : 'ui_hide_dashboard'
})

export const clear_dashboard = () => ({
	type: 'clear_dashboard'
})