import {Module} from 'vuex'
import {RootState} from '../index'

interface DesignDetail{
    designCollectionList:any,
    likeDesignCollectionList:any,
    deleteDesignCollectionList:any,
    userGroupId:any,
    designCollectionId:any,
    templateId:any,
    templateImgUrl:any,
    designId:any,
	showSketchList:any,

	toProductImageList:any,// 产品图片
	relightList:any,// 灯光列表
	uploadElement:any,// 上传元素  toproduct、relight、pose
	poseTransfer:any,// 姿态转换
	deReconstruction:any,// 拼贴
	patternMaking3D:any,//3d
	canvasData:any,// 拼贴canvas
}

const HomeStoreModule : Module<DesignDetail,RootState> = {
    state:{
        designCollectionList:[],
        likeDesignCollectionList:[],
        deleteDesignCollectionList:[],
        userGroupId:'',
        designCollectionId:'',
        templateId:'',
        templateImgUrl:'',
        designId:'',
		showSketchList:[],
		toProductImageList:{
			list:[],
			likedList:[],
		},
		relightList:{
			list:[],
			likedList:[],
		},
		uploadElement:[],
		lastFrameList:[],
		poseTransfer:{
			list:[],
			likedList:[],
		},
		deReconstruction:[{
			isLike:false,
			url:'',
			category:'123',
			categoryValue:'23',
			categoryShow:false
		}],
		deReconstructionUploadImages:[],
		patternMaking3D:{
			collectionElementId:null,
			threeDSimpleId:null,
			printMinioUrl:null,
			url:'',
			x:null,
			y:null,
		},
		// patternMaking3D:null,
		canvasData:{
			deReconstruction:null,
			canvas:null
		}
    },
    mutations:{
		setshowSketchList(state,data){
            state.showSketchList = data
        },
		setPoseTransferLastFrameList(state,data){
			// 支持两种方式：set 替换整个列表；add/删除与 uploadElement 一致
			if(data.str === 'set'){
				state.lastFrameList = data.list || []
				return
			}else{
				state.lastFrameList = []
			}
		},
        setDesignCollectionList(state,data){
            state.designCollectionList = data
        },
        setLikeDesignCollectionList(state,data){
			data = data || []
            state.likeDesignCollectionList = data
        },
		setRelightList(state,data){
			if(data.str == 'add'){
				if(data.list){
					state.relightList.list.unshift(...data.list)
				}
				if(data.likedList){
					state.relightList.likedList.unshift(...data.likedList)
				}
			}else{
				if(data.list){
					state.relightList.list.splice(data.index,1)
				}else if(data.likedList){
					state.relightList.likedList.splice(data.index,1)
				}
			}
        },
		setUploadElement(state,data){
			if(data?.isGetModule){
				state.uploadElement = []
			}
			if(data.str == 'add'){
	            state.uploadElement.unshift(...data.list)
			}else{
	            state.uploadElement.splice(data.index,1)
			}
		},
		setPoseTransfer(state,data){
			if(data.str == 'add'){
				if(data.list){
					data.list.forEach((item:any) => {
						item.url = item.firstFrameUrl
					});
					state.poseTransfer.list.unshift(...data.list)
				}
				if(data.likedList){
					data.likedList.forEach((item:any) => {
						item.url = item.firstFrameUrl
					});
					state.poseTransfer.likedList.unshift(...data.likedList)
				}
			}else{
				if(data.list){
					state.poseTransfer.list.splice(data.index,1)
				}else if(data.likedList){
					state.poseTransfer.likedList.splice(data.index,1)
				}
			}
        },
		toolsClear(state){
			console.log('toolsClear')
			state.relightList.likedList = []
			state.uploadElement = []
			state.toProductImageList.likedList = [] 
			state.poseTransfer.likedList = []
		},
		setDeReconstruction(state,data){
			if(data.str == 'add'){
				state.deReconstruction = []
				let obj = {
					...data.list[0],
				}
	            state.deReconstruction.unshift(obj)
			}else{
	            state.deReconstruction.splice(data.index,1)
			}
        },
		setDeReconstructionUploadImages(state,data){
			if(data.str == 'add'){
	            state.deReconstructionUploadImages.unshift(...data.list)
			}else{
	            state.deReconstructionUploadImages.splice(data.index,1)
			}
        },
		setPatternMaking3D(state,data){
			for (const key in data) {
				if(data[key] == undefined)continue
				if(key == 'x' || key == 'y'){
					state.patternMaking3D[key] = Number(data[key].toFixed(3))	
				}else{
					state.patternMaking3D[key] = data[key]	
				}
				// state.patternMaking3D[key] = data[key]	
			}
		},
		setCanvasData(state,data){
			state.canvasData[data.type] = data.file
		},
		setToProductImage(state,data){
			if(data.str == 'add'){
				if(data.list){
					state.toProductImageList.list.unshift(...data.list)
				}
				if(data.likedList){
					state.toProductImageList.likedList.unshift(...data.likedList)
				}
			}else{
				if(data.list){
					state.toProductImageList.list.splice(data.index,1)
				}else if(data.likedList){
					state.toProductImageList.likedList.splice(data.index,1)
				}
			}
        },
		setDeleteDesignCollectionList(state,data){
            let obj = state.designCollectionList.splice(data,1)
			// state.deleteDesignCollectionList.unshift(...obj)
        },
		cancelDeleteDesignCollectionList(state,data){
            let obj = state.deleteDesignCollectionList.splice(data,1)
			state.designCollectionList.unshift(...obj)
        },
        setSingleDesignCollectionList(state,data){
            state.designCollectionList[data.index] = data.design
        },
        setSingleLikeDesignCollectionList(state,data){
            state.likeDesignCollectionList[data.index] = data.design
        },
        addDesignCollectionList(state,data){
            state.designCollectionList.unshift(...data)
        },
        addLikeDesignCollectionList(state,data){
            state.likeDesignCollectionList.unshift(data)
        },
        deleteDesignCollectionList(state,index){
            state.designCollectionList.splice(index,1)
        },
        deleteLikeDesignCollectionList(state,index){
            state.likeDesignCollectionList.splice(index,1)
        },
        setUserGroupId(state,data){
            state.userGroupId = data
        },
        deleteUserGroupId(state){
            state.userGroupId = ''
        },
        setDesignCollectionId(state,data){
            state.designCollectionId = data
        },
        deleteDesignCollectionId(state,data){
            state.designCollectionId = ''
        },
        setDesignId(state,data){
            state.designId = data
        },
        deleteDesignId(state){
            state.designId = ''
        },
        clearAllCollection(state){
			console.log('clearAll')
            state.designCollectionList=[]
            state.likeDesignCollectionList = []
            state.designCollectionId = ''
			state.deleteDesignCollectionList = []
			state.relightList = {
				list:[],
				likedList:[],
			}
			state.uploadElement = []
			state.toProductImageList = {
				list:[],
				likedList:[],
			}
			state.poseTransfer = {
				list:[],
				likedList:[],
			}
			state.deReconstruction = null
			state.deReconstructionUploadImages = []
			state.patternMaking3D = {
				collectionElementId:null,
				threeDSimpleId:null,
				printMinioUrl:null,
				url:null,
				x:null,
				y:null,
			}
			state.canvasData = {
				deReconstruction:null,
				canvas:null
			}
			state.showSketchList = []
        },
        setTemplateData(state,data){
            state.templateId=data.id
            state.templateImgUrl = data.url
        },
        clearTemplateData(state){
            state.templateId= ''
            state.templateImgUrl = ''
        },
		

        
    },
    actions:{

    }
}

export default HomeStoreModule