;(function () {
	const watermarker_name = 'WEOffice_watermarkerGroup'
	let waterMakerGroup = []

	/**
	 * 创建水印文字
	 */
	function _createTextMark(
		container,
		content,
		fontfamily,
		fontcolor,
		fontsize,
		ratation,
		left,
		top,
		height,
		width
	) {
		let txt = container.Shapes.AddTextEffect(
			0,
			content,
			fontfamily,
			fontsize,
			false,
			false,
			left,
			top
		)
		txt.Fill.Solid()
		txt.Fill.ForeColor.RGB = fontcolor
		txt.Line.Visible = false
		txt.Fill.Transparency = 0.5
		txt.LockAspectRatio = true
		if (!!height) txt.Height = height
		if (!!width) txt.Width = width
		txt.WrapFormat.AllowOverlap = true
		txt.Rotation = ratation
		txt.WrapFormat.Side = 3
		txt.WrapFormat.Type = 3
		return txt
	}

	function SetWaterMarkTest(
		indoc,
		index,
		content = '清样',
		fontfamily = '仿宋',
		fontcolor = 12566463,
		fontsize = 12,
		ratation = 315,
		viewIndex
	) {
		let app = wps.WpsApplication()
		let doc = indoc || app.ActiveDocument

		app.ActiveWindow.ActivePane.View.SeekView = viewIndex || index
		let pageSetup = doc.Sections.Item(1).PageSetup
		let maxwidth = pageSetup.PageWidth
		let maxheight = pageSetup.PageHeight

		let left = 0,
			initLeft = -10,
			top = 0

		let markcontainer = doc.Sections.Item(1).Headers.Item(index)
		//添加水印文字

		let first = _createTextMark(
			markcontainer,
			content,
			fontfamily,
			fontcolor,
			fontsize,
			ratation,
			left,
			top
		)

		let unitWidth = Math.abs(Math.cos(ratation) * first.Width),
			unitHeight = Math.abs(Math.sin(ratation) * first.Width)

		//每页放置16个最多
		const maxCount = 4
		const margin = { vertical: 0, horizontal: 0 }
		if (maxwidth > unitWidth * maxCount)
			margin.horizontal = (maxwidth - unitWidth * maxCount) / (maxCount - 1)
		if (maxheight > unitWidth * maxCount)
			margin.vertical = (maxheight - unitHeight * maxCount) / (maxCount - 1)

		first.Delete()
		top = unitHeight
		left = initLeft
		let arr = []

		while (top < maxheight && left < maxwidth) {
			const txt = _createTextMark(
				markcontainer,
				content,
				fontfamily,
				fontcolor,
				fontsize,
				ratation,
				left,
				top,
				null,
				null
			)
			arr.push(txt.Name)
			left = left + unitWidth + margin.horizontal
			if (top < maxheight && left > maxwidth) {
				top += unitHeight + margin.vertical
				left = initLeft
			}
		}

		var waterMark = markcontainer.Shapes.Range(arr).Group()
		waterMark.Name = watermarker_name

		waterMark.Select()
		wps.Application.Selection.Copy()

		if (doc.Sections.Count > 1) {
			for (var i = 2; i <= doc.Sections.Count; i++) {
				h = doc.Sections.Item(i).Headers.Item(index)
				if (h.Shapes.Item(watermarker_name) == null) {
					wps.Application.Selection.SetRange(h.Range.Start, h.Range.Start)
					wps.Application.Selection.Paste()
				}
			}
		}
		waterMakerGroup[index] = waterMark
		app.ActiveWindow.ActivePane.View.SeekView = 0
	}

	function getGroup(indoc, headerindex) {
		let doc = indoc || wps.WpsApplication().ActiveDocument
		let header = doc.Sections.Item(headerindex).Headers.Item(headerindex)
		return header.Shapes.Item(watermarker_name)
	}

	function SetWaterMark(
		doc,
		content = '清样',
		fontfamily = '仿宋',
		fontsize = 12,
		fontcolor = 12566463,
		ratation = 315
	) {
		loginfo('设置水印')
		//首页不同
		var DifferentFirstPageHeaderFooter =
			!!doc.PageSetup.DifferentFirstPageHeaderFooter
		//奇偶页不同
		var OddAndEvenPagesHeaderFooter =
			!!doc.PageSetup.OddAndEvenPagesHeaderFooter

		if (!DifferentFirstPageHeaderFooter && !OddAndEvenPagesHeaderFooter) {
			SetWaterMarkTest(
				doc,
				2,
				content,
				fontfamily,
				fontcolor,
				fontsize,
				ratation,
				9
			)
		} else if (OddAndEvenPagesHeaderFooter && !DifferentFirstPageHeaderFooter) {
			SetWaterMarkTest(
				doc,
				1,
				content,
				fontfamily,
				fontcolor,
				fontsize,
				ratation
			)
			SetWaterMarkTest(
				doc,
				3,
				content,
				fontfamily,
				fontcolor,
				fontsize,
				ratation
			)
		} else {
			SetWaterMarkTest(
				doc,
				2,
				content,
				fontfamily,
				fontcolor,
				fontsize,
				ratation
			)
			var pagecount = doc.ActiveWindow.Panes.Item(1).Pages.Count
			if (DifferentFirstPageHeaderFooter && pagecount > 2) {
				SetWaterMarkTest(
					doc,
					1,
					content,
					fontfamily,
					fontcolor,
					fontsize,
					ratation
				)
			}
			if (OddAndEvenPagesHeaderFooter && pagecount > 1) {
				SetWaterMarkTest(
					doc,
					3,
					content,
					fontfamily,
					fontcolor,
					fontsize,
					ratation
				)
			}
		}

		wps.Application.Selection.SetRange(0, 0)
		wps.Application.Selection.Text = ' '
		wps.Application.Selection.Copy()
		wps.Application.Selection.Delete()

		loginfo('设置水印完成')
	}
	/**
	 * 清理水印
	 */
	function ClearWaterMark(indoc) {
		//if (waterMakerGroup) waterMakerGroup.Delete()
		//双保险
		// ;[1, 2, 3].forEach((v) => {
		// 	let max = indoc.Sections.Count
		// 	for (let i = 1; i <= max; i++) {
		// 		let group = indoc.Sections.Item(i)
		// 			.Headers.Item(v)
		// 			.Shapes.Item(watermarker_name)
		// 		if (group) {
		// 			console.log('删除水印')
		// 			group.Delete()
		// 		} else {
		// 			console.log('未找到水印')
		// 		}
		// 	}
		// })
		// wps.Application.ActiveWindow.ActivePane.View.SeekView = 0
		waterMakerGroup.forEach((v) => v.Delete())
	}

	window.WaterMark = {
		SetWaterMark,
		SetWaterMarkTest,
		ClearWaterMark,
	}
})(window)
