import { wait } from '../tools.js'
import { myBridge } from './map.js'
import { getMapToken, getServiceToken } from '@/service/map.js'
import useLayoutStore from '@/store/layout.js'

export function invokeParams(ActionName, params) {
	console.log('invokeParams', ActionName, params)
	myBridge.bridgeContent.Invoke({
		ActionName: ActionName,
		Parameters: {
			...params,
		},
	})
}

async function returnProxyUrl(url) {
	let arr = url.split('/')
	let username = ''
	let password = ''
	if (arr.indexOf('MapProxyApi') !== -1) {
		//不需要授权服务
		for (let i = 0; i < arr.length; i++) {
			if (arr[i] === 'getSceneServer') {
				username = arr[i + 1]
				password = arr[i + 2]
				const response = await getServiceToken(username, password)
				if (response?.data.length > 0) {
					arr.splice(i - 1, 4)
					return arr.join('/') + '/MapServiceProxy/' + response.data
				}
			}
		}
	} else {
		return url
	}
}

//重点区域定位
export function importantAreaPosition(x, y, z, heading, tilt, isfly = true) {
	invokeParams('SetLocation', {
		x: x,
		y: y,
		z: z,
		heading: heading,
		tilt: tilt,
		isfly: isfly,
	})
}

export async function AddBaseMapLayer() {
	//let url = await returnProxyUrl('https://192.168.2.140:6443/geoscene/rest/services/JC_dark/MapServer')
	invokeParams('AddBaseMapLayer', {
		id: 'BaseMap',
		title: 'BaseMap',
		visible: true,
		opacity: 1,
		url: 'https://192.168.2.140:6443/geoscene/rest/services/JC_dark/MapServer',
	})
}

export async function AddZhenwuMapLayer() {
	//let url = await returnProxyUrl('https://cimweb.zjw.sh.cegn.cn:2008/MapProxyApi/getSceneServer/ptgl/37072265')
	invokeParams('AddBaseMapLayer', {
		id: 'BaseMapZhenwu',
		title: 'BaseMapZhenwu',
		visible: true,
		opacity: 1,
		url: 'https://192.168.2.140:6443/geoscene/rest/services/JC_white/MapServer',
	})
}

export async function AddYaoganMapLayer() {
	//let url = 'https://gisser.china-gis.com/geoserver/rest/services/Hosted/WT_HP/MapServer'
	invokeParams('AddBaseMapLayer', {
		id: 'BaseMapYaoGan',
		title: 'BaseMapYaoGan',
		visible: true,
		opacity: 1,
		type: 'tile',
		url: 'https://192.168.2.140:6443/geoscene/rest/services/WT_HP/MapServer',
	})
}

export function showLayersWithScaleEvents(params) {
	invokeParams('ShowLayersWithScaleEvents', {
		status: params.status ? 'show' : 'hide',
		scale_handle_id: 'scale_handle_tileLayerChange',
	})
}

export async function AddSingleLayer(params) {
	// if(params.token){
	//     params.url = "https://cimweb.zjw.sh.cegn.cn:2008/MapServiceProxy/" + params.token;
	// }
	params.url = await returnProxyUrl(params.url)
	let wkid = '0' // 0 代表上海城建 1:代表3857
	if (params.wkid) {
		if (params.type == '动态地图' && params.wkid == '1') {
			params.type = 'dynamic'
		}
		if (params.type == '切片地图' && params.wkid == '1') {
			params.type = 'tile'
		}
	}
	if (params.type == 'scene' || params.type == '3DObject') {
		if (params.title == '全市白模') {
			invokeParams('AddSingleLayer', {
				id: params.id,
				title: params.title,
				visible: params.visible,
				opacity: 1,
				url: params.url + '/SceneServer',
				where: 'objectid not in  (18797)',
				type: 'scene',
				theme: '高度',
				popupEnabled: false,
				elevationInfo: {
					//下沉高度
					mode: 'absolute-height',
					offset: -3.5,
				},
				renderer: {
					type: 'simple',
					symbol: {
						type: 'mesh-3d',
						symbolLayers: [
							{
								type: 'fill',
								material: {
									//color: [102, 167, 219, 0.9],
									color: [255, 255, 255, 1],
									colorMixMode: 'replace',
								},
							},
						],
					},
				},
				mapID: params.mapID,
			})
		} else if (params.title == '东方明珠') {
			invokeParams('AddSingleLayer', {
				id: params.id,
				title: params.title,
				visible: params.visible,
				opacity: 1,
				url: params.url + '/SceneServer',
				type: 'scene',
				elevationInfo: {
					//下沉高度
					mode: 'absolute-height',
					offset: -3.5,
				},
				mapID: params.mapID,
			})
		} else {
			invokeParams('AddSingleLayer', {
				id: params.id,
				title: params.title,
				visible: params.visible,
				opacity: params.opacity,
				url: params.url + '/SceneServer',
				type: 'scene',
				popupEnabled: false,
				mapID: params.mapID,
			})
		}
	} else if (params.type == 'bim') {
		switch (params.title) {
			case '朱家角车站':
				invokeParams('AddSingleLayer', {
					id: params.id,
					title: params.title,
					visible: params.visible,
					opacity: params.opacity === undefined ? 1 : params.opacity,
					url: params.url,
					elevationInfo: {
						//下沉高度
						mode: 'absolute-height',
						offset: -19.5,
					},
					type: 'building',
					mapID: params.mapID,
				})
				break
			default:
				invokeParams('AddSingleLayer', {
					id: params.id,
					title: params.title,
					visible: params.visible,
					opacity: params.opacity === undefined ? 1 : params.opacity,
					url: params.url,
					elevationInfo: {
						//下沉高度
						mode: 'absolute-height',
						offset: -19.5,
					},
					type: 'building',
					mapID: params.mapID,
				})
				break
		}
	} else if (params.type == 'mesh') {
		invokeParams('AddSingleLayer', {
			id: params.id,
			title: params.title,
			visible: params.visible,
			opacity: params.opacity === undefined ? 1 : params.opacity,
			url: params.url,
			//"renderer":locationRenderer,
			type: 'mesh',
			mapID: params.mapID,
		})
		return
	} else if (params.type == 'dynamic') {
		invokeParams('AddSingleLayer', {
			id: params.id,
			title: params.title,
			visible: params.visible,
			opacity: params.opacity === undefined ? 1 : params.opacity,
			url: params.url,
			// "sublayers": [
			//     {
			//         "id": 0
			//     },{
			//         "id": 1
			//     }
			// ],
			type: 'dynamic',
			mapID: params.mapID,
		})
	} else if (params.type == 'tile') {
		invokeParams('AddSingleLayer', {
			id: params.id,
			title: params.title,
			visible: params.visible,
			opacity: params.opacity === undefined ? 1 : params.opacity,
			url: params.url,
			type: 'tile',
			mapID: params.mapID,
		})
	} else if (params.type == '动态地图') {
		invokeParams('AddSingleLayer', {
			id: params.id,
			title: params.title,
			visible: params.visible,
			opacity: params.opacity === undefined ? 1 : params.opacity,
			url: params.url,
			type: 'shc_map',
			mapID: params.mapID,
		})
	} else if (params.type == '切片地图') {
		let size = [256, 256]
		let origin = {
			x: -9080400.0,
			y: 10002300,
		}
		invokeParams('AddSingleLayer', {
			id: params.id,
			title: params.title,
			visible: params.visible,
			opacity: params.opacity === undefined ? 1 : params.opacity,
			url: params.url,
			type: 'shc_tile',
			mapID: params.mapID,
		})
	}
}

// 清空绘制
export function clearDraw() {
	invokeParams('Draw', {
		status: 'hide',
	})
}

// 空域展示
export function showShapes({ id, data }) {
	invokeParams('GeometryMeshEffect', {
		id,
		status: data ? 'show' : 'hide',
		data,
	})
}

// 绘制区域
export function DrawPolygon(params) {
	invokeParams('Draw', {
		type: 'polygon',
		clear: true,
		hasZ: params?.hasZ || false,
		token: '',
	})
}

// 核查网格
export function InspectCube(params) {
	const actionMap = {
		polygon: 'GetPrismCube',
		funnel: 'GetFunnelCube',
		'frustum-cone': 'GetFrustumConeCube',
		'inverted-cone': 'GetInvertedConeCube',
		polyline: 'GetPathCube',
	}
	if (!actionMap[params?.type]) return
	invokeParams(actionMap[params.type], {
		id: params.id.toString(),
		status: params.show ? 'show' : 'hide',
		token: '',
		...params.shape,
	})
}

// 栅格占用查询、空域剖面查询
export function QueryCube(params) {
	invokeParams(params.evaluation ? 'GetPowerCube' : 'GetOccupyCube', {
		id: params.id || 'QueryCube',
		status: params.show ? 'show' : 'hide',
		deepShow: params.deepShow ? params.deepShow : false,
		level: params.level,
		minZ: params.minZ,
		maxZ: params.maxZ,
		rings: params.rings,
		token: '',
	})
}

// 分类核查网格(航线)
export function InspectPathCube(params) {
	invokeParams('ShowThreeCubeDetail', {
		id: params.id,
		status: params.show ? 'show' : 'hide',
		points: params.points,
		size: params.size,
		token: '',
	})
}

// 态势航线显隐藏控制
export function toggleMovePath(stauts) {
	invokeParams('MovePaths', {
		status: stauts ? 'show' : 'hide',
		token: '',
	})
}

// 态势网格显隐藏控制
export function toggleMoveCube(stauts) {
	invokeParams('ShowMovePointCube', {
		status: stauts ? 'show' : 'hide',
		token: '',
	})
}

// 空域点查询
export function toggleFeaturesClickEvent(status) {
	invokeParams('AccessFeaturesWithPointEvents', {
		status: status ? 'show' : 'hide',
		type: 'click',
		mouse_time: 500,
		popup_title: '悬浮结果',
		mouse_handle_id: 'mouse_handle_id',
		highlight_handle_id: 'highlight_handle_id',
		token: '',
	})
}

// 空域栅格修改高度
export function setKysgHeight({ minZ, maxZ }) {
	invokeParams('ChangeScaleEventCubeHeight', {
		minZ,
		maxZ,
		token: '',
	})
}

// 总体态势
export async function setUavDemo(type, stauts) {
	const layoutStore = useLayoutStore()
	layoutStore.toggleGlobalLoading(true)
	const allTypes = ['SituationAssessment', 'PathConflict', 'UavYawWarning', 'DetectionConflict']
	if (stauts) {
		allTypes.forEach((t) => {
			if (t !== type) {
				invokeParams(t, {
					status: 'hide',
					token: '',
				})
			}
		})
		await wait(1000)
		invokeParams(type, {
			status: 'show',
			token: '',
		})
	} else {
		invokeParams(type, {
			status: 'hide',
			token: '',
		})
	}
	layoutStore.toggleGlobalLoading(false)
}

export function limitHeightAnalysis(params) {
	invokeParams('LimitHeightAnalysis', {
		status: params.show ? 'show' : 'hide',
		layerId: 'D8503010320240901',
		limitH: params.minZ,
		maxLimitH: params.maxZ,
		rings: [
			[4929.0513305664, 2427.762878418, 0],
			[544.4196777344, 2427.762878418, 0],
			[544.4196777344, -1598.4520874023, 0],
			[4929.0513305664, -1598.4520874023, 0],
		],
		token: '',
	})
}