// 在线状态
export const getOnlineStateFromSvr = (userID) =>
  uni.$u?.http.post(
    "/user/get_users_status",
    JSON.stringify({
      userID,
      userIDs: [userID],
    }),
    {
      custom: {
        isIMApi: true,
      },
      header: {
        token: uni.getStorageSync("IMToken"),
      },
    }
);

export const getRtcInvitaion = () =>
  uni.$u?.http.post(
    "/third/get_rtc_invitation_start_app",
    JSON.stringify({
      operationID: uuidv4(),
    }),
    {
      custom: {
        isIMApi: true,
      },
      header: {
        token: uni.getStorageSync("IMToken"),
      },
    },
  );

export const subUserOnlineStatus = (userID, targetID) =>
  uni.$u?.http.post(
    "/user/subscribe_users_status",
    JSON.stringify({
      userID,
      userIDs: [targetID],
      genre: 1,
    }),
    {
      custom: {
        isIMApi: true,
      },
      header: {
        token: uni.getStorageSync("IMToken"),
      },
    }
);



export const unSubUserOnlineStatus = (userID, targetID) =>
  uni.$u?.http.post(
    "/user/unsubscribe_users_status",
    JSON.stringify({
      userID,
      userIDs: [targetID],
      genre: 2,
    }),
    {
      custom: {
        isIMApi: true,
      },
      header: {
        token: uni.getStorageSync("IMToken"),
      },
    },
  );
  


//获取超级管理员token
 export const getImAdminToken = () =>
   uni.$u?.http.post(
     "/auth/get_admin_token", 
     JSON.stringify({ secret:"openIM123",userID: "imAdmin"}),
     {
       custom: {
         isIMApi: true,
       },
       header: {
         //operationID: Date.now()
       }
     }
 );
 
 //注册IM用户
 export const registerImUser = (data) =>
   uni.$u?.http.post(
     "/user/user_register", 
     data,
     {
       custom: {
         isIMApi: true,
       },
       header: {
         //operationID: Date.now(),
		 token:uni.getStorageSync("IMAdminToken"),
       },
     }
 );
 
 
 //修改IM用户
 export const updateImUser = (data) =>
   uni.$u?.http.post(
     "/user/update_user_info_ex", 
     data,
     {
       custom: {
         isIMApi: true,
       },
       header: {
         //operationID: Date.now(),
 		 token:uni.getStorageSync("IMAdminToken"),
       },
     }
 );
 
 
 
 
 //获取用户token
 export const getImUserToken = (userID) =>
   uni.$u?.http.post(
     "/auth/get_user_token", 
     JSON.stringify(
	    {"platformID": getRuntimePlatform(),"userID": userID}
 	 ),
     {
       custom: {
         isIMApi: true,
       },
       header: {
         //operationID: Date.now(),
 		 token:uni.getStorageSync("IMAdminToken")
       },
     }
 );
 
 
 //查询用户是否注册
 export const imAccountCheck = (userID) =>
   uni.$u?.http.post(
     "/user/account_check", 
     JSON.stringify({
		  checkUserIDs:[ userID ]
	 }),
     {
       custom: {
         isIMApi: true,
       },
       header: {
         //operationID: Date.now(),
 		 token:uni.getStorageSync("IMAdminToken")
       },
     }
 );
 
 
 
 function getRuntimePlatform() {
   const systemInfo = uni.getSystemInfoSync();
   const compilePlatform = process.env.UNI_PLATFORM;
   //1：iOS，2：Android，3：Windows，4：OSX，5：WEB，6：小程序，7：linux，8：AndroidPad，9：IPad，10：Admin
   let platformType=6;
   // H5 环境
   if (compilePlatform === 'h5') platformType= 5;  
 
   // 小程序环境
   if (compilePlatform.startsWith('mp-')) {
      const mpType = compilePlatform.split('-')[1]; // 如 weixin/alipay
      platformType=6;
	 // return `小程序（${mpType}）- ${systemInfo.platform}`;
   }
 
   // App 环境
   if (compilePlatform === 'app-plus') {
	 if(systemInfo.platform.toLowerCase() === 'android'){
		 platformType=2;
	 }
	 if(systemInfo.platform.toLowerCase() === 'ios'){
	 		 platformType=1;
	 }
   }
 
   return platformType;
 }