// packageD/playBack/playBack.js
String.prototype.endWith = function (endStr) {
  var d = this.length - endStr.length;
  return (d >= 0 && this.lastIndexOf(endStr) == d);
};
var app = getApp();
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    liveList: [],
    heightauto: 'auto',
    widthauto: 'auto',
    minStyle: true,
    screeheight: 0,
    myvideo: '',
    rid: "",
    showVideo: "",
    showName: "",
    curr: ""
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function (options) {
    this.data.myvideo = wx.createVideoContext('myvideo');
    this.getinformation();
    if (options.rid) {
      this.setData({
        rid: options.rid,
        showName: options.name
      });
    }
    if (options.mid) {
      app._setMid(options.mid);
    }
  },

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

  },

  /**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function () {
    this.getRoom();
  },
  chooseItem(e) {
    this.setData({
      curr: e.currentTarget.dataset.index,
      showVideo: this.data.liveList[e.currentTarget.dataset.index].media_url
    });
  },
  getRoom() {
    let that = this;
    let urlStr = app.getNetAddresss("plugin.appletslive.frontend.controllers.live.getReplay");
    urlStr += "&rid=" + this.data.rid;
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          let list = [];
          for (let i = 0; i < res.data.live_replay.length; i++) {
            if (!res.data.live_replay[i].media_url.endWith("m3u8")) {
              list.push(res.data.live_replay[i]);
            }
          }
          if (list.length >= 1) {
            that.setData({
              liveList: list,
              curr: 0,
              showVideo: list[0].media_url
            });
          } else {
            wx.showToast({
              title: '当前房间号没有回放',
              icon: 'none',
              duration: 2000
            });
          }
        } else {
          console.log(res.msg);
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getinformation() {
    try {
      const res = wx.getSystemInfoSync();
      let windowHeight = res.windowHeight;
      let windowWidth = res.windowWidth;
      this.setData({
        heightauto: windowHeight + 'px',
        widthauto: windowWidth + 'px'
      });
      this.data.screeheight = windowHeight;
    } catch (e) {
      // Do something when catch error
    }
  },

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

  },

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

  },

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

  },

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

  },

  /**
	 * 用户点击右上角分享
	 */
  onShareTimeline: function () {
    let value = wx.getStorageSync('yz_uid');
    let mid = '';
    if (value) {
      mid = value;
    }
    return {
      query: 'rid=' + this.data.rid + '&name=' + this.data.showName + '&mid=' + mid
    };
  },


  /**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function () {
    var value = wx.getStorageSync('yz_uid');
    var path = '';
    if (value) {
      path = value;
    }

    let _title = "直播回放";

    let _link = '/packageD/playBack/playBack?rid=+' + this.data.rid + '+&name=' + this.data.showName + '&mid=' + path;

    return {
      title: _title,
      path: _link,
      success: function (shareTickets) {
        console.info(shareTickets + '成功');
        // 转发成功
      },
      fail: function (res) {
        console.log(res + '失败');
        // 转发失败
      }
    };
  }
});
