import { getImAdminToken,imAccountCheck,registerImUser,getImUserToken,updateImUser } from "@/pages_im/api/imApi";

export const imAccountCheckReg = (userID) => {
  return new Promise(async (resolve, reject) => {
	   let data={};
	   let hasReg=false;
		try {
			  let token = uni.getStorageSync('IMAdminToken');
			  let tokenData={};
			  if(!token){
				   const respTokenObj = await getImAdminToken();
					if(respTokenObj.errCode==0){
						tokenData = respTokenObj.data;
						token=tokenData.token;
						uni.setStorageSync("IMAdminToken",token);
					}
			  }
			  data = await imAccountCheck(userID);
			  if(data.results.lenth>0){
			  	  uni.setStorage({key: "IMUserID",data: userID});
			  	  hasReg=true;
			  }
			  data={hasReg:hasReg};  
			  resolve(data);
		} catch (e) {
			reject(e);
		}
       
  });
};



export const getImUserTokenReq = (userID) => {
  return new Promise(async (resolve, reject) => {
	   let data={};
		try {
			  let token = uni.getStorageSync('IMAdminToken');
			  let tokenData={};
			  if(!token){
				    const data = await getImAdminToken();
					console.log("qxj getImAdminToken data:"+JSON.stringify(data));
					token=data.token;
					uni.setStorageSync("IMAdminToken",token);
			  }
			  data = await getImUserToken(userID);
			  uni.setStorage({key: "IMToken", data: data.token});
			  uni.setStorage({key: "IMUserID",data: userID});
			  resolve(data);
		} catch (err) {
			  reject(err);
		}
      
  });
};


export const registerImUserReq = (userID,nickname,faceURL) => {
   return new Promise(async (resolve, reject) => {
	   let data={};
		try {
			  let token = uni.getStorageSync('IMAdminToken');
			  let tokenData={};
			  const respTokenObj = await getImAdminToken();
			  if(!token){
					if(!!respTokenObj && respTokenObj.token){
						token=respTokenObj.token;
						uni.setStorageSync("IMAdminToken",token);
					}
			  }
			  let params={
			  	  users:[ { userID:userID,nickname:nickname,faceURL:faceURL } ]
			  };
			  let respObj = await registerImUser(params);
			  console.log("----qxj registerImUserReq respObj:"+JSON.stringify(respObj));
			  //if(respObj.errCode==0){
				   data = await getImUserToken(userID);
				   uni.setStorage({key: "IMToken", data: data.token});
				   uni.setStorage({key: "IMUserID",data: userID});
			  //}
			  resolve(data);
		} catch (e) {
			reject(e);
		}
        
  });
};


export const updateImUserReq = (userID,nickname,faceURL) => {
   return new Promise(async (resolve, reject) => {
	   let data={};
		try {
			  let token = uni.getStorageSync('IMAdminToken');
			  let tokenData={};
			  const respTokenObj = await getImAdminToken();
			  if(!token){
					if(!!respTokenObj && respTokenObj.token){
						token=respTokenObj.token;
						uni.setStorageSync("IMAdminToken",token);
					}
			  }
			  let params={
			  	  userInfo:{ "userID":userID,"nickname":nickname,"faceURL":faceURL }
			  };
			  let respObj = await updateImUser(params);
			  //if(respObj.errCode==0){
				   data = await getImUserToken(userID);
				   uni.setStorage({key: "IMToken", data: data.token});
				   uni.setStorage({key: "IMUserID",data: userID});
			  //}
			  resolve(data);
		} catch (e) {
			reject(e);
		}
        
  });
};


export const openImLoginApi =async (data,IMToken,IMUserID,succCallBack,failCallBack) => {
	console.log("----qxj IMUserID:"+IMUserID+" IMToken:"+IMToken);
    let userID=data.userID;
	let nickName=data.nickName;
	let faceURL=data.avatar;
	if(!!IMUserID || !!IMToken){
		  getImUserTokenReq(IMUserID).then(res => {
			   updateImUserReq(userID,nickName,faceURL).then(res1 => {
			   		succCallBack(res.token);
			   	},
			   	rej => {
			   		failCallBack();
			   	}
			   ).catch((res) => {
			   	 failCallBack();
			   });
				//succCallBack();
			},
			rej => {
				if(rej.errCode==1004 && rej.errMsg=="RecordNotFoundError"){  //未找到
					registerImUserReq(userID,nickName,faceURL).then(res => {
							succCallBack();
						},
						rej => {
							failCallBack();
						}
					).catch((res) => {
						 failCallBack();
					});
				}
				failCallBack();
			}
		).catch((err) => {
		     failCallBack();
		});
	}
	else{
		
		registerImUserReq(userID,nickName,faceURL).then(res => {
				succCallBack();
			},
			rej => {
				failCallBack();
			}
		).catch(() => {
			
		});
	}

};