import { getWedaAPI } from '@cloudbase/weda-client';
let privacyHandler;
// let privacyResolves = new Set();
let closeOtherPagePopUpHooks = new Set();

const isSuportPrivacyApi = wx.onNeedPrivacyAuthorization && wx.requirePrivacyAuthorize && wx.openPrivacyContract;

if (wx.onNeedPrivacyAuthorization) {
  wx.onNeedPrivacyAuthorization((resolve) => {
    if (typeof privacyHandler === 'function') {
      privacyHandler(resolve);
    }
  });
}

const closeOtherPagePopUp = (closePopUp) => {
  closeOtherPagePopUpHooks.forEach((hook) => {
    if (closePopUp !== hook) {
      hook();
    }
  });
};

Component({
  /**
   * 组件的属性列表
   */
  properties: {},

  /**
   * 组件的初始数据
   */
  data: {
    visible: false,
    title: getWedaAPI().app.mpAppName || '用户隐私保护提示',
    desc1: `在您使用 ${getWedaAPI().app.mpAppName || '本小程序'} 的后续服务之前，请仔细阅读并同意`,
    urlTitle: '《用户隐私保护指引》。',
    desc2: '如您拒绝，则指引中提及的相关功能使用将受限。',
  },

  lifetimes: {
    created() {
      this.resolvers = new Set();
    },
    attached: function () {
      this.resolvers = new Set();
      const closePopUp = () => {
        this.close();
      };
      privacyHandler = (resolve) => {
        this.resolvers.add(resolve);
        this.show();

        // 额外逻辑：当前页面的隐私弹窗弹起的时候，关掉其他页面的隐私弹窗
        closeOtherPagePopUp(closePopUp);
      };

      this.closePopUp = closePopUp;
      closeOtherPagePopUpHooks.add(this.closePopUp);
    },
    detached: function () {
      this.handleDisagree();
      closeOtherPagePopUpHooks.delete(this.closePopUp);
    },
  },

  pageLifetimes: {
    show() {
      if (this.closePopUp) {
        privacyHandler = (resolve) => {
          this.resolvers.add(resolve);
          this.show();
          // 额外逻辑：当前页面的隐私弹窗弹起的时候，关掉其他页面的隐私弹窗
          closeOtherPagePopUp(this.closePopUp);
        };
      }
    },
  },
  /**
   * 组件的方法列表
   */
  methods: {
    handleAgree(e) {
      this.close();
      // 这里演示了同时调用多个wx隐私接口时要如何处理：让隐私弹窗保持单例，点击一次同意按钮即可让所有pending中的wx隐私接口继续执行
      this.resolvers.forEach((resolve) => {
        resolve({
          event: 'agree',
          buttonId: 'agree-btn',
        });
      });
      this.resolvers.clear();
    },
    handleDisagree() {
      this.close();
      this.resolvers.forEach((resolve) => {
        resolve({
          event: 'disagree',
        });
      });
      this.resolvers.clear();
    },
    openPrivacyContract() {
      return new Promise((resolve, reject) => {
        if (wx.openPrivacyContract) {
          wx.openPrivacyContract({
            success: resolve,
            fail: reject,
          });
        } else {
          reject(new Error('wx.openPrivacyContract is not a function'));
        }
      }).catch((e) => {
        // wx.previewImage({
        //   urls: ['https://img-operation.csdnimg.cn/csdn/silkroad/img/1693902370785.jpg']
        // })
      });
    },
    close() {
      if (this.data.visible) {
        this.setData({
          visible: false,
        });
      }
    },
    show() {
      if (this.data.visible === false) {
        this.setData({
          visible: true,
        });
      }
    },
  },
});
