import UrlParamsHandler from "../../../utils/store";
import { appversion, version } from "../../../env";
import { request } from "../../../utils/request";
const defaultAvatarUrl =
  "https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0";
// pages/live/registration/registration.ts
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {
      avatarUrl: wx.getStorageSync("avatarUrl") || defaultAvatarUrl,
      nickName: wx.getStorageSync("userName") || "未登录用户",
    },
    state: -1,
    info: "",
    content: "",
  },

  async register(state: string) {
    const app = getApp<IAppOption>();
    const bool = await app.login("register");
    if (!bool) return;
    const params = {
      openId: wx.getStorageSync("openId"),
      state,
    };

    return await request({
      options: {
        url: "/app/watch/register?version=" + version.register,
        data: params,
      },
    });
  },

  async registers(code: string) {
    const app = getApp<IAppOption>();
    const bool = await app.login("register");
    if (!bool) return;
    const params = {
      openId: wx.getStorageSync("openId"),
      code,
    };
    const data = await request({
      options: {
        url: `/app/video-watch/details?version=${version.details}&appVer=${appversion}`,
        method: "GET",
        data: params,
      },
    });

    if (!data.watch.url) {
      this.setData({
        content: data.watch.content,
        state: -1,
      });
    } else {
      wx.redirectTo({
        url: `/pages/live/video/video?openId=${params.openId}&code=${params.code}&seeId=${data.watch.id}`,
      });
    }
  },
  async refresh() {
    const params = {
      openId: wx.getStorageSync("openId"),
      code: wx.getStorageSync("code"),
    };
    const res = await request({
      options: {
        url: "/app/video-watch/refresh",
        method: "GET",
        data: params,
      },
    });
    if (res.pass)
      wx.redirectTo({
        url: `/pages/live/video/video?openId=${params.openId}&code=${params.code}`,
      });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options: any) {
    new UrlParamsHandler(options);
    wx.hideShareMenu({
      menus: ["shareAppMessage"], // 单独禁用好友分享
    });
    if (options.code && !options.content) {
      this.registers(options.code);
    }
    if (options.state && typeof options.state != "number" && !options.content) {
      const res = await this.register(options.state);
      if (res.code == 200) {
        this.setData({
          state: res.data.state,
          info: res.data.info,
        });
      }
    }
    if (options.content) {
      this.setData({
        content: options.content,
        state: options.state,
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});