import {
	PAPER_W,
	PAPER_H,
	SVG_BORDER_PROP,
	CONTENT_MT,
	CONTENT_ML,
	ANCHORPROP,
	CONTENT_START_X,
	CONTENT_START_Y,
	INFO_W,
	INFO_H,
	INFO_ITEM_GAP,
	INFO_ITEM_MARGIN,
	ID_LENGTH,
	INFO_LEFT_X,
	INFO_LEFT_W,
	ID_TITLE_H,
	ID_TITLE_Y,
	NUMBER_CELL_H,
	NUMBER_ITEM_W,
	NUMBER_ITEM_H,
	NUMBER_ITEM_MT,
	NUMBER_ITEM_ML
} from '../../utils/sheetConfig'
export default {
	state: {
		count: 0,
		pages: 1,
		isNewPage: false,
		isAutoCreate: false,
		needFixArr: [26],
		paperItem: {
			name: ''
		},
		editorInfos: {

		},
		sheetConfig: {
			// 答题卡宽高
			pageConfig: {
				width: PAPER_W,
				height: PAPER_H
			},
			// 答题卡四个定位锚点
			pageAnchor: [
				[ANCHORPROP.gapX, ANCHORPROP.gapY, ANCHORPROP.width, ANCHORPROP.height],
				[PAPER_W - ANCHORPROP.width - ANCHORPROP.gapX, ANCHORPROP.gapY, ANCHORPROP.width, ANCHORPROP
					.height
				],
				[ANCHORPROP.gapX, PAPER_H - ANCHORPROP.height - ANCHORPROP.gapY, ANCHORPROP.width, ANCHORPROP
					.height
				],
				[PAPER_W - ANCHORPROP.width - ANCHORPROP.gapX, PAPER_H - ANCHORPROP.height - ANCHORPROP.gapY,
					ANCHORPROP.width, ANCHORPROP.height
				]
			],
			// 答题卡外框
			outerRect: [SVG_BORDER_PROP.x, SVG_BORDER_PROP.y, SVG_BORDER_PROP.width, SVG_BORDER_PROP.height],
			// 基本信息框
			baseInfoRect: [CONTENT_START_X, CONTENT_START_Y, INFO_W, INFO_H],
			// 客观题框
			objectiveRect: [],
			// 填空题框
			completeRect: [],
			// 主观题框
			subjectiveRect: []
		},
		config: {
			"id":null,
			"columns": null, //列数
			"pageCount": 1,

			"pageWidth": SVG_BORDER_PROP.width, //页面大小(回字组成的)
			"pageHeight": SVG_BORDER_PROP.height , //页面大小(回字组成的)

			"pageNumBlockCount": 3, //页码块数量
			"pageNumStartValue": 1, //页码起始值, 起始是3个空格方块开始,写1; 2个空格1个实心,写0

			// "threshValue": [190, 220, 10], //二值化区间(pc)-->不同扫描仪的结果图像数据有差距, 按需调整
			// "threshValuePhone": [190, 220, 10], //二值化区间(手机)

			// "huiSize": ANCHORPROP.width, //单个整体回字大小
			// "huiSize": 24, //绘制有误差，先暂时按照实际大小写固定值（待确认）
			// "minHuiSize": 0.005, //最小回字的大小比列, 以回字最中间的方块为最小
			// "maxHuiSize": 0.1, //最大回字的大小比列, 以回字最外层的方块为最大
			// "notHuisRect": [0.3, 0.3, 0.4, 0.4], //不检测回字区域(pc版辅助效果20%, 手机版50%)

			//内容块 vblockCount为该模块竖向的方块数 hblockCount为该模块横向方块数
			"contents": [],
			
			"infoMode":'number',
			"infoPos":[],
			"objectivePos":[],
			"mode":"A4"
		}
	},

	mutations: {
		setInfoMode(state, val) {
			state.config.infoMode = val
		},
		setInfoPos(state, val) {
			state.config.infoPos = val
		},
		setObjectivePos(state, val) {
			state.config.objectivePos = val
		},
		setCreateModal(state, val) {
			state.isAutoCreate = val === 'auto'
		},
		setPaper(state, val) {
			state.paperItem = val
			state.config.id = val.sheetId || null
		},
		setPaperItems(state, val) {
			state.paperItem.item = val
		},
		setColumnCount(state, val) {
			state.config.columns = 1
		},
		// 设置信息填写区域的配置数据
		setInfoConfig(state, val) {
			val.index = 0
			val.x = Number(val.x.toFixed())
			val.y = Number(val.y.toFixed())
			val.width = Number(val.width.toFixed())
			val.height = Number(val.height.toFixed())
			val.count = val.hblockCount
			val.id = val.pageNum + '-' + val.index
			if(state.config.contents.length){
				let infoItem = state.config.contents.find(i => i.type === 0)
				if(infoItem){
					// infoItem.hblockCount = val.hblockCount
				}else{
					delete val.vblockCount
					delete val.hblockCount
					state.config.contents.push(val)
				}
			}else{
				state.config.contents.push(val)
			}
		},
		// 设置客观题区域的配置数据
		setObjectiveConfig(state,val){
			val.index = state.config.contents.filter(i => i.pageNum === val.pageNum).length + 1
			val.x = Number(val.x.toFixed())
			val.y = Number(val.y.toFixed())
			val.width = Number(val.width.toFixed())
			val.height = Number(val.height.toFixed())
			val.id = val.pageNum + '-' + val.index
			val.index = 1
			if(state.config.contents.length){
				let infoItem = state.config.contents.find(i => i.type === 1)
				if(infoItem){
					infoItem.height= val.height
					// infoItem.vblockCount= val.vblockCount
				}else{
					delete val.vblockCount
					delete val.hblockCount
					state.config.contents.push(val)
				}
			}else{
				delete val.vblockCount
				delete val.hblockCount
				state.config.contents.push(val)
			}
		},
		setSubjectiveConfig(state,val){
			val.index = state.config.contents.filter(i => i.pageNum === val.pageNum).length + 1
			val.x = Number(val.x.toFixed())
			val.y = Number(val.y.toFixed())
			val.width = Number(val.width.toFixed())
			val.height = Number(val.height.toFixed())
			val.id = val.pageNum + '-' + val.index
			// 如果是补充框 ID要保持和上一个ID一致
			if(val.index === 1 && val.isFix){
				let prePageItems = state.config.contents.filter(i => i.pageNum === val.pageNum - 1)
				let preItem = prePageItems[prePageItems.length - 1]
				val.id = preItem.id
			}
			if(state.config.contents.length){
				let infoItem = state.config.contents.find(i => i.id === val.id && i.pageNum === val.pageNum)
				if(infoItem){
					infoItem.height= val.height
					infoItem.y= val.y
					infoItem.pageNum = val.pageNum
				}else{
					val.index = val.startOrder
					delete val.startOrder
					state.config.contents.push(val)
				}
			}else{
				val.index = val.startOrder
				delete val.startOrder
				state.config.contents.push(val)
			}
		},
		deleteItem(state, val) {
			let index = state.paperItem.item.map(i => i.id).indexOf(val)
			state.paperItem.item.splice(index, 1)
		},
		addPage(state) {
			state.pages++
		},
		clearPage(state) {
			 state.pages = 1
			 state.config.contents = state.config.contents.filter(i => i.type !== 2)
		},
		clearAllConfig(state){
			state.config.contents = []
		},
		changeNewPage(state, val) {
			state.isNewPage = val
		},
		addFixItem(state, val) {
			state.needFixArr.push(val)
		},
		clearFixArr(state) {
			state.needFixArr = []
		},
		clearIsNewPage(state) {
			state.isNewPage = false
			state.editorInfos = {

			}
		},
		setEditorHeight(state, val) {
			if (state.editorInfos[val.id]) {
				state.editorInfos[val.id].height = val.height
				if (val.content) {
					state.editorInfos[val.id].content = val.content
				}
			} else {
				state.editorInfos[val.id] = {
					height: val.height,
					content: val.content
				}
			}
		}
	},

}
