import * as api from '@/api/index'
import {getPowerDept} from "@/api/modules/home-management/home";
import {getUserInfo} from "@/api/modules/global/global";

const state = {
	apiProxy: {
		EzServer6Api: 'EzServer6-api',	// 地图底图代理
		geoserverApi: 'geoserver-api',	// 地图服务代理
		historyTrackWSApi: 'history-track-ws-api',	//	历史轨迹代理
		rhWSApi: 'rh-ws-api',	// 融合实时船舶ws代理
		bdWSApi: 'bd-ws-api',	// 北斗实时船舶ws代理
		aisWSApi: 'ais-ws-api', //全球ais
		xmbWSApi: 'xmb-ws-api',	// 小目标雷达实时船舶ws代理
		hlxWSApi: 'hlx-ws-api',	// 海兰信守望者实时船舶ws代理
		hlxRadarWSApi: 'hlx-radar-ws-api',	// 海兰信原始雷达实时船舶ws代理
		zyhWSApi: 'zyh-ws-api',	// 中远海实时船舶ws代理
		dcWSApi: 'dc-ws-api',	// 历史回放单船ws代理
		kxWSApi: 'kx-ws-api',	// 历史回放框选ws代理
		rhFindShipApi: 'rh-find-ship-api',	// 融合找船代理
		warningWSApi: 'warning-ws-api',	//	预警推送ws代理
		infoCenterWSApi: 'info-center-ws-api',	//	消息中心ws代理
		policeManWSApi: 'police-man-ws-api',	//	执法记录仪ws代理
		speedAnalysisApi: 'speed-analysis-api',	//	轨迹列表航速分析导出
		jwtWSApi: 'jwt-ws-api',	//	警务通实时ws代理
		pdtWSApi: 'pdt-ws-api',	//	PDT实时ws代理
		policeCarWSApi: 'police-car-ws-api',	//	警车实时ws代理
		policeBoatWSApi: 'police-boat-ws-api',	//	巡逻艇实时ws代理
	},
	userInfo: <any>{},
	permissions: [],
	deptTree: [],
	deptList: [],
	deptTreeMap: new Map(),
	deptAllTree: [],
	deptAllList: [],
	deptAllTreeMap: new Map(),
	userAllTree: [],
	userAllList: [],
	userAllTreeMap: new Map(),
	powerDept: [],
	zIndex: 100,
	isDrag: false,
	globalConfig: {
		fileMax: 0,	//	上传文件最大值
		fileType: '',	//	上传文件类型
		imgMax: 0,	//	上传图片最大值
		imgType: '',	//	上传图片类型
		passwordLevel: '2',	//	密码安全等级
	}
}

const getters = {
}

const mutations = {
	SET_USER_INFO(state: any, data: object) {
		state.userInfo = data
	},
	SET_PERMISSIONS(state: any, data: Array<string>) {
		state.permissions = data
	},
	SET_DEPT_TREE(state: any, data: Array<any>) {
		const recursive=(list: any) => {
			return list.map((v: any) => {
				if('children' in v && v.children && v.children.length!==0) {
					v.children= recursive(v.children)
				}else {
					v.children=undefined
				}
				return {
					deptLevel:v.deptLevel,
					deptName:v?.deptName,
					children:v.children,
					deptId:v?.deptId
				}
			})
		}
		const deptMap = new Map()
		const deptList: any = []
		const setDept = (nodes: any) => {
			nodes.forEach((v: any) => {
				// v.deptId = v.id
				// v.deptName = v.label
				deptMap.set(v.deptId, v)
				deptList.push(v)
				if (v.children && v.children.length > 0) {
					setDept(v.children)
				}
			})
		}
		setDept(data)
		state.deptTree = recursive(data)
		state.deptList = deptList
		state.deptTreeMap = deptMap
	},
	SET_DEPT_ALL_TREE(state: any, data: Array<any>) {
		const deptAllMap = new Map()
		const deptAllList: any = []
		const setDept = (nodes: any) => {
			nodes.forEach((v: any) => {
				// v.deptId = v.id
				// v.deptName = v.label
				deptAllMap.set(v.deptId, v)
				deptAllList.push(v)
				if (v.children && v.children.length > 0) {
					setDept(v.children)
				}
			})
		}
		setDept(data)
		state.deptAllTree = data
		state.deptAllList = deptAllList
		state.deptAllTreeMap = deptAllMap
	},
	SET_USER_ALL_TREE(state: any, data: Array<any>) {
		const userAllMap = new Map()
		const userAllList: any = []
		const setUser = (nodes: any) => {
			nodes.forEach((v: any) => {
				if (!v.isDept) {
					userAllMap.set(v.id, v)
					userAllList.push(v)
				}
				if (v.children && v.children.length > 0) {
					setUser(v.children)
				}
			})
		}
		setUser(data)
		state.userAllTree = data
		state.userAllList = userAllList
		state.userAllTreeMap = userAllMap
	},
	SET_POWER_DEPT(state: any, data: any) {
		state.powerDept = data
	},
	SET_DRAG_Z (state) {
		state.zIndex++
	},
	SET_IS_DRAG(state) {
		state.isDrag = !state.isDrag
	},
	SET_GLOBAL_CONFIG(state, rows) {
		rows.forEach(v => {
			if (v.configKey === 'global.fileSize') {
				state.globalConfig.fileMax = Number(v.configValue)
			} else if (v.configKey === 'global.fileType') {
				state.globalConfig.fileType = v.configValue
			} else if (v.configKey === 'global.imageSize') {
				state.globalConfig.imgMax = Number(v.configValue)
			} else if (v.configKey === 'global.imageType') {
				state.globalConfig.imgType = v.configValue
			} else if (v.configKey === 'global.pwdSecurityLevel') {
				state.globalConfig.passwordLevel = v.configValue
			}
		})
	}
}

const actions = {
	LOAD_USER_INFO({ commit }: any, refresh: boolean = false) {
		return new Promise((resolve, reject) => {
			if (refresh || !state.userInfo?.userId) {
				getUserInfo().then((res: any) => {
					if (res.code === 200) {
						commit('SET_USER_INFO', res.user)
						commit('SET_PERMISSIONS', res.permissions)
						resolve(state.userInfo)
					} else {
						reject(res.msg)
					}
				})
			} else {
				resolve(state.userInfo)
			}
		})
	},
	LOAD_DEPT_TREE({ commit }: any, userDeptId = '', refresh = false) {
		return new Promise((resolve, reject) => {
			if (state.deptTree.length === 0 || refresh) {
				api.default.getDeptTree().then((res: any) => {
					commit('SET_DEPT_TREE', res.data)
					resolve(res.data)
				}).catch(() => {
					reject('获取部门树列表错误')
				})
			} else {
				resolve(null)
			}
		})
	},
	LOAD_DEPT_ALL_TREE({ commit }: any, refresh = false) {
		return new Promise((resolve, reject) => {
			if (state.deptAllTree.length === 0 || refresh) {
				api.default.getDeptTreeAll().then((res: any) => {
					commit('SET_DEPT_ALL_TREE', res.data)
					resolve(res.data)
				}).catch(() => {
					reject('获取部门树列表错误')
				})
			} else {
				resolve(null)
			}
		})
	},
	LOAD_USER_ALL_TREE({ commit }: any, refresh = false) {
		return new Promise((resolve, reject) => {
			if (state.userAllTree.length === 0 || refresh) {
				api.default.getUserAll().then((res: any) => {
					commit('SET_USER_ALL_TREE', res.data)
					resolve(res.data)
				}).catch(() => {
					reject('获取全部用户树列表错误')
				})
			} else {
				resolve(null)
			}
		})
	},
	LOAD_POWER_DEPT({ commit }: any, userDeptId = '', refresh = false) {
		return new Promise((resolve, reject) => {
			if (state.powerDept?.length === 0 || refresh) {
				getPowerDept(userDeptId).then((res: any) => {
					commit('SET_POWER_DEPT', res.data)
					resolve(res.data)
				}).catch(() => {
					reject('获取权限部门错误')
				})
			} else {
				resolve(null)
			}
		})
	},
	LOAD_DRAG_Z ({ commit }) {
		return new Promise(resolve => {
			commit('SET_DRAG_Z')
			resolve(state.zIndex)
		})
	},
	LOAD_IS_DRAG({ commit }) {
		commit('SET_IS_DRAG')
	},
	LOAD_GLOBAL_CONFIG({ commit }, refresh = false) {
		return new Promise((resolve, reject) => {
			if (!state.globalConfig.fileType || refresh) {
				api.default.getParameterList()
					.then(res => {
						commit('SET_GLOBAL_CONFIG', res.rows)
						resolve(res.data)
					})
					.catch(() => {
						reject('获取公共配置错误')
					})
			} else {
				resolve(null)
			}
		})
	},
}

export default {
	namespaced: true,
	state,
	getters,
	mutations,
	actions
}
