import {Module} from 'vuex'
import {RootState} from '../index'
import { Https } from "@/tool/https";
import { setLang } from "@/tool/guide";
import { setCookie } from "@/tool/cookie";
import store from '../index'
interface UserHabit{
    clothingType:any,
	credits:any,
    // devise:any,
    // deviseType:any,
    // system_per:any,
    // model:any,
	printType:any,
	designElementsType:any,
	FemalePosition:any,
	MalePosition:any,
	Position:any,
	SketchGenerateType:any,
	SketchStyle:any,
	systemUser:any,
	mannequinStyle:any,
	userInfo:any,
    messageSystem:any,
    sex:any,
	userDetail:any,
	bindEmail:any,
}

const userHabit : Module<UserHabit,RootState> = {
    state:{
        clothingType:[],
		credits:{
			value:0
		},
		printType:[{
			name:'Pattern',
			value:'Pattern'
		},{
			name:'Slogan',
			value:'Slogan'
		},{
			name:'Logo',
			value:'Logo'
		},],
		designElementsType:[],
		FemalePosition:[],
		MalePosition:[],
		SketchGenerateType:[],
		Position:[],
		SketchStyle:[],
		mannequinStyle:null,
		systemUser:{
			value : -1
		},
		userInfo:{

		},
		messageSystem:{
			messageNum:0,
			messageType:{},
		},
		sex:{
		},
		userDetail:{
			email:'-------------',
			userName: "---",
			userId: -1,
			isBeginner: false,
			systemUser: -1,
			avatar: "https://www.minio.aida.com.hk:12024/aida-users/87/avatar/default.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=admin%2F20241209%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20241209T055108Z&X-Amz-Expires=86400&X-Amz-SignedHeaders=host&X-Amz-Signature=04bb98c0a264b91f3c30173f7ab9e91de7bf31bd48440f4595fdd91f05629b80",
			followeeCount: '-',
			followerCount: '-',
			accountExtendList:null,
			systemList:[],
			expireTime:null,
			language:'',
			timeData:{
				isExpiration:false,
				text:''
			},
			subscriptionType:null,
			subscriptionId:null,
			usernameModify:0,
			occupation:'',//职业
			country:'',//国家
			title:'',//称呼
			surname:'',//姓氏
			givenName:'',//名字

		},
		bindEmail : {
			isBindEmail:false,
			type:''
		}
    },
    mutations:{
        res_clothingType(state,data){
            state.clothingType = data
        },
		setCredits(state,data){
			state.credits.value = data
		},
		setPrintType(state,data){
			state.printType = data
		},
		setDesignElementsType(state,data){
			state.designElementsType = data
		},
		setFemalePosition(state,data){
			state.FemalePosition = data
		},
		setSketchGenerateType(state,data){
			state.SketchGenerateType = data
		},
		setSketchStyle(state,data){
			state.SketchStyle = data
		},
		setMalePosition(state,data){
			state.MalePosition = data
		},
		setPosition(state,data){
			if(data == 'Female'){
				state.Position = state.FemalePosition
			}else{
				state.Position = state.MalePosition
			}
		},
		upUserDetail(state,data){
			if(!data)return
			if(data.affiliate){
				if(state.userDetail.systemList.indexOf(data.affiliate) == -1)state.userDetail.systemList.push(2)
			}
			for (const key in data) {
				state.userDetail[key] = data[key]
			}
		},
		setMessageSystem(state,data){
			let num = 0
			// let userInfo = ['-1']
			let userInfo = ['followeeCount','followerCount']
			data.forEach((item:any) => {
				for (const iterator in item) {
					if(userInfo.indexOf(iterator) > -1){
					}else{
						state.messageSystem.messageType[iterator] = item[iterator]
					}
				}
			});
			let allObj = state.messageSystem.messageType
			for (const iterator in allObj) {
				if(userInfo.indexOf(iterator) > -1){
				}else{
					if(iterator != 'newPosted')num+=allObj[iterator]
				}
			}
			state.messageSystem.messageNum = num
		},
		setMannequinStyle(state,data){
			state.mannequinStyle = data
		},
		closeMessageSystem(state,data){
			if(!data){
				state.messageSystem.messageNum = 0
				state.messageSystem.messageType = null
			}else{

			}
		},
		setSex(state,data){
			state.sex.value = data
		},
		createDetail(state,data){
		    state.userDetail = {
				email:'-------------',
				userName: "---",
				userId: -1,
				isBeginner: false,
				systemUser: -1,
				avatar: "https://www.minio.aida.com.hk:12024/aida-users/87/avatar/default.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=admin%2F20241209%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20241209T055108Z&X-Amz-Expires=86400&X-Amz-SignedHeaders=host&X-Amz-Signature=04bb98c0a264b91f3c30173f7ab9e91de7bf31bd48440f4595fdd91f05629b80",
				followeeCount: '-',
				followerCount: '-',
				accountExtendList:null,
				systemList:[],
				expireTime:null,
				language:'',
				timeData:{
					isExpiration:false,
					text:''
				},
				subscriptionType:null,
				subscriptionId:null,
				//是否是affiliate用户
				affiliate:false,
				usernameModify:0,
				occupation:'',//职业
				country:'',//国家
				title:'',//称呼
				surname:'',//姓氏
				givenName:'',//名字
				
			}
			state.bindEmail = {
				isBindEmail:false,
				type:''
			}
		},
		setIsBindEmail(state,data){
			state.bindEmail.isBindEmail = data.isBindEmail
			state.bindEmail.type = data.type

		},
		setUserDetail(state,data){
			
			// if(!data.email && !state.userDetail.email)state.isBindEmail = true
			// state.userDetail = data
			state.userDetail.email = data.email?data.email:'-------------'
			state.userDetail.userName = data.userName//用户名
			state.userDetail.userId = data.userId//用户id
			state.userDetail.avatar = data.avatar//头像
			state.userDetail.country = data.country//头像
			state.userDetail.occupation = data.occupation//头像
			state.userDetail.usernameModify = data.usernameModify//当月剩余修改次数
			state.userDetail.isBeginner = data.isBeginner == 1 ? true : false;//是否完成新手指引
			state.userDetail.title = data.title//当月剩余修改次数
			state.userDetail.surname = data.surname//当月剩余修改次数
			state.userDetail.givenName = data.givenName//当月剩余修改次数
			// state.bindEmail.isBindEmail = true

			if(
				(!data.email && state.userDetail.email == '-------------') || 
				(!data.country && !state.userDetail.country) || 
				(!data.occupation && !state.userDetail.occupation) ||
				(!data.title && !state.userDetail.title) ||
				(!data.surname && !state.userDetail.surname) ||
				(!data.givenName && !state.userDetail.givenName) 
			){
				state.bindEmail.isBindEmail = true
				state.bindEmail.type = ''
				setCookie("isBeginner", false);
			}else{
				setCookie("isBeginner", state.userDetail.isBeginner);
			}
			/**
			 * 用于区分游客与系统用户
			 * 0 ： 游客
			 * 1 ： 年付用户
			 * 2 ： 月付用户
			 * 3 ： 试用用户
			 * 4 ： 参加活动获取30天有效期和6000个积分的用户
			 * 5 ： 企业管理员账号
			 * 6 ： 企业子账号
			 * 7 ： 学校管理员
			 * 8 ： 学校子账号
			 */
			state.userDetail.affiliate = data.affiliate
			state.userDetail.systemUser = data.systemUser
			// 身份列表1：可以使用aida系统，2:可以使用affiliate页面，3：系统管理员用户
			state.userDetail.systemList = []
			if(data.systemUser != 0)state.userDetail.systemList.push(1)
			if(data.affiliate)state.userDetail.systemList.push(2)
			let adminIdList = [88,6,46,31,73,83,87,4,11482,11630,12201,12592]
			// if(data.email == '' || data.email)state.userDetail.systemList.push(3)
			if(adminIdList.indexOf(data.userId) > -1)state.userDetail.systemList.push(3)

			state.userDetail.followeeCount = data.followeeCount//粉丝数量
			state.userDetail.followerCount = data.followerCount//关注数量
			state.userDetail.accountExtendList = {}
			if(data.accountExtendList){
				if(!Array.isArray(data.accountExtendList)){
					state.userDetail.accountExtendList = data.accountExtendList
				}else{
					let WeChat = data.accountExtendList.filter((item:any) => item.authType == 'WeChat')[0]
					let Google = data.accountExtendList.filter((item:any) => item.authType == 'Google')[0]
					let obj = {
						WeChat,Google
					}
					state.userDetail.accountExtendList = obj//绑定相关内容
				}
			}
			let current = Math.floor(Date.now() );
			let timeData
			if(data.validEndTime){
				timeData = {
					isExpiration:current<data.validEndTime,
					text:new Date(parseInt(String(data.validEndTime))).toLocaleDateString()
				}
			}else{
				timeData = {
					isExpiration:true,
					text:'- - - - / - - / - -'
				}
			}
			state.userDetail.timeData = timeData//有效期
			state.userDetail.language = data.language//当前语言
			setLang(data.language)
			state.userDetail.expireTime = data.expireTime//有效期
			state.userDetail.subscriptionType = data.subscriptionType//当前订阅类型
			state.userDetail.status = data.status//当前订阅类型
			// state.userDetail.status = data.status || 'active'//当前订阅类型
			state.userDetail.subscriptionId = data.subscriptionId//最新订阅id
		}
    },

    actions:{
		get_clothingType(context){
			var arr = [{
				id:11,
				name:"nv"
			},{
				id:22,
				name:"nan",
			}]
			context.commit('res_clothingType',arr)

			// Https.axiosGet(url)
			// 	.then((rv: any) => {
			// 		context.commit('res_clothingType',rv)
			// 	})
			// 	.catch((rv) => {
			// 	});
		},
		getCredits(context){
			return new Promise( async (resolve) => {
				await Https.axiosGet(Https.httpUrls.getCredits,).then((rv) => {
					context.commit('setCredits',rv)
					resolve(true)
				}).catch((res) => {
					resolve(true)
				});
			})
		},
		async getLangType(context){
			let allPromise = [];
			let getList = ['DesignElementsEnum','PrintboardLevel2TypeEnum','FemalePosition','MalePosition','SketchStyle','SketchGenerateType','Sex']
			let workspaceAllPosition:any = []
			let axiosGet = (item:any)=>{
				return new Promise((resolve,reject) => {
			        Https.axiosGet(Https.httpUrls.workspaceenumValues,{params:{enumName:item}}).then((rv: any) => {
						if (rv) {
							rv.forEach((item:any) => {
								let name = item.name
								item.name = item.value
								item.value = name
								item.optype = false
							});
							if(item == 'DesignElementsEnum'){
								context.commit('setDesignElementsType',rv)
							}else if(item == 'PrintboardLevel2TypeEnum'){
								context.commit('setPrintType',rv)
							}else if(item == 'FemalePosition'){
								context.commit('setFemalePosition',rv)
								workspaceAllPosition.push(...rv)
							}else if(item == 'MalePosition'){
							    context.commit('setMalePosition',rv)
								context.commit('setPosition','MalePosition')
								workspaceAllPosition.push(...rv)
							}else if(item == 'SketchStyle'){
								context.commit('setSketchStyle',rv)
							}else if(item == 'SketchGenerateType'){
								context.commit('setSketchGenerateType',rv)
							}else if(item == 'Sex'){
								context.commit('setSex',rv)
							}
							resolve('')
						}
					}).catch(()=>{
						reject('')
					})
			    })
			}
			context.commit('setWorkspaceAllPosition',workspaceAllPosition)
			let getUserDetail = ()=>{
				return new Promise((resolve,reject) => {
					context.commit('createDetail')
			        Https.axiosPost(Https.httpUrls.accountDetail).then((rv: any) => {
						if(rv){
							context.commit('setUserDetail',rv)
						}
						resolve('')
					}).catch(()=>{
						reject('')
					})
				})
			}
			return await Promise.all([...getList.map(item => axiosGet(item)),getUserDetail()])
		}
    }
}

export default userHabit