/**
 * 图形变换工具类
 * 支持拖拽、缩放、旋转、磁性吸附等功能
 */

// 元素项类型定义
export type ItemElement = {
	id: number
	type: number // 0:图片 1:文字 2:贴纸
	index: number // 层级
	
	// 位置和尺寸
	left: number
	top: number
	width: number
	height: number
	
	// 变换属性
	scale: number
	angle: number
	
	// 状态标识
	active: boolean
	activeguide: boolean // 辅助线
	activescale: boolean // 旋转控制
	activehorn: boolean // 四角控制
	activeedge: boolean // 四边控制
	
	// 操作类型标识
	typetext?: string // 'left-top', 'right-bottom', 'left', 'right', 'top', 'bottom', 'rotate'
	
	// 触摸/鼠标数据
	touchX?: number
	touchY?: number
	touchMoveX?: number
	touchMoveY?: number
	startX?: number
	startY?: number
	
	// 缓存数据
	lastElmX?: number
	lastElmY?: number
	lastElmW?: number
	lastElmH?: number
	lastScale?: number
	
	// 初始尺寸（用于计算最小缩放限制）
	initialWidth?: number
	initialHeight?: number
	
	// 临时计算数据
	elmX?: number
	elmY?: number
	elmW?: number
	elmH?: number
	newRotate?: number
	fixedX?: number
	fixedY?: number
	centerPos?: UTSJSONObject
	x?: number
	y?: number
	oScale?: number
	
	// 辅助线显示
	isShowLineV?: boolean
	isShowLineH?: boolean
	selectLineLeft?: number
	selectLineTop?: number
	
	// 其他属性
	isDrag?: boolean
	content?: string
	anglePre?: number
}

// 辅助线数据类型
export type GuideLine = {
	type: string // 'h' 或 'v'
	left: number
	top: number
}

/**
 * 计算两点间的角度
 */
export function getAngle(x: number, y: number): number {
		let theta = Math.atan2(y, x)
		theta = Math.round((180 / Math.PI) * theta)
		if (theta < 0) theta = 360 + theta
		return theta
}

/**
 * 计算两点间的距离
 */
export function getDistance(x1: number, y1: number, x2: number, y2: number): number {
		const dx = x2 - x1
		const dy = y2 - y1
		return Math.sqrt(dx * dx + dy * dy)
}

/**
 * 记录按下时的数据
 */
export function recordPressDownData(item: ItemElement) {
		item.lastElmX = item.left
		item.lastElmY = item.top
		item.lastElmW = item.width
		item.lastElmH = item.height
		item.lastScale = item.scale
		
		let rotate = Math.abs(item.angle) % 360
		if (item.angle < 0) {
			let multiple = Math.ceil(Math.abs(item.angle) / 360)
			rotate = item.angle + multiple * 360
		}
		if (rotate == 360) {
			rotate = 0
		}
		item.newRotate = rotate
		
		// 记录固定点坐标
		let fixed: UTSJSONObject | null = null
		if (item.typetext == 'left-top') {
			fixed = { x: 'right', y: 'bottom' } as UTSJSONObject
		} else if (item.typetext == 'left-bottom' || item.typetext == 'left') {
			fixed = { x: 'right', y: 'top' } as UTSJSONObject
		} else if (item.typetext == 'right-top' || item.typetext == 'top') {
			fixed = { x: 'left', y: 'bottom' } as UTSJSONObject
		} else if (item.typetext == 'right-bottom' || item.typetext == 'right' || item.typetext == 'bottom') {
			fixed = { x: 'left', y: 'top' } as UTSJSONObject
		}
		
		if (fixed != null) {
			let rect = {
				top: item.top,
				right: item.left + item.width,
				bottom: item.top + item.height,
				left: item.left
			}
			let fixedCoordinate = rotatedPoint(rect, rotate, fixed)
			item.fixedX = fixedCoordinate['x'] as number
			item.fixedY = fixedCoordinate['y'] as number
		}
}

/**
 * 计算旋转后的坐标点
 */
export function rotatedPoint(rect: UTSJSONObject, rotate: number, point: UTSJSONObject): UTSJSONObject {
		let top = rect['top'] as number
		let right = rect['right'] as number
		let bottom = rect['bottom'] as number
		let left = rect['left'] as number
		let rad = (Math.PI / 180) * rotate
		let cos = Math.cos(rad)
		let sin = Math.sin(rad)
		let originX = (right - left) / 2 + left
		let originY = (bottom - top) / 2 + top
		let pointX = point['x'] as string
		let pointY = point['y'] as string
		let x = rect[pointX] as number
		let y = rect[pointY] as number
		x -= originX
		y -= originY
		return {
			x: Math.floor((x * cos - y * sin + originX) * 10000) / 10000,
			y: Math.floor((x * sin + y * cos + originY) * 10000) / 10000
		}
}

/**
 * 计算固定点位置
 */
export function fixedTo(item: ItemElement, fixed: UTSJSONObject, rotate: number): UTSJSONObject {
		let elmX = item.elmX as number
		let elmY = item.elmY as number
		let elmW = item.elmW as number
		let elmH = item.elmH as number
		let rect = {
			top: elmY,
			right: elmX + elmW,
			bottom: elmY + elmH,
			left: elmX
		}
		let rotated = rotatedPoint(rect, rotate, fixed)
		let fixedX = rotated['x'] as number
		let fixedY = rotated['y'] as number
		let dX = (item.fixedX as number) - fixedX
		let dY = (item.fixedY as number) - fixedY
		return {
			x: elmX + dX,
			y: elmY + dY
		}
}

/**
 * 四角缩放移动事件
 */
export function scaleMove(e: any, item: ItemElement, designWidth: number, designHeight: number, allLineListData: GuideLine[] = []) {
		// 状态初始化
		item.isShowLineV = false
		item.isShowLineH = false
		item.active = false
		item.activeguide = false
		item.activescale = false
		item.activehorn = true
		item.activeedge = false
		
		// 计算最小尺寸：初始尺寸的50%
		const initialWidth = item.initialWidth != null ? item.initialWidth : item.lastElmW as number
		const initialHeight = item.initialHeight != null ? item.initialHeight : item.lastElmH as number
		const minWidth = initialWidth * 0.5
		const minHeight = initialHeight * 0.5
		// 取消最大尺寸限制，允许无限放大
		// const maxWidth = 800
		// const maxHeight = 800
		
		// 获取触摸位置
		const touchEvent = e as UniTouchEvent
		if (touchEvent.touches.length > 0) {
			item.touchMoveX = touchEvent.touches[0].clientX
			item.touchMoveY = touchEvent.touches[0].clientY
		} else {
			const mouseEvent = e as MouseEvent
			item.touchMoveX = mouseEvent.clientX
			item.touchMoveY = mouseEvent.clientY
		}
		
		let touchMoveX = item.touchMoveX as number
		let touchMoveY = item.touchMoveY as number
		let touchX = item.touchX as number
		let touchY = item.touchY as number
		let newRotate = item.newRotate as number
		
		let width = touchMoveX - touchX
		let height = touchMoveY - touchY
		let c = Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2))
		let angle = getAngle(width, height)
		let rad = (Math.PI / 180) * (angle - newRotate)
		let diffY = Math.sin(rad) * c
		let diffX = Math.cos(rad) * c
		
		item.elmX = item.lastElmX
		item.elmY = item.lastElmY
		item.elmW = item.lastElmW
		item.elmH = item.lastElmH
		
		let elmW = item.elmW as number
		let elmH = item.elmH as number
		let scale: number = elmW / elmH
		let fixed: UTSJSONObject = { x: 'left', y: 'top' } as UTSJSONObject
		
		// 根据操作角确定固定点和缩放方向
		if (item.typetext == 'left-top') {
			fixed = { x: 'right', y: 'bottom' } as UTSJSONObject
			diffX = -diffX
			elmW += diffX
			elmH = elmW / scale
			item.elmW = elmW
			item.elmH = elmH
		} else if (item.typetext == 'left-bottom') {
			fixed = { x: 'right', y: 'top' } as UTSJSONObject
			diffX = -diffX
			elmW += diffX
			elmH = elmW / scale
			item.elmW = elmW
			item.elmH = elmH
		} else if (item.typetext == 'right-top') {
			fixed = { x: 'left', y: 'bottom' } as UTSJSONObject
			elmW += diffX
			elmH = elmW / scale
			item.elmW = elmW
			item.elmH = elmH
		} else if (item.typetext == 'right-bottom') {
			fixed = { x: 'left', y: 'top' } as UTSJSONObject
			elmW += diffX
			elmH = elmW / scale
			item.elmW = elmW
			item.elmH = elmH
		}
		
		item.isDrag = true
		
		// 尺寸限制
		elmW = item.elmW as number
		elmH = item.elmH as number
		
		if (elmW < minWidth) {
			elmW = minWidth
			elmH = elmW / scale
			item.elmW = elmW
			item.elmH = elmH
			item.isDrag = false
		}
		if (elmH < minHeight) {
			elmH = minHeight
			elmW = elmH * scale
			item.elmW = elmW
			item.elmH = elmH
			item.isDrag = false
		}
		// 取消最大尺寸限制，允许无限放大
		// if (elmW > maxWidth) {
		// 	elmW = maxWidth
		// 	elmH = elmW / scale
		// 	item.elmW = elmW
		// 	item.elmH = elmH
		// }
		// if (elmH > maxHeight) {
		// 	elmH = maxHeight
		// 	elmW = elmH * scale
		// 	item.elmW = elmW
		// 	item.elmH = elmH
		// }
		
		let getNewData = fixedTo(item, fixed, item.newRotate!)
		item.elmX = getNewData['x'] as number
		item.elmY = getNewData['y'] as number
		item.left = item.elmX!
		item.top = item.elmY!
		item.width = item.elmW!
		item.height = item.elmH!
		
		// 磁性吸附（仅在触摸移动且无旋转时）
		if (item.newRotate == 0) {
			applyMagneticAdsorption(item, designWidth, designHeight, allLineListData)
		}
		
		// 更新中心位置
		updateCenterPos(item)
}

/**
 * 四边拉伸移动事件
 */
export function edgeMove(e: any, item: ItemElement, designWidth: number, designHeight: number, allLineListData: GuideLine[] = []) {
		// 状态初始化
		item.active = true
		item.activeguide = false
		item.activescale = false
		item.activehorn = false
		item.activeedge = true
		item.isShowLineV = false
		item.isShowLineH = false
		
		// 获取移动位置
		const touchEvent = e as UniTouchEvent
		if (touchEvent.touches.length > 0) {
			item.touchMoveX = touchEvent.touches[0].clientX
			item.touchMoveY = touchEvent.touches[0].clientY
		} else {
			const mouseEvent = e as MouseEvent
			item.touchMoveX = mouseEvent.clientX
			item.touchMoveY = mouseEvent.clientY
		}
		
		let touchMoveX = item.touchMoveX as number
		let touchMoveY = item.touchMoveY as number
		let touchX = item.touchX as number
		let touchY = item.touchY as number
		let newRotate = item.newRotate as number
		
		let width = touchMoveX - touchX
		let height = touchMoveY - touchY
		let c = Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2))
		let angle = getAngle(width, height)
		let rad = (Math.PI / 180) * (angle - newRotate)
		let diffY = Math.sin(rad) * c
		let diffX = Math.cos(rad) * c
		
		item.elmX = item.lastElmX
		item.elmY = item.lastElmY
		item.elmW = item.lastElmW
		item.elmH = item.lastElmH
		
		let elmW = item.elmW as number
		let elmH = item.elmH as number
		let fixed: UTSJSONObject = { x: 'left', y: 'top' } as UTSJSONObject
		
		// 根据拉伸方向确定固定点
		if (item.typetext == 'left') {
			fixed = { x: 'right', y: 'top' } as UTSJSONObject
			elmW += diffX * (-1)
		} else if (item.typetext == 'top') {
			fixed = { x: 'left', y: 'bottom' } as UTSJSONObject
			elmH += diffY * (-1)
		} else if (item.typetext == 'right') {
			fixed = { x: 'left', y: 'top' } as UTSJSONObject
			elmW += diffX * (1)
		} else if (item.typetext == 'bottom') {
			fixed = { x: 'left', y: 'top' } as UTSJSONObject
			elmH += diffY * (1)
		}
		
		// 最小尺寸限制
		if (elmW < 20) elmW = 20
		if (elmH < 20) elmH = 20
		
		item.elmW = elmW
		item.elmH = elmH
		
		let getNewData = fixedTo(item, fixed, item.newRotate!)
		item.elmX = getNewData['x'] as number
		item.elmY = getNewData['y'] as number
		item.left = item.elmX!
		item.top = item.elmY!
		item.width = item.elmW!
		item.height = item.elmH!
		
		// 磁性吸附
		if (item.newRotate == 0) {
			applyEdgeMagneticAdsorption(item, designWidth, designHeight, allLineListData)
		}
		
		// 更新中心位置
		updateCenterPos(item)
}

/**
 * 拖拽移动事件
 */
export function dragMove(e: any, item: ItemElement, designWidth: number, designHeight: number, allLineListData: GuideLine[] = []) {
		item.isShowLineV = false
		item.isShowLineH = false
		
		let mouseX: number
		let mouseY: number
		
		const touchEvent = e as UniTouchEvent
		if (touchEvent.touches.length > 0) {
			mouseX = touchEvent.touches[0].clientX
			mouseY = touchEvent.touches[0].clientY
		} else {
			const mouseEvent = e as MouseEvent
			mouseX = mouseEvent.clientX
			mouseY = mouseEvent.clientY
		}
		
		item.elmX = item.lastElmX
		item.elmY = item.lastElmY
		item.elmW = item.lastElmW
		item.elmH = item.lastElmH
		
		let diffX = mouseX - (item.startX as number)
		let diffY = mouseY - (item.startY as number)
		
		let elmX = item.elmX as number
		let elmY = item.elmY as number
		elmX += diffX
		elmY += diffY
		item.elmX = elmX
		item.elmY = elmY
		item.left = elmX
		item.top = elmY
		
		// 磁性吸附
		if (item.newRotate == 0) {
			applyDragMagneticAdsorption(item, designWidth, designHeight, allLineListData)
		}
		
		item.left = item.elmX!
		item.top = item.elmY!
		
		// 更新中心位置
		updateCenterPos(item)
}

/**
 * 旋转移动事件
 */
export function rotateMove(e: any, item: ItemElement) {
		let mouseX: number
		let mouseY: number
		
		const touchEvent = e as UniTouchEvent
		if (touchEvent.touches.length > 0) {
			mouseX = touchEvent.touches[0].clientX
			mouseY = touchEvent.touches[0].clientY
		} else {
			const mouseEvent = e as MouseEvent
			mouseX = mouseEvent.clientX
			mouseY = mouseEvent.clientY
		}
		
		// 计算图片中心点（考虑缩放）
		let centerX = item.left + (item.width * item.scale) / 2
		let centerY = item.top + (item.height * item.scale) / 2
		
		// 如果有缓存的中心点，使用缓存的
		if (item.centerPos != null) {
			centerX = item.centerPos['x'] as number
			centerY = item.centerPos['y'] as number
		}
		
		let touchX = item.touchX as number
		let touchY = item.touchY as number
		
		// 计算起始角度（触摸点相对于中心点）
		let startAngle = Math.atan2(touchY - centerY, touchX - centerX) * 180 / Math.PI
		
		// 计算当前角度（当前触摸点相对于中心点）
		let currentAngle = Math.atan2(mouseY - centerY, mouseX - centerX) * 180 / Math.PI
		
		// 计算角度差
		let deltaAngle = currentAngle - startAngle
		
		// 更新旋转角度（基于初始角度）
		let initialAngle = item.anglePre != null ? item.anglePre : 0
		let rawAngle = initialAngle + deltaAngle
		
		// 标准化角度到 -180 到 180
		while (rawAngle > 180) rawAngle -= 360
		while (rawAngle < -180) rawAngle += 360
		
		// 应用角度吸附效果
		item.angle = applyAngleSnap(rawAngle)
}

/**
 * 角度吸附函数
 * 当角度接近关键角度（0°、45°、90°、135°、180°、-45°、-90°、-135°）时自动吸附
 */
export function applyAngleSnap(angle: number): number {
		const snapTolerance = 5 // 吸附容差：5度
		const snapAngles = [0, 45, 90, 135, 180, -45, -90, -135] // 关键吸附角度
		
		// 检查是否接近任何关键角度
		for (let i = 0; i < snapAngles.length; i++) {
			let snapAngle = snapAngles[i]
			let diff = Math.abs(angle - snapAngle)
			
			// 处理 180° 和 -180° 的特殊情况
			if (snapAngle == 180 || snapAngle == -180) {
				let diff180 = Math.abs(Math.abs(angle) - 180)
				if (diff180 <= snapTolerance) {
					return snapAngle
				}
			} else if (diff <= snapTolerance) {
				return snapAngle
			}
		}
		
		// 如果不在吸附范围内，返回原始角度
		return angle
}

/**
 * 更新中心位置
 */
export function updateCenterPos(item: ItemElement) {
		item.centerPos = {
			x: item.left + item.width / 2,
			y: item.top + item.height / 2
		}
		item.x = item.centerPos!['x'] as number
		item.y = item.centerPos!['y'] as number
}

/**
 * 初始化位置
 */
export function initPos(item: ItemElement) {
		if (item == null) return
		item.centerPos = {
			x: item.left + item.width / 2,
			y: item.top + item.height / 2
		}
		item.x = item.centerPos!['x'] as number
		item.y = item.centerPos!['y'] as number
}

/**
 * 磁性吸附 - 四角缩放时
 */
export function applyMagneticAdsorption(item: ItemElement, designWidth: number, designHeight: number, allLineListData: GuideLine[]) {
		const tolerance = 4 // 吸附容差
		let distance = 0
		
		if (item.typetext == 'left-top') {
			let chosenGuides = getIshasDrawData(item, allLineListData)
			let leftGuide = chosenGuides['left']! as UTSJSONObject
			let leftDist = leftGuide['dist'] as number
			if (leftDist <= tolerance && leftGuide['guide'] != null) {
				item.isShowLineV = true
				let leftGuideObj = leftGuide['guide']! as GuideLine
				let leftOffset = leftGuide['offset'] as number
				item.selectLineLeft = leftGuideObj.left
				distance = item.left - (leftGuideObj.left - leftOffset)
				let elmW = item.elmW as number
				let elmH = item.elmH as number
				let elmX = item.elmX as number
				let elmY = item.elmY as number
				let scale = elmW / elmH
				elmX -= distance
				elmY -= distance * scale
				elmW += distance
				elmH += distance * scale
				item.elmX = elmX
				item.elmY = elmY
				item.elmW = elmW
				item.elmH = elmH
			} else {
				let topGuide = chosenGuides['top']! as UTSJSONObject
				let topDist = topGuide['dist'] as number
				if (topDist <= tolerance && topGuide['guide'] != null) {
					item.isShowLineH = true
					let topGuideObj = topGuide['guide']! as GuideLine
					let topOffset = topGuide['offset'] as number
					item.selectLineTop = topGuideObj.top
					distance = item.top - (topGuideObj.top - topOffset)
				let elmW = item.elmW as number
				let elmH = item.elmH as number
				let elmX = item.elmX as number
				let elmY = item.elmY as number
				let scale = elmW / elmH
				elmX -= distance / scale
				elmY -= distance
				elmW += distance / scale
				elmH += distance
					item.elmX = elmX
					item.elmY = elmY
					item.elmW = elmW
					item.elmH = elmH
				}
			}
			// 边界吸附
			if (item.left > -tolerance && item.left < tolerance) {
				distance = item.left
				let elmW = item.elmW as number
				let elmH = item.elmH as number
				let elmX = item.elmX as number
				let elmY = item.elmY as number
				let scale = elmW / elmH
				elmX -= distance
				elmY -= distance * scale
				elmW += distance
				elmH += distance * scale
				item.elmX = elmX
				item.elmY = elmY
				item.elmW = elmW
				item.elmH = elmH
				item.isShowLineV = true
				item.selectLineLeft = 0
			}
			
			item.left = item.elmX as number
			item.top = item.elmY as number
			item.width = item.elmW as number
			item.height = item.elmH as number
		}
		// 其他角的吸附逻辑类似...
}

/**
 * 磁性吸附 - 边拉伸时
 */
export function applyEdgeMagneticAdsorption(item: ItemElement, designWidth: number, designHeight: number, allLineListData: GuideLine[]) {
		const tolerance = 4
		let distance = 0
		
		if (item.typetext == 'right') {
			let right = designWidth - (item.left + item.width)
			let chosenGuides = getIshasDrawData(item, allLineListData)
			let leftGuide = chosenGuides['left']! as UTSJSONObject
			let leftDist = leftGuide['dist'] as number
			
			if (leftDist <= tolerance && leftGuide['guide'] != null) {
				item.isShowLineV = true
				let leftGuideObj = leftGuide['guide']! as GuideLine
				let leftOffset = leftGuide['offset'] as number
				item.selectLineLeft = leftGuideObj.left
				distance = (leftGuideObj.left - leftOffset) - item.left
				let elmW = item.elmW as number
				elmW += distance
				item.elmW = elmW
			} else if (right > -tolerance && right < tolerance) {
				distance = right
				let elmW = item.elmW as number
				elmW += distance
				item.elmW = elmW
				item.isShowLineV = true
				item.selectLineLeft = designWidth - 0.5
			}
			
			item.width = item.elmW as number
		} else if (item.typetext == 'left') {
			let chosenGuides = getIshasDrawData(item, allLineListData)
			let leftGuide = chosenGuides['left']! as UTSJSONObject
			let leftDist = leftGuide['dist'] as number
			
			if (leftDist <= tolerance && leftGuide['guide'] != null) {
				item.isShowLineV = true
				let leftGuideObj = leftGuide['guide']! as GuideLine
				let leftOffset = leftGuide['offset'] as number
				item.selectLineLeft = leftGuideObj.left
				distance = item.left - (leftGuideObj.left - leftOffset)
				let elmX = item.elmX as number
				let elmW = item.elmW as number
				elmX -= distance
				elmW += distance
				item.elmX = elmX
				item.elmW = elmW
			} else if (item.left > -tolerance && item.left < tolerance) {
				distance = item.left
				let elmX = item.elmX as number
				let elmW = item.elmW as number
				elmX -= distance
				elmW += distance
				item.elmX = elmX
				item.elmW = elmW
				item.isShowLineV = true
				item.selectLineLeft = 0
			}
			
			item.left = item.elmX as number
			item.width = item.elmW as number
		} else if (item.typetext == 'top') {
			let chosenGuides = getIshasDrawData(item, allLineListData)
			let topGuide = chosenGuides['top']! as UTSJSONObject
			let topDist = topGuide['dist'] as number
			
			if (topDist <= tolerance && topGuide['guide'] != null) {
				item.isShowLineH = true
				let topGuideObj = topGuide['guide']! as GuideLine
				let topOffset = topGuide['offset'] as number
				item.selectLineTop = topGuideObj.top
				distance = item.top - (topGuideObj.top - topOffset)
				let elmY = item.elmY as number
				let elmH = item.elmH as number
				elmY -= distance
				elmH += distance
				item.elmY = elmY
				item.elmH = elmH
			} else if (item.top > -tolerance && item.top < tolerance) {
				distance = item.top
				let elmY = item.elmY as number
				let elmH = item.elmH as number
				elmY -= distance
				elmH += distance
				item.elmY = elmY
				item.elmH = elmH
				item.isShowLineH = true
				item.selectLineTop = 0
			}
			
			item.top = item.elmY as number
			item.height = item.elmH as number
		} else if (item.typetext == 'bottom') {
			let bottom = designHeight - (item.top + item.height)
			let chosenGuides = getIshasDrawData(item, allLineListData)
			let topGuide = chosenGuides['top']! as UTSJSONObject
			let topDist = topGuide['dist'] as number
			
			if (topDist <= tolerance && topGuide['guide'] != null) {
				item.isShowLineH = true
				let topGuideObj = topGuide['guide']! as GuideLine
				let topOffset = topGuide['offset'] as number
				item.selectLineTop = topGuideObj.top
				distance = (topGuideObj.top - topOffset) - item.top
				let elmH = item.elmH as number
				elmH += distance
				item.elmH = elmH
			} else if (bottom > -tolerance && bottom < tolerance) {
				distance = bottom
				let elmH = item.elmH as number
				elmH += distance
				item.elmH = elmH
				item.isShowLineH = true
				item.selectLineTop = designHeight - 0.6
			}
			
			item.height = item.elmH as number
		}
}

/**
 * 磁性吸附 - 拖拽时
 */
export function applyDragMagneticAdsorption(item: ItemElement, designWidth: number, designHeight: number, allLineListData: GuideLine[]) {
		const tolerance = 5
		
		let chosenGuides = getIshasDrawData(item, allLineListData)
		let leftGuide = chosenGuides['left']! as UTSJSONObject
		let leftDist = leftGuide['dist'] as number
		
		// 垂直辅助线吸附
		if (leftDist <= tolerance && leftGuide['guide'] != null) {
			let leftGuideObj = leftGuide['guide']! as GuideLine
			let leftOffset = leftGuide['offset'] as number
			item.elmX = leftGuideObj.left - leftOffset
			item.isShowLineV = true
			item.selectLineLeft = leftGuideObj.left
		} else {
			// 中心线吸附
			if (item.left > (designWidth / 2 - tolerance) && item.left < (designWidth / 2 + tolerance)) {
				item.elmX = designWidth / 2
			} else if (item.left > (designWidth / 2 - item.width - tolerance) && item.left < (designWidth / 2 - item.width + tolerance)) {
				item.elmX = designWidth / 2 - item.width
			} else if (item.left > -tolerance && item.left < tolerance) {
				item.elmX = 0
			} else if (item.left > designWidth - item.width - tolerance && item.left < designWidth - item.width + tolerance) {
				item.elmX = designWidth - item.width
			} else if (item.left > ((designWidth - item.width) / 2 - tolerance) && item.left < ((designWidth - item.width) / 2 + tolerance)) {
				item.elmX = (designWidth - item.width) / 2
			}
		}
		
		// 水平辅助线吸附
		let topGuide = chosenGuides['top']! as UTSJSONObject
		let topDist = topGuide['dist'] as number
		if (topDist <= tolerance && topGuide['guide'] != null) {
			let topGuideObj = topGuide['guide']! as GuideLine
			let topOffset = topGuide['offset'] as number
			item.elmY = topGuideObj.top - topOffset
			item.isShowLineH = true
			item.selectLineTop = topGuideObj.top
		} else {
			// 中心线吸附
			let centerY = designHeight / 2
			if (item.top > (centerY - item.height - tolerance) && item.top < (centerY - item.height + tolerance)) {
				item.elmY = centerY - item.height
			} else if (item.top > -tolerance && item.top < tolerance) {
				item.elmY = 0
			} else if (item.top > designHeight - item.height - tolerance && item.top < designHeight - item.height + tolerance) {
				item.elmY = designHeight - item.height
			} else if (item.top > (centerY - (item.height / 2) - tolerance) && item.top < (centerY - (item.height / 2) + tolerance)) {
				item.elmY = centerY - (item.height / 2)
			}
		}
}

/**
 * 检测是否需要显示辅助线
 */
export function getIshasDrawData(item: ItemElement, allLineListData: GuideLine[]): UTSJSONObject {
		let pos = {
			left: item.left,
			top: item.top
		}
		
		let chosenGuides: UTSJSONObject = {
			top: { dist: 5 } as UTSJSONObject,
			left: { dist: 5 } as UTSJSONObject
		} as UTSJSONObject
		
		let selectLineListData = addallLineListDataXAndY(item)
		
		if (allLineListData.length > 0 && selectLineListData.length > 0) {
			allLineListData.forEach((guide: GuideLine) => {
				selectLineListData.forEach((elemGuide: UTSJSONObject) => {
					let elemType = elemGuide['type'] as string
					if (guide.type == elemType) {
						let prop = guide.type == 'h' ? 'top' : 'left'
						let elemPropVal = elemGuide[prop] as number
						let guidePropVal = guide[prop] as number
						let posPropVal = pos[prop] as number
						let d = Math.abs(elemPropVal - guidePropVal)
						let chosenProp = chosenGuides[prop] as UTSJSONObject
						let chosenDist = chosenProp['dist'] as number
						if (d < chosenDist) {
							chosenProp['dist'] = d
							chosenProp['offset'] = elemPropVal - posPropVal
							chosenProp['guide'] = guide
						}
					}
				})
			})
		}
		
		return chosenGuides
}

/**
 * 添加元素的所有参考线坐标
 */
export function addallLineListDataXAndY(item: ItemElement): UTSJSONObject[] {
		if (item.angle != 0) {
			// 旋转情况下的参考线计算
			let center = {
				x: item.left + item.width / 2,
				y: item.top + item.height / 2
			}
			let topLeft = { x: item.left, y: item.top }
			let topRight = { x: item.left + item.width, y: item.top }
			let bottomLeft = { x: item.left, y: item.top + item.height }
			let bottomRight = { x: item.left + item.width, y: item.top + item.height }
			
			let point1 = calNewPos(topLeft, center, item.angle)
			let point2 = calNewPos(topRight, center, item.angle)
			let point3 = calNewPos(bottomLeft, center, item.angle)
			let point4 = calNewPos(bottomRight, center, item.angle)
			
			return [
				{ type: 'h', left: point1.x, top: point1.y },
				{ type: 'h', left: point2.x, top: point2.y },
				{ type: 'h', left: point3.x, top: point3.y },
				{ type: 'h', left: point4.x, top: point4.y },
				{ type: 'v', left: point1.x, top: point1.y },
				{ type: 'v', left: point2.x, top: point2.y },
				{ type: 'v', left: point3.x, top: point3.y },
				{ type: 'v', left: point4.x, top: point4.y }
			]
		} else {
			return [
				{ type: 'h', left: item.left, top: item.top },
				{ type: 'h', left: item.left, top: item.top + item.height },
				{ type: 'v', left: item.left, top: item.top },
				{ type: 'v', left: item.left + item.width, top: item.top },
				{ type: 'h', left: item.left, top: item.top + item.height / 2 },
				{ type: 'v', left: item.left + item.width / 2, top: item.top }
			]
		}
}

/**
 * 通过旋转角度计算新坐标
 */
export function calNewPos(origin: UTSJSONObject, center: UTSJSONObject, angle: number): UTSJSONObject {
		let radian = Math.PI / 180 * angle
		let originX = origin['x'] as number
		let originY = origin['y'] as number
		let centerX = center['x'] as number
		let centerY = center['y'] as number
		let newX = (originX - centerX) * Math.cos(radian) - (originY - centerY) * Math.sin(radian) + centerX
		let newY = (originY - centerY) * Math.cos(radian) + (originX - centerX) * Math.sin(radian) + centerY
		return {
			x: parseFloat(newX.toFixed(4)),
			y: parseFloat(newY.toFixed(4))
		}
}

