import * as api from '@/api/index'
import { dictionary, dictionaryDefine } from './dictionary-define.js'

const state = {
	...dictionary,
	caseTypeTree: [],	// 案件类型级联
	caseTypeTreeMap: new Map(),	// 案件类型级联
	cbglPersonDictionary: [],	// 船籍港一级
	cbglPersonDictionaryMap: new Map(), // 船籍港一级
	portOfRegistryTwoList: [],	// 船籍港二级
	portOfRegistryTwoMap: new Map(), // 船籍港二级
	cbglPersonTemporary: [],	// 常驻停泊点一级
	cbglPersonTemporaryMap: new Map(), // 常驻停泊点一级
	residentMooringPointTwoList: [],	// 常驻停泊点二级
	residentMooringPointTwoMap: new Map(), // 常驻停泊点二级
  ShipData:{
		ShipId:'',
		ShipStatus:'',
		getStatus:false
	},
	bayonetDeepCheckList: {
		all: [],
		car: [],
		face: []
	},	//	卡口深度核查-点位选择-地图框选卡口标点数据
	bayonetDeepCheckTree: {
		car: [],
		face: []
	},	//	卡口深度核查-点位选择-树弹窗选择数据
	bayonetDeepCheckTreeMap: {
		car: new Map(),
		face: new Map()
	},	//	卡口深度核查-点位选择-树弹窗选择数据-Map翻译
	videoList: [],	//	视频监控
	videoMap: new Map(),	//	视频监控
	portList: [], //	港口
	portMap: new Map(),	//	港口
}

const mutations = {
	SET_SHIPDATA_ID(state, data){
         switch (data.type) {
			 case 'id':
				state.ShipData.ShipId=data.value
				state.ShipData.getStatus=false
				 break
			 case 'get':
				state.ShipData.ShipId=data.value
				state.ShipData.getStatus=true
				 break
			 case 'sta':
				state.ShipData.ShipStatus=data.value
				 break
		 }
	},
	SET_DICT_LIST(state: any, { listName, mapName, objMapName, data }: any) {
		const map = new Map()
		const objMap = new Map()
		if (data.length > 0) {
			data.forEach((v: any) => {
				v.selectLabel = v.dictLabel
				v.selectValue = v.dictValue
				map.set(v.dictValue, v.dictLabel)
				objMap.set(v.dictValue, v)
			})
		}
		state[listName] = data
		state[mapName] = map
		state[objMapName] = objMap
	},
	SET_CASE_TYPE_TREE(state: any, data: Array<any>) {
		const map = new Map()
		const deep = (arr: any) => {
			arr.forEach((v: any) => {
				map.set(v.value, v.label)
				if (v.children?.length > 0) {
					deep(v.children)
				}
			})
		}
		deep(data)
		state.caseTypeTree = data
		state.caseTypeTreeMap = map
	},
	SET_PORT_OF_REGISTRY(state: any, {one, two}: any) {
		const mapOne = new Map()
		const mapTwo = new Map()
		one.forEach((v: any) => {
			mapOne.set(v.dictType, v.dictName)
		})
		two.forEach((v: any) => {
			mapTwo.set(v.dictValue, v.dictName)
		})
		state.cbglPersonDictionary = one
		state.cbglPersonDictionaryMap = mapOne
		state.portOfRegistryTwoList = two
		state.portOfRegistryTwoMap = mapTwo
	},
	SET_RESIDENT_MOORING_POINT(state: any, {one, two}: any) {
		const mapOne = new Map()
		const mapTwo = new Map()
		one.forEach((v: any) => {
			mapOne.set(v.dictType, v.dictName)
		})
		two.forEach((v: any) => {
			mapTwo.set(v.dictValue, v.dictName)
		})
		state.cbglPersonTemporary = one
		state.cbglPersonTemporaryMap = mapOne
		state.residentMooringPointTwoList = two
		state.residentMooringPointTwoMap = mapTwo
	},
	SET_BAYONET_DEEP_CHECK(state, {carTree, faceTree, bayonetAll, bayonetCar, bayonetFace}) {
		const carTreeMap = new Map()
		const faceTreeMap = new Map()
		const deepTree = (map, arr, pObj: any = []) => {
			return arr.map(v => {
				v.cIds = []
				if (!v.children && v.videos) {
					v.children = v.videos
				}
				v.videoNum = 0
				if (v.indexCode === undefined) {
					v.source = 'dept'
					v.treeName = v.deptName
					v.treeId = v.deptId + v.deptName
				} else {
					v.source = 'video'
					v.treeName = v.name
					v.treeId = v.name
					pObj.forEach((p: any, i) => {
						p.videoNum++
						if (i === pObj.length - 1) {
							p.lastDept = true
						}
					})
				}
				v.pIds = pObj.map((p: any) => {
					p.cIds.push(v.treeId)
					return p.treeId
				})
				if (v.children?.length > 0) {
					v.children = deepTree(map, v.children, [...pObj, v])
				}
				map.set(v.treeId, v)
				return v
			})
		}
		state.bayonetDeepCheckList.all = bayonetAll
		state.bayonetDeepCheckList.car = bayonetCar
		state.bayonetDeepCheckList.face = bayonetFace
		state.bayonetDeepCheckTree.car = deepTree(carTreeMap, carTree)
		state.bayonetDeepCheckTreeMap.car = carTreeMap
		state.bayonetDeepCheckTree.face = deepTree(faceTreeMap, faceTree)
		state.bayonetDeepCheckTreeMap.face = faceTreeMap
	},
	SET_VIDEO(state, data) {
		const map = new Map()
		data.forEach(v => {
			if (v.equipmentNumber) {
				map.set(v.equipmentNumber, v)
			}
		})
		state.videoMap = map
		state.videoList = data
	},
	SET_PORT (state, data) {
		const map = new Map()
		data.forEach(v => {
			map.set(v.id, v)
		})
		state.portList = data
		state.portMap = map
	},
}

const actions = {
	LOAD_DICT_LIST({ commit }: any, type: any) {
		return new Promise((resolve, reject) => {
			if (state[dictionaryDefine[type][0]].length === 0) {
				api.default.dicDeptDictList(type).then((res: any) => {
					commit('SET_DICT_LIST', {
						listName: dictionaryDefine[type][0],
						mapName: dictionaryDefine[type][1],
						objMapName: dictionaryDefine[type]?.[2],
						data: res.data
					})
					resolve(res.data)
				}).catch((e: any) => {
					console.log('e: ', e);
					reject('获取'+dictionaryDefine[type][0]+'字典错误')
				})
			} else {
				resolve(state[dictionaryDefine[type][0]])
			}
		})
	},
	LOAD_CASE_TYPE({ commit }: any, refresh = false) {
		return new Promise((resolve, reject) => {
			if (state.caseTypeTree?.length > 0 && !refresh) {
				resolve(state.caseTypeTree)
			} else {
				api.default.getCaseTypeTree({
					caseTypeSource: 'xjz'
				}).then((res: any) => {
					if (res.code === 200) {
						commit('SET_CASE_TYPE_TREE', res.data)
					} else {
						reject(null)
					}
				}).catch(() => {
					reject(null)
				})
			}
		})
	},
	// 船籍港
	LOAD_PORT_OF_REGISTRY({ commit }: any, refresh = false) {
		return new Promise((resolve, reject) => {
			if (state.cbglPersonDictionary?.length > 0 && !refresh) {
				resolve(state.cbglPersonDictionary)
			} else {
				api.default.cbglPersonDictionary().then((res: any) => {
					if (res.code === 200) {
						const one = res.data
						Promise.all([
							...one.map((v: any) => api.default.cbglPersonDictionary2(v.dictType))
						]).then((res) => {
							const two: any = []
							res.forEach((v: any, i: number) => {
								if (v.code === 200) {
									two.push(...v.data.map((s: any) => {
										s.parent = one[i].dictType
										return s
									}))
								} else {
									two.push([])
								}
							})
							commit('SET_PORT_OF_REGISTRY', {one, two})
							resolve({one, two})
						}).catch(() => {
							reject(null)
						})
					} else {
						reject(null)
					}
				}).catch(() => {
					reject(null)
				})
			}
		})
	},
	// 常驻停泊点
	LOAD_RESIDENT_MOORING_POINT({ commit }: any, refresh = false) {
		return new Promise((resolve, reject) => {
			if (state.cbglPersonTemporary?.length > 0 && !refresh) {
				resolve(state.cbglPersonTemporary)
			} else {
				api.default.cbglPersonTemporary().then((res: any) => {
					if (res.code === 200) {
						const one = res.data
						Promise.all([
							...one.map((v: any) => api.default.cbglPersonTemporary2(v.dictType))
						]).then((res) => {
							const two: any = []
							res.forEach((v: any, i: number) => {
								if (v.code === 200) {
									two.push(...v.data.map((s: any) => {
										s.parent = one[i].dictType
										return s
									}))
								} else {
									two.push([])
								}
							})
							commit('SET_RESIDENT_MOORING_POINT', {one, two})
							resolve({one, two})
						}).catch(() => {
							reject(null)
						})
					} else {
						reject(null)
					}
				}).catch(() => {
					reject(null)
				})
			}
		})
	},
	//	卡口深度核查-卡口数据
	LOAD_BAYONET_DEEP_CHECK({commit}, refresh = false){
		return new Promise((resolve, reject) => {
			if (state.bayonetDeepCheckList.all.length === 0 || refresh) {
				//	api请求数据
				Promise.all([
					api.default.getVideoHKTree('car'),
					api.default.getVideoHKTree('face'),
					api.default.getVideoMap(),
					api.default.getVideoMap(`type=car`),
					api.default.getVideoMap(`type=face`),
				]).then(([carTreeRes, faceTreeRes, bayonetAllRes, bayonetCarRes, bayonetFaceRes]) => {
					commit('SET_BAYONET_DEEP_CHECK', {
						carTree: [carTreeRes.data] || [],
						faceTree: [faceTreeRes.data] || [],
						bayonetAll: bayonetAllRes.data || [],
						bayonetCar: bayonetCarRes.data || [],
						bayonetFace: bayonetFaceRes.data || []
					})
					resolve('')
				})
			} else {
				resolve('')
			}
		})
	},
	//	视频监控数据
	LOAD_VIDEO({commit}, deptId, refresh = false){
		return new Promise((resolve, reject) => {
			if (state.videoList.length === 0 || refresh) {
				api.default.monitoringList(`pageNum=1&pageSize=5000&deptId=${deptId}`).then(res => {
					if (res.code === 200 && res.rows?.length > 0) {
						commit('SET_VIDEO', res.rows)
					}
					resolve(state.videoList)
				})
			} else {
				resolve(state.videoList)
			}
		})
	},
	LOAD_PORT ({ commit }, refresh = false) {
		return new Promise((resolve, reject) => {
			if (state.portList.length === 0 || refresh) {
				api.default
					.portList()
					.then(res => {
						commit('SET_PORT', res.rows)
						resolve(res.rows)
					})
					.catch(() => {
						reject('获取港岙口列表错误')
					})
			} else {
				resolve(null)
			}
		})
	}
}

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