<script>
import { mapGetters, mapActions } from "vuex";
import IMSDK, {
  IMMethods,
  MessageType,
  SessionType,
} from "openim-uniapp-polyfill";
import config from "./common/config";
import { getDbDir, toastWithCallback } from "@/util/common.js";
import { conversationSort } from "@/util/imCommon";
import { PageEvents, UpdateMessageTypes } from "@/constant";
const openSdk = uni.requireNativePlugin('Tuoyun-OpenIMSDK');
const globalEvent = uni.requireNativePlugin('globalEvent');

export default {
  onLaunch: function () {
    console.log("App Launch");
	console.log(openSdk);
    this.setGlobalIMlistener();
    this.tryLogin();
    // #ifdef APP-PLUS
    checkUpdate(); // 直接调用独立函数，避免this问题
    // #endif
    // #ifdef H5
    console.error(
      `暂时不支持运行到 Web，如果需要移动端的 Web 项目，参考 [H5 demo](https://github.com/openimsdk/openim-h5-demo)`
    );
    // #endif
    // #ifdef MP-WEIXIN
    console.error(`暂时不支持运行到小程序端`);
    // #endif
    // 集成cid全局获取和存储
    // uni.getPushClientId({
    //   success: (res) => {
    //     console.log(res, 'app-Push-ID');
    //     if (res && res.cid) {
    //       uni.setStorageSync('cid', res.cid);
    //     }
    //   },
    //   fail: (err) => {
    //     console.log(err)
    //   }
    // })
  },
  onShow: function () {
    console.log("App Show");
    IMSDK.asyncApi(IMSDK.IMMethods.SetAppBackgroundStatus, IMSDK.uuid(), false);
	//#ifdef APP-PLUS
	// var info = plus.push.getClientInfo()
	// plus.push.addEventListener("click", function(msg) {
	// 				console.log("click:" + JSON.stringify(msg));
	// 				console.log(msg.payload);
	// 				console.log(JSON.stringify(msg));
	// 			}, false);
	// 			// 监听在线消息事件    
	// 			plus.push.addEventListener("receive", function(msg) {
	// 				//业务代码
	// 				console.log("recevice:" + JSON.stringify(msg))
	// 			}, false);
	//#endif
	

  },
  onHide: function () {
    console.log("App Hide");
    IMSDK.asyncApi(IMSDK.IMMethods.SetAppBackgroundStatus, IMSDK.uuid(), true);
  },
  computed: {
    ...mapGetters([
      "storeConversationList",
      "storeCurrentConversation",
      "storeCurrentUserID",
      "storeSelfInfo",
      "storeRecvFriendApplications",
      "storeRecvGroupApplications",
      "storeHistoryMessageList",
      "storeIsSyncing",
      "storeGroupList",
    ]),
  },
  methods: {
    ...mapActions("message", ["pushNewMessage", "updateOneMessage"]),
    ...mapActions("conversation", ["updateCurrentMemberInGroup"]),
    ...mapActions("contact", [
      "updateFriendInfo",
      "pushNewFriend",
      "updateBlackInfo",
      "pushNewBlack",
      "pushNewGroup",
      "updateGroupInfo",
      "pushNewRecvFriendApplition",
      "updateRecvFriendApplition",
      "pushNewSentFriendApplition",
      "updateSentFriendApplition",
      "pushNewRecvGroupApplition",
      "updateRecvGroupApplition",
      "pushNewSentGroupApplition",
      "updateSentGroupApplition",
    ]),
    setGlobalIMlistener() {
      console.log("setGlobalIMlistener");
      // init
      const kickHander = (message) => {
        toastWithCallback(message, () => {
          uni.removeStorage({
            key: "IMToken",
          });
          uni.removeStorage({
            key: "BusinessToken",
          });
          uni.$u.route("/pages/login/index");
        });
      };
      IMSDK.subscribe(IMSDK.IMEvents.OnKickedOffline, (data) => {
        kickHander("您的账号在其他设备登录，请重新登陆！");
      });
      IMSDK.subscribe(IMSDK.IMEvents.OnUserTokenExpired, (data) => {
        kickHander("您的登录已过期，请重新登陆！");
      });
      IMSDK.subscribe(IMSDK.IMEvents.OnUserTokenInvalid, (data) => {
        kickHander("您的登录已无效，请重新登陆！");
      });

      // sync
      const syncStartHandler = ({ data }) => {
        this.$store.commit("user/SET_IS_SYNCING", true);
        this.$store.commit("user/SET_REINSTALL", data);
      };
      const syncProgressHandler = ({ data }) => {
        this.$store.commit("user/SET_PROGRESS", data);
      };
      const syncFinishHandler = () => {
        uni.hideLoading();
        this.$store.dispatch("conversation/getConversationList");
        this.$store.dispatch("contact/getFriendList");
        this.$store.dispatch("contact/getGrouplist");
        this.$store.dispatch("conversation/getUnReadCount");
        this.$store.commit("user/SET_IS_SYNCING", false);
      };
      const syncFailedHandler = () => {
        uni.hideLoading();
        uni.$u.toast("同步消息失败");
        this.$store.dispatch("conversation/getConversationList");
        this.$store.dispatch("conversation/getUnReadCount");
        this.$store.commit("user/SET_IS_SYNCING", false);
      };
      IMSDK.subscribe(IMSDK.IMEvents.OnSyncServerStart, syncStartHandler);
      IMSDK.subscribe(IMSDK.IMEvents.OnSyncServerFinish, syncFinishHandler);
      IMSDK.subscribe(IMSDK.IMEvents.OnSyncServerFailed, syncFailedHandler);
      IMSDK.subscribe(IMSDK.IMEvents.OnSyncServerProgress, syncProgressHandler);

      // self
      const selfInfoUpdateHandler = ({ data }) => {
        this.$store.commit("user/SET_SELF_INFO", {
          ...this.storeSelfInfo,
          ...data,
        });
      };

      IMSDK.subscribe(IMSDK.IMEvents.OnSelfInfoUpdated, selfInfoUpdateHandler);

      // message
      const newMessagesHandler = ({ data }) => {
        if (this.storeIsSyncing) {
          return;
        }
        data.forEach(this.handleNewMessage);
      };

      IMSDK.subscribe(IMSDK.IMEvents.OnRecvNewMessages, newMessagesHandler);

      // friend
      const friendInfoChangeHandler = ({ data }) => {
        uni.$emit(IMSDK.IMEvents.OnFriendInfoChanged, { data });
        this.updateFriendInfo({
          friendInfo: data,
        });
      };
      const friendAddedHandler = ({ data }) => {
        this.pushNewFriend(data);
      };
      const friendDeletedHander = ({ data }) => {
        this.updateFriendInfo({
          friendInfo: data,
          isRemove: true,
        });
      };

      IMSDK.subscribe(
        IMSDK.IMEvents.OnFriendInfoChanged,
        friendInfoChangeHandler
      );
      IMSDK.subscribe(IMSDK.IMEvents.OnFriendAdded, friendAddedHandler);
      IMSDK.subscribe(IMSDK.IMEvents.OnFriendDeleted, friendDeletedHander);

      // blacklist
      const blackAddedHandler = ({ data }) => {
        this.pushNewBlack(data);
      };
      const blackDeletedHandler = ({ data }) => {
        this.updateBlackInfo({
          blackInfo: data,
          isRemove: true,
        });
      };

      IMSDK.subscribe(IMSDK.IMEvents.OnBlackAdded, blackAddedHandler);
      IMSDK.subscribe(IMSDK.IMEvents.OnBlackDeleted, blackDeletedHandler);

      // group
      const joinedGroupAddedHandler = ({ data }) => {
        this.pushNewGroup(data);
      };
      const joinedGroupDeletedHandler = ({ data }) => {
        this.updateGroupInfo({
          groupInfo: data,
          isRemove: true,
        });
      };
      const groupInfoChangedHandler = ({ data }) => {
        this.updateGroupInfo({
          groupInfo: data,
        });
      };
      const groupMemberInfoChangedHandler = ({ data }) => {
        uni.$emit(IMSDK.IMEvents.OnGroupMemberInfoChanged, { data });
        if (data.groupID === this.storeCurrentConversation?.groupID) {
          this.updateCurrentMemberInGroup(data);
        }
      };

      IMSDK.subscribe(
        IMSDK.IMEvents.OnJoinedGroupAdded,
        joinedGroupAddedHandler
      );
      IMSDK.subscribe(
        IMSDK.IMEvents.OnJoinedGroupDeleted,
        joinedGroupDeletedHandler
      );
      IMSDK.subscribe(
        IMSDK.IMEvents.OnGroupInfoChanged,
        groupInfoChangedHandler
      );
      IMSDK.subscribe(
        IMSDK.IMEvents.OnGroupMemberInfoChanged,
        groupMemberInfoChangedHandler
      );

      // application
      const friendApplicationNumHandler = ({ data }) => {
        const isRecv = data.toUserID === this.storeCurrentUserID;
        if (isRecv) {
          this.pushNewRecvFriendApplition(data);
        } else {
          this.pushNewSentFriendApplition(data);
        }
      };
      const friendApplicationAccessHandler = ({ data }) => {
        const isRecv = data.toUserID === this.storeCurrentUserID;
        if (isRecv) {
          this.updateRecvFriendApplition({
            application: data,
          });
        } else {
          this.updateSentFriendApplition({
            application: data,
          });
        }
      };
      const groupApplicationNumHandler = ({ data }) => {
        const isRecv = data.userID !== this.storeCurrentUserID;
        if (isRecv) {
          this.pushNewRecvGroupApplition(data);
        } else {
          this.pushNewSentGroupApplition(data);
        }
      };
      const groupApplicationAccessHandler = ({ data }) => {
        const isRecv = data.userID !== this.storeCurrentUserID;
        if (isRecv) {
          this.updateRecvGroupApplition({
            application: data,
          });
        } else {
          this.updateSentGroupApplition({
            application: data,
          });
        }
      };

      IMSDK.subscribe(
        IMSDK.IMEvents.OnFriendApplicationAdded,
        friendApplicationNumHandler
      );
      IMSDK.subscribe(
        IMSDK.IMEvents.OnFriendApplicationAccepted,
        friendApplicationAccessHandler
      );
      IMSDK.subscribe(
        IMSDK.IMEvents.OnFriendApplicationRejected,
        friendApplicationAccessHandler
      );
      IMSDK.subscribe(
        IMSDK.IMEvents.OnGroupApplicationAdded,
        groupApplicationNumHandler
      );
      IMSDK.subscribe(
        IMSDK.IMEvents.OnGroupApplicationAccepted,
        groupApplicationAccessHandler
      );
      IMSDK.subscribe(
        IMSDK.IMEvents.OnGroupApplicationRejected,
        groupApplicationAccessHandler
      );

      // conversation
      const totalUnreadCountChangedHandler = ({ data }) => {
        if (this.storeIsSyncing) {
          return;
        }
        this.$store.commit("conversation/SET_UNREAD_COUNT", data);
      };
      const newConversationHandler = ({ data }) => {
        if (this.storeIsSyncing) {
          return;
        }
        const result = [...data, ...this.storeConversationList];
        this.$store.commit(
          "conversation/SET_CONVERSATION_LIST",
          conversationSort(result)
        );
      };
      const conversationChangedHandler = ({ data }) => {
        if (this.storeIsSyncing) {
          return;
        }
        let filterArr = [];
        console.log(data);
        const chids = data.map((ch) => ch.conversationID);
        filterArr = this.storeConversationList.filter(
          (tc) => !chids.includes(tc.conversationID)
        );
        const idx = data.findIndex(
          (c) =>
            c.conversationID === this.storeCurrentConversation.conversationID
        );
        if (idx !== -1)
          this.$store.commit(
            "conversation/SET_CURRENT_CONVERSATION",
            data[idx]
          );
        const result = [...data, ...filterArr];
        this.$store.commit(
          "conversation/SET_CONVERSATION_LIST",
          conversationSort(result)
        );
      };

      IMSDK.subscribe(
        IMSDK.IMEvents.OnTotalUnreadMessageCountChanged,
        totalUnreadCountChangedHandler
      );
      IMSDK.subscribe(IMSDK.IMEvents.OnNewConversation, newConversationHandler);
      IMSDK.subscribe(
        IMSDK.IMEvents.OnConversationChanged,
        conversationChangedHandler
      );

      // 撤回消息回调
      const messageRevokedHandler = ({ data }) => {
        if (this.storeIsSyncing) {
          return;
        }
        // 兼容：如果没有 contentType，补充为撤回类型
        if (!data.contentType) {
          data.contentType = MessageType.RevokeMessage;
          data.notificationElem = {
            detail: JSON.stringify({
              revokerID: data.revokerID,
              revokerNickName: data.revokerNickname || data.revokerID
            })
          };
        }
        // 更新被撤回的消息为撤回通知消息
        this.updateOneMessage({
          message: data,
          type: UpdateMessageTypes.Overall,
        });
      };

      IMSDK.subscribe(
        IMSDK.IMEvents.OnNewRecvMessageRevoked,
        messageRevokedHandler
      );
    },

    tryLogin() {
      const initStore = () => {
        this.$store.dispatch("user/getSelfInfo");
        this.$store.dispatch("conversation/getConversationList");
        this.$store.dispatch("conversation/getUnReadCount");
        this.$store.dispatch("contact/getFriendList");
        this.$store.dispatch("contact/getGrouplist");
        this.$store.dispatch("contact/getBlacklist");
        this.$store.dispatch("contact/getRecvFriendApplications");
        this.$store.dispatch("contact/getSentFriendApplications");
        this.$store.dispatch("contact/getRecvGroupApplications");
        this.$store.dispatch("contact/getSentGroupApplications");
        uni.switchTab({
          url: "/pages/conversation/conversationList/index?isRedirect=true",
        });
      };
      getDbDir()
        .then(async (path) => {
          const flag = await IMSDK.asyncApi(IMMethods.InitSDK, IMSDK.uuid(), {
            systemType: "uni-app",
            apiAddr: config.getApiUrl(), // SDK的API接口地址。如：http://xxx:10002
            wsAddr: config.getWsUrl(), // SDK的websocket地址。如： ws://xxx:10001
            dataDir: path, // 数据存储路径
            logLevel: 6,
            logFilePath: path,
            isLogStandardOutput: true,
            isExternalExtensions: false,
          });
          if (!flag) {
            plus.navigator.closeSplashscreen();
            uni.$u.toast("初始化IMSDK失败！");
            return;
          }
          const status = await IMSDK.asyncApi(
            IMSDK.IMMethods.GetLoginStatus,
            IMSDK.uuid()
          );
          if (status === 3) {
            initStore();
            return;
          }

          const IMToken = uni.getStorageSync("IMToken");
          const IMUserID = uni.getStorageSync("IMUserID");
          if (IMToken && IMUserID) {
            IMSDK.asyncApi(IMSDK.IMMethods.Login, IMSDK.uuid(), {
              userID: IMUserID,
              token: IMToken,
            })
              .then(initStore)
              .catch((err) => {
                console.log(err);
                uni.removeStorage({
                  key: "IMToken",
                });
                uni.removeStorage({
                  key: "BusinessToken",
                });
                plus.navigator.closeSplashscreen();
              });
          } else {
            plus.navigator.closeSplashscreen();
          }
        })
    },
    handleNewMessage(newServerMsg) {
      if (this.inCurrentConversation(newServerMsg)) {
        if (
          newServerMsg.contentType !== MessageType.TypingMessage &&
          newServerMsg.contentType !== MessageType.RevokeMessage
        ) {
          newServerMsg.isAppend = true;
          this.pushNewMessage(newServerMsg);
          setTimeout(() => uni.$emit(PageEvents.ScrollToBottom, true));
          uni.$u.debounce(this.markConversationAsRead, 2000);
        }
      }
    },
    inCurrentConversation(newServerMsg) {
      switch (newServerMsg.sessionType) {
        case SessionType.Single:
          return (
            newServerMsg.sendID === this.storeCurrentConversation.userID ||
            (newServerMsg.sendID === this.storeCurrentUserID &&
              newServerMsg.recvID === this.storeCurrentConversation.userID)
          );
        case SessionType.WorkingGroup:
          return newServerMsg.groupID === this.storeCurrentConversation.groupID;
        case SessionType.Notification:
          return newServerMsg.sendID === this.storeCurrentConversation.userID;
        default:
          return false;
      }
    },
    markConversationAsRead() {
      IMSDK.asyncApi(
        IMSDK.IMMethods.MarkConversationMessageAsRead,
        IMSDK.uuid(),
        this.storeCurrentConversation.conversationID
      );
    },
  },
};

function checkUpdate(forceCheck = false) {
  const localVersion = plus.runtime.version;
  const localWgtVersion = uni.getStorageSync('wgtVersion') || localVersion;
  uni.request({
    url: 'http://47.117.71.33:11295/fadapp-update/version.json?t=' + Date.now(),
    success: (res) => {
      const remoteVersion = res.data.version;
      const wgtUrl = res.data.wgtUrl;
      // 取本地基座和本地wgt的最大版本
      const currentVersion = compareVersion(localWgtVersion, localVersion) > 0 ? localWgtVersion : localVersion;
      console.log('本地基座版本:', localVersion, '本地wgt版本:', localWgtVersion, '当前对比版本:', currentVersion, '远程版本:', remoteVersion);
      if (compareVersion(remoteVersion, currentVersion) > 0) {
        // 检查版本兼容性
        if (!checkVersionCompatibility(remoteVersion, localVersion)) {
          console.warn('版本不兼容，跳过更新');
          uni.showModal({
            title: '版本不兼容',
            content: `新版本 ${remoteVersion} 与当前基座版本 ${localVersion} 不兼容，请通过应用商店更新完整版本。`,
            showCancel: false,
            confirmText: '确定'
          });
          return;
        }
        
        // 检查是否已忽略当前版本（除非强制检查）
        const ignoredVersion = uni.getStorageSync('ignoredVersion');
        if (!forceCheck && ignoredVersion === remoteVersion) {
          console.log('用户已选择忽略此版本:', remoteVersion);
          return;
        }
        
        uni.showModal({
          title: '发现新版本',
          content: `检测到新版本（${remoteVersion}），是否立即下载并更新？`,
          confirmText: '立即更新',
          cancelText: '暂不更新',
          showCancel: true,
          success: (modalRes) => {
            if (modalRes.confirm) {
              // 检查存储空间
              checkStorageSpace().then(() => {
                uni.showLoading({title: '正在下载更新包...'});
                console.log('开始下载wgt包:', wgtUrl);
              uni.downloadFile({
                url: wgtUrl,
                success: (downloadResult) => {
                  uni.hideLoading();
                  console.log('下载结果:', downloadResult);
                  if (downloadResult.statusCode === 200) {
                    console.log('开始安装wgt包:', downloadResult.tempFilePath);
                    // 检查文件是否存在
                    plus.io.resolveLocalFileSystemURL(downloadResult.tempFilePath, (entry) => {
                      console.log('文件存在，开始安装');
                      plus.runtime.install(downloadResult.tempFilePath, {force: true}, function() {
                        console.log('wgt包安装成功');
                        // 更新本地wgt版本号
                        uni.setStorageSync('wgtVersion', remoteVersion);
                        uni.showModal({
                          title: '更新完成',
                          content: '应用需要重启才能生效，是否立即重启？',
                          success: function (res) {
                            if (res.confirm) {
                              plus.runtime.restart();
                            }
                          }
                        });
                      }, function(e) {
                        console.error('wgt包安装失败:', e);
                        let errorMsg = '安装失败';
                        if (e && e.message) {
                          errorMsg += ': ' + e.message;
                        }
                        if (e && e.code) {
                          errorMsg += ' (错误代码: ' + e.code + ')';
                        }
                        uni.showModal({
                          title: '安装失败',
                          content: errorMsg + '\n\n可能的原因：\n1. 版本不兼容\n2. 文件损坏\n3. 权限不足\n4. 存储空间不足',
                          showCancel: false,
                          confirmText: '确定'
                        });
                      });
                    }, (error) => {
                      console.error('文件不存在:', error);
                      uni.showModal({
                        title: '安装失败',
                        content: '下载的文件不存在或已损坏',
                        showCancel: false,
                        confirmText: '确定'
                      });
                    });
                  } else {
                    console.error('下载失败，状态码:', downloadResult.statusCode);
                    uni.showModal({
                      title: '下载失败',
                      content: `服务器返回错误，状态码: ${downloadResult.statusCode}`,
                      showCancel: false,
                      confirmText: '确定'
                    });
                  }
                },
                fail: (error) => {
                  uni.hideLoading();
                  console.error('下载失败:', error);
                  uni.showModal({
                    title: '下载失败',
                    content: '网络连接异常，请检查网络后重试',
                    showCancel: false,
                    confirmText: '确定'
                  });
                }
              });
            }).catch((error) => {
              console.error('存储空间检查失败:', error);
              uni.showModal({
                title: '存储空间不足',
                content: '设备存储空间不足，无法下载更新包',
                showCancel: false,
                confirmText: '确定'
              });
            });
            } else {
              // 用户选择暂不更新，询问是否忽略此版本
              uni.showModal({
                title: '忽略更新',
                content: `是否忽略版本 ${remoteVersion}？忽略后下次启动时将不再提示此版本更新。`,
                confirmText: '忽略此版本',
                cancelText: '下次提醒',
                success: (ignoreRes) => {
                  if (ignoreRes.confirm) {
                    uni.setStorageSync('ignoredVersion', remoteVersion);
                    uni.showToast({title: '已忽略此版本更新'});
                  }
                }
              });
            }
          }
        });
      } else {
        // uni.showToast({title: '当前已是最新版本'});
        console.log('当前已是最新版本');
      }
    },
    fail: (error) => {
      console.error('检查更新失败:', error);
      uni.showToast({title: '网络异常，请稍后重试'});
    }
  });
}
function extractVersionNum(str) {
  // 匹配第一个出现的数字版本号
  const match = str.match(/(\d+\.\d+(?:\.\d+)?)/);
  return match ? match[1] : '0.0.0';
}

function compareVersion(v1, v2) {
  v1 = extractVersionNum(v1).split('.').map(Number);
  v2 = extractVersionNum(v2).split('.').map(Number);
  for (let i = 0; i < Math.max(v1.length, v2.length); i++) {
    const n1 = v1[i] || 0;
    const n2 = v2[i] || 0;
    if (n1 > n2) return 1;
    if (n1 < n2) return -1;
  }
  return 0;
}

// 更新管理工具函数
function clearIgnoredVersion() {
  uni.removeStorageSync('ignoredVersion');
  console.log('已清除忽略的版本设置');
}

function getIgnoredVersion() {
  return uni.getStorageSync('ignoredVersion');
}

function setIgnoredVersion(version) {
  uni.setStorageSync('ignoredVersion', version);
  console.log('已设置忽略版本:', version);
}

// 检查版本兼容性
function checkVersionCompatibility(wgtVersion, baseVersion) {
  console.log('检查版本兼容性:', { wgtVersion, baseVersion });
  
  // 提取主版本号进行比较
  const wgtMajor = extractVersionNum(wgtVersion).split('.')[0];
  const baseMajor = extractVersionNum(baseVersion).split('.')[0];
  
  // 主版本号必须匹配
  if (wgtMajor !== baseMajor) {
    console.warn('主版本号不匹配:', { wgtMajor, baseMajor });
    return false;
  }
  
  // 检查wgt版本是否高于基座版本
  if (compareVersion(wgtVersion, baseVersion) <= 0) {
    console.warn('wgt版本不高于基座版本');
    return false;
  }
  
  return true;
}

// 检查存储空间
function checkStorageSpace() {
  return new Promise((resolve, reject) => {
    // #ifdef APP-PLUS
    if (typeof plus !== 'undefined' && plus.io) {
      plus.io.requestFileSystem(plus.io.PRIVATE_DOC, (fs) => {
        fs.root.getDirectory('temp', { create: true }, (dir) => {
          // 简单检查，实际项目中可能需要更复杂的空间检查
          resolve(true);
        }, (error) => {
          console.error('检查存储空间失败:', error);
          reject(error);
        });
      });
    } else {
      resolve(true);
    }
    // #endif
    
    // #ifndef APP-PLUS
    resolve(true);
    // #endif
  });
}

// 导出更新管理函数供其他页面使用
uni.$updateManager = {
  checkUpdate: (forceCheck = false) => checkUpdate(forceCheck),
  clearIgnoredVersion,
  getIgnoredVersion,
  setIgnoredVersion,
  checkVersionCompatibility,
  checkStorageSpace
};
</script>

<style lang="scss">
/*每个页面公共css */
@import "@/uni_modules/uview-ui/index.scss";
@import "@/styles/login.scss";
@import "@/styles/global.scss";

uni-page-body {
  height: 100vh;
  overflow: hidden;
}

.uni-tabbar .uni-tabbar__icon {
  width: 28px !important;
  height: 28px !important;
}
</style>
