import {Module,useStore} from 'vuex'
import {RootState} from '../index'
import homeStore from '../homeStore/homeStore'
import workspace from '../workspace/workspace'
import { message } from "ant-design-vue";
import { useI18n } from "vue-i18n";
import GO from '@/tool/GO';
interface UploadFiles{
	moodboard:any,
	disposeMoodboard:any,
	moodboardPosition:any,
    moodboardFiles:any,
	moodboardGenerateFiles:any,
	moodboardMaterialFiles:any,
	sketchboard:any,
    sketchboardFiles:any,
	sketchGenerateFiles:any,
	sketchMaterialFiles:any,
	showSketchboard:any,
	printboard:any,
    printboardFiles:any,
	printGenerateFiles:any,
	printMaterialFiles:any,
    colorBoards:any,
    marketingSketchFiles:any,
    allBoardData:any, //备份的所有数据，点finish之后备份
    moodTemplateId:any,
	chooseIsDesign:any,
	modularData:any,
}

const UploadFilesModule : Module<UploadFiles,RootState> = {
    state:{
		moodboard:[],
		disposeMoodboard:[],
		moodboardPosition:{},
        moodboardFiles:[],
		moodboardGenerateFiles:[],
		moodboardMaterialFiles:[],
		sketchboard:[],
        sketchboardFiles:[],
		sketchGenerateFiles:[],
		sketchMaterialFiles:[],
		showSketchboard:[],
		printboard:[],
        printboardFiles:[],
		printGenerateFiles:[],
		printMaterialFiles:[],
        colorBoards:[],
        marketingSketchFiles:[],
        allBoardData:{
			disposeMoodboard:[],
			moodboardPosition:null,
			moodboardFiles:[],
			moodTemplateId:'',
			printboardFiles:[],
			sketchboardFiles:[],
			colorBoards:[],
		},
        moodTemplateId:'',
		chooseIsDesign:{value:true},
		modularData:{
			design:{
				userlikeGroupId:null,
				likeData:[],
			},
			toProduct:null,
			relight:null,
			poseTransfer:null,
			canvas:null,
			patternMaking3D:null,
			deReconstruction:null,
		},
    },
    mutations:{
		
        setMoodboardFile(state,files){
			state.moodboardFiles = files
			state.moodboard = [...state.moodboardFiles,...state.moodboardGenerateFiles,...state.moodboardMaterialFiles]	
        },
		setDisposeMoodboard(state,files){
			state.disposeMoodboard = [files]
        },
		setDisposeMoodboardPosition(state,files){
			state.moodboardPosition = files
        },
		setMoodboardGenerateFiles(state,files){
			state.moodboardGenerateFiles = files
			state.moodboard = [...state.moodboardFiles,...state.moodboardGenerateFiles,...state.moodboardMaterialFiles]	
        },
		setMoodboardMaterialFiles(state,files){
			state.moodboardMaterialFiles = files
			state.moodboard = [...state.moodboardFiles,...state.moodboardGenerateFiles,...state.moodboardMaterialFiles]	
        },
		addGenerateMaterialFils(state,data:any){
			let file
			let arr
			let maxImg = 8
			if(data.type_.type2 == 'Sketchboard'){
				maxImg = 20
			}else if(data.type_.type2 == 'Printboard'){
				maxImg = 16
			}
			if(data.type_.type1 == 'generate'){//判断是generate||library
				data.resData.designType = 'Generate'
				if(data.type_.type2 == 'Moodboard'){
					file = state.moodboardGenerateFiles
				}else if(data.type_.type2 == 'Sketchboard'){
					file = state.sketchGenerateFiles
				}else if(data.type_.type2 == 'Printboard'){
					file = state.printGenerateFiles
				}
			}else{
				data.resData.designType = 'Library'
				if(data.type_.type2 == 'Moodboard'){
					file = state.moodboardMaterialFiles
				}else if(data.type_.type2 == 'Sketchboard'){
					file = state.sketchMaterialFiles
				}else if(data.type_.type2 == 'Printboard'){
					file = state.printMaterialFiles
				}
			}
			if(data.type_.type2 == 'Moodboard'){//判断选中图片数量
				arr = state.moodboard
			}else if(data.type_.type2 == 'Sketchboard'){
				arr = state.sketchboard
			}else{
				arr = state.printboard
			}
			
			if(file.length == 0){
				if(arr.length >= maxImg){
					// message.warning(GO.jsContent1)
					message.warning(data.jsContent1)
				}else{
					data.collectionChecked = true
					file.push(data)
				}
			}else{
				let str = true
				for (let index = 0; index < file.length; index++) {
					if(file[index].id == data.id){
						// data.id_ = GO.id++
						str = false
					}
				}
				if(str){
					if(arr.length >= maxImg){
						message.warning(data.jsContent1)
					}else{
						data.collectionChecked = true
						file.push(data)
					}
				}else if(data.state == 'delete'){
					//重复选中后取消选择改为无反应
					data.collectionChecked = false
					delete data.state
					file = file.filter((v:any)=> v.id != data.id)
				}
			}
			if(data.type_.type1 == 'generate'){
				if(data.type_.type2 == 'Moodboard'){
					state.moodboardGenerateFiles = file 
				}else if(data.type_.type2 == 'Sketchboard'){
					state.sketchGenerateFiles = file 
				}else if(data.type_.type2 == 'Printboard'){
					state.printGenerateFiles = file
				}
			}else{
				if(data.type_.type2 == 'Moodboard'){
					state.moodboardMaterialFiles = file 
				}else if(data.type_.type2 == 'Sketchboard'){
					state.sketchMaterialFiles = file 
				}else if(data.type_.type2 == 'Printboard'){
					state.printMaterialFiles = file
				}
			}
			state.moodboard = [...state.moodboardFiles,...state.moodboardGenerateFiles,...state.moodboardMaterialFiles]	
			state.sketchboard = [...state.sketchboardFiles,...state.sketchGenerateFiles,...state.sketchMaterialFiles]	
			state.printboard = [...state.printboardFiles,...state.printGenerateFiles,...state.printMaterialFiles]	
        },
		setSketchboardFile(state,files){
            state.sketchboardFiles = files
			state.sketchboard = [...state.sketchboardFiles,...state.sketchGenerateFiles,...state.sketchMaterialFiles]	
        },
		setSketchboardGenerateFiles(state,files){
			state.sketchGenerateFiles = files
			state.sketchboard = [...state.sketchboardFiles,...state.sketchGenerateFiles,...state.sketchMaterialFiles]	
        },
		setSketchboardMaterialFiles(state,files){
			state.sketchMaterialFiles = files
			state.sketchboard = [...state.sketchboardFiles,...state.sketchGenerateFiles,...state.sketchMaterialFiles]	

        },
		setShowSketchboard(state,files){
			state.showSketchboard = files
		},
		clearShowSketchboard(state,files){
			state.showSketchboard = []
		},
		setPrintboardFile(state,files){
            state.printboardFiles = files
			state.printboard = [...state.printboardFiles,...state.printGenerateFiles,...state.printMaterialFiles]	
        },
		setPrintboardGenerateFiles(state,files){
			state.printGenerateFiles = files
			state.printboard = [...state.printboardFiles,...state.printGenerateFiles,...state.printMaterialFiles]	
        },
		setPrintboardMaterialFiles(state,files){
			state.printMaterialFiles = files
			state.printboard = [...state.printboardFiles,...state.printGenerateFiles,...state.printMaterialFiles]	
        },

        setColorboardList(state,colorBoards){
            state.colorBoards = colorBoards || []
        },
        setMarketingSketchFile(state,files){
            state.marketingSketchFiles = files
        },
        setAllBoardData(state,data){
			let str:any = data.str
			let navListData = (workspace as any).state.projectList
			let currentState = false
			if(str == 'moodBoard'){
				state.allBoardData.disposeMoodboard = state.disposeMoodboard
				state.allBoardData.moodboardPosition = state.moodboardPosition
				state.allBoardData.moodboardFiles = state.moodboard
				state.allBoardData.moodTemplateId = state.moodTemplateId
				if(state.moodboard.length > 0)currentState = true
			}else if(str == 'printBoard'){
				state.allBoardData.printboardFiles = state.printboard
				if(state.printboard.length > 0)currentState = true
			}else if(str == 'sketchBoard'){
				state.allBoardData.sketchboardFiles = state.sketchboard
				if(state.sketchboard.length > 0)currentState = true
			}else if(str == 'colorBoard'){
				state.allBoardData.colorBoards = state.colorBoards
				if(state.colorBoards.length > 0)currentState = true
			}
			// let navListDataa:any = navListData[0]
			// navListDataa.select = true;
			navListData[0].select = true;
			(workspace as any).state.projectList = navListData;
			if(currentState){
				let index = navListData.findIndex((item:any) => item.value === str);
				navListData[index].select = true
			}
        },
		clearAllBoardData(state,data){
			if(data == 'moodBoard'){
				state.allBoardData.disposeMoodboard = []
				state.allBoardData.moodboardPosition = null
				state.allBoardData.moodboardFiles = []
				state.allBoardData.moodTemplateId = ''
			}else if(data == 'printBoard'){
				state.allBoardData.printboardFiles = []
			}else if(data == 'sketchBoard'){
				state.allBoardData.sketchboardFiles = []
			}else if(data == 'colorBoard'){
				state.allBoardData.colorBoards = []
			}
			let navListData = (workspace as any).state.projectList
			let index = navListData.findIndex((item:any) => item.value === data);
			navListData[index].select = false
        },
		
		createModularData(state){
			state.modularData = {
				design:null,
				toProduct:null,
				relight:null,
				poseTransfer:null,
				canvas:null,
				patternMaking3D:null,
				deReconstruction:null,
			}
		},
        setAllBoardDataChoose(state,data){
            state.allBoardData = data
			if(Object.keys(data).length === 0){
				state.chooseIsDesign.value = true
			}
        },
		setChooseIsDesign(state,data){
            state.chooseIsDesign.value = data
        },
        clearAllData(state){
            state.moodboard = []
            state.moodboardFiles = []
            state.moodboardGenerateFiles = []
            state.moodboardMaterialFiles = []
            state.sketchboard = []
			state.sketchboardFiles = []
            state.sketchGenerateFiles = []
            state.sketchMaterialFiles = []
			state.printboard = []
			state.printboardFiles = []
            state.printGenerateFiles = []
            state.printMaterialFiles = []
			state.colorBoards = []
            state.marketingSketchFiles = []
            state.moodTemplateId = ''
			state.disposeMoodboard = []
			// state.showSketchboard = []
			state.chooseIsDesign.value = true
			state.moodboardPosition = {}
        },
		clearAllId(state){
            state.moodboard.forEach((v:any) => {
				delete v.id_
			});
			state.sketchboard.forEach((v:any) => {
				delete v.id_
			});
			state.printboard.forEach((v:any) => {
				delete v.id_
			});
		},
        setMoodTemplateId(state,moodTemplateId){
            state.moodTemplateId = moodTemplateId
        },
        clearMoodTemplateId(state){
			state.disposeMoodboard = []
			state.moodboardPosition = {}
        },
    },
    actions:{
		setAllBoardData({ state,commit },data) {
			//   commit('increment'); // 提交 mutation
			let navListData = JSON.parse(JSON.stringify((workspace as any).state.projectList))
			let str:any = data.type
			let currentState = false
			if(str == 'moodBoard'){
				state.allBoardData.disposeMoodboard = state.disposeMoodboard
				state.allBoardData.moodboardPosition = state.moodboardPosition
				state.allBoardData.moodboardFiles = state.moodboard
				state.allBoardData.moodTemplateId = state.moodTemplateId
				if(state.moodboard.length > 0)currentState = true
			}else if(str == 'printBoard'){
				state.allBoardData.printboardFiles = state.printboard
				if(state.printboard.length > 0)currentState = true
			}else if(str == 'sketchBoard'){
				state.allBoardData.sketchboardFiles = state.sketchboard
				console.log(state.allBoardData.sketchboardFiles)
				if(state.sketchboard.length > 0)currentState = true
			}else if(str == 'colorBoard'){
				state.allBoardData.colorBoards = state.colorBoards
				if(state.colorBoards.length > 0)currentState = true
			}
			let index = navListData.findIndex((item:any) => item.value === str);
			if(currentState){
				navListData[index].select = true
			}else{
				navListData[index].select = false
			}
			commit('setProjectList',navListData)
		},
		setModularData({ state,commit },data){
			let navListData = JSON.parse(JSON.stringify((workspace as any).state.projectList))
			let value = null
			let homeData:any = homeStore.state
			if(data.type == 'design'){
				let userGroupId = homeData.userGroupId
				let likeData = homeData.likeDesignCollectionList
				value = {userlikeGroupId:userGroupId?userGroupId:null,likeData:likeData?likeData:[]}
			}else if(data.type == 'toProduct'){
				value = homeData.toProductImageList
			}else if(data.type == 'relight'){
				value = homeData.relightList
			}else if(data.type == 'poseTransfer'){
				value = homeData.poseTransfer
			}else if(data.type == 'deReconstruction'){
				value = homeData.deReconstruction
			}else if(data.type == 'patternMaking3D'){
				value = homeData.patternMaking3D
			}
			let index = navListData.findIndex((item:any) => item.value === data.type);
			if(
				(value == null || value?.length == 0) || 
				(data.type == 'design' && !value.userlikeGroupId)
			){
				navListData[index].select = false
			}else{
				navListData[index].select = true
			}
			state.modularData[data.type] = value
			commit('setProjectList',navListData)
		},
		getProjectData({ state,commit },str){
			return new Promise((resolve,reject) => {
				let data:any = {}
				let allBoardData = ['sketchBoard','moodBoard','printBoard','colorBoard']
				if(allBoardData.indexOf(str) != -1){
					if(str == 'colorBoard'){
						let boardData = state.colorBoards || []
						let colorBoards = boardData.map((v: any) => {
							let data = {
								id: v.id,
								name: v.name,
								tcx: v.tcx,
								gradient:v.gradient,
								rgbValue: "",
							};
							data.rgbValue = `${v.rgbValue.r} ${v.rgbValue.g} ${v.rgbValue.b}`;
							return data;
						});
						data[str] = colorBoards
					}else if(str == 'moodBoard'){
						state.moodboard = state.moodboard || []
						let dataList = state.moodboard.map((v: any) => {
							let data: any = {
								id: v.resData.id,
								designType: v.resData.designType,
							};
							return data;
						});
						data[str] = {
							moodBoards:dataList,
							moodTemplateId:state.allBoardData.disposeMoodboard?.[0]?.id?state.allBoardData.disposeMoodboard?.[0]?.id:null,
							moodboardPosition:JSON.stringify(state.moodboardPosition),
						}
					}else if (str == 'printBoard') {
						let dataList = state.printboard.map((v: any) => {
							let data: any = {
								id: v.resData.id,
								designType: v.resData.designType,
								level2Type: v.categoryValue,
								isPin: v.pin ? 1 : 0,
							};
							return data;
						});
						data[str] = dataList
					}else if(str == 'sketchBoard'){
						let sketchBoards = state.sketchboard.map((v: any) => {
							let data = {
								designType: v.resData.designType,
								isPin: v.pin ? 1 : 0,
								level2Type: v.categoryValue,
								sketchBoardId: v.resData.id,
							};
							return data;
						});
						data[str] = sketchBoards
					}
				}else{
					data[str] = state.modularData[str]
				}
				resolve(data)
			})
		},
		getProjectCanvasData({ state,commit },str){
			return new Promise((resolve,reject) => {
				let data:any = {}
				let allBoardData = ['sketchBoard','moodBoard','printBoard','colorBoard']
				if(allBoardData.indexOf(str) != -1){
					if(str == 'colorBoard'){
					}else if(str == 'moodBoard'){
						data[str] = state.moodboard
					}else if (str == 'printBoard') {
						let dataList = state.printboard;
						data[str] = dataList
					}else if(str == 'sketchBoard'){
						let sketchBoards = state.sketchboard
						data[str] = sketchBoards
					}
				}else{
					if(str == 'design'){
						data[str] = state.modularData[str].likeData
					}else{
						data[str] = state.modularData[str]
					}
				}
				resolve(data)
			})
		},
    }
}

export default UploadFilesModule