import common from '../../utils/util';
import main from '../../utils/main';

const app = getApp();
var imageUrlPrefix;
var currentContent;
var backgroundAudioManager;


Page({
  data: {
    LessonID: 1,
    ArrTirtle: [],
  },
  onLoad: function (options) {
    var that = this;
    var LessonID = 1;
    var LessonPage = 1;
    if (options && options.LessonID) {
      LessonID = options.LessonID;
      var arrName=['','第一课', '第二课', '第三课', '第四课'];
      wx.setNavigationBarTitle({
        title: arrName[LessonID],
      });
    } 
    if (options && options.LessonPage) {
      LessonPage = options.LessonPage;
    }
    else
      LessonPage=1;

    that.setData({
      Containnerheight: main.getWindowHeight(),
      LessonID: LessonID,
      LessonPage: LessonPage,
    });
    backgroundAudioManager = wx.getBackgroundAudioManager();
    backgroundAudioManager.onEnded(that.bindEnded);

    imageUrlPrefix = app.globalData.uploadImageUrl + "lesson/lesson" + LessonID + "/";
    
    that.getData(LessonID,function(data){
      if (data){
        var id = that.data.LessonPage;
        if (id < 10)
          id = "0" + id;

        that.setData({
          ArrTirtle: data.List,
          IsPause: true,
          AudioSrc: imageUrlPrefix + "sounds/" + that.data.LessonPage + ".m4a",
          ImageUrl: imageUrlPrefix + "%E5%B9%BB%E7%81%AF%E7%89%87" + id + ".jpg",
        });

        if (that.data.LessonPage > 1) {
          that.setData({
            IsPause: false,
          });
          backgroundAudioManager.title = that.data.ArrTirtle[that.data.LessonPage - 1].Title;
          backgroundAudioManager.src = that.data.AudioSrc;
        }
      }
    });
    
    currentContent = [];
    app.globalData.LessonID = 0;
    app.globalData.LessonPage = 0;
  },
  getData:function(LessonID,callback){
    var list = wx.getStorageSync("LessonList");
    if (list){
      callback(list[LessonID-1]);
    }
    else{
      main.getData("GetMiaoguoLesson2?Lesson=" + LessonID + "&time=" + new Date().getTime(), function (data) {
        if (data)
          callback(data[LessonID-1]);
        else
          callback();
      });
    }
  },
  onUnload: function () {
    this.InsertMiaoguoUserLesson();
  },
  onHide: function () {
    this.InsertMiaoguoUserLesson();
  },
  InsertMiaoguoUserLesson: function () {
    if (currentContent.length > 0) {
      var url = "InsertMiaoguoUserLesson?UserID=" + app.globalData.userInfo.UserID;
      url += "&LessonID=" + this.data.LessonID;
      url += "&Content=" + currentContent.join(",");

      main.getData(url, function (data) {
        if (currentContent.length>0){
          var lastNum = currentContent[currentContent.length-1];
          currentContent=[];
          currentContent.push(lastNum);
        }
      });
    }
  },
  playAudio: function (e) {
    var that = this;
    var id = Number(e.currentTarget.dataset.id) + 1;
    currentContent.push(id);
    if (id < 10)
      id = "0" + id;
    this.setData({
      IsPause: false,
      LessonPage: Number(id),
      AudioSrc: imageUrlPrefix + "sounds/" + Number(id) + ".m4a",
      ImageUrl: imageUrlPrefix + "%E5%B9%BB%E7%81%AF%E7%89%87" + id + ".jpg",
    });
    backgroundAudioManager.title = this.data.ArrTirtle[Number(e.currentTarget.dataset.id)].Title;
    backgroundAudioManager.src = this.data.AudioSrc;
  },
  bindEnded: function () {
    var id = this.data.LessonPage;
    this.InsertMiaoguoUserLesson();
    if (id < this.data.ArrTirtle.length){
      this.playAudio({ currentTarget: { dataset: { id: id } } });
    }
    else{
      this.setData({
        IsPause: true,
      });
    }
  },
  audioPlay: function () {
    var that=this;
    if (this.data.IsPause){
      backgroundAudioManager.title = that.data.ArrTirtle[that.data.LessonPage - 1].Title;
      backgroundAudioManager.src = that.data.AudioSrc;
      this.setData({
        IsPause: false,
      });
    }
    else{
      backgroundAudioManager.pause();
      this.setData({
        IsPause: true,
      });
    }
  },
  previewImage:function(e){
    var url=e.currentTarget.dataset.url;
    wx.previewImage({
      current: url,
      urls: [url]
    });
  },
  onShareAppMessage: function () {
    var title ="课程";
    var imageUrl = "";
    if (this.data.LessonID == 5) {
      title = "结业致辞";
      imageUrl = this.data.ArrTirtle[0].ImageUrl;
    }
    else if (this.data.LessonPage>0){
      title = this.data.ArrTirtle[this.data.LessonPage - 1].Title;
      imageUrl = this.data.ArrTirtle[this.data.LessonPage - 1].ImageUrl;
      if (this.data.LessonID==1)
        imageUrl = this.data.ArrTirtle[0].ImageUrl;
    }

    imageUrl=app.globalData.uploadImageUrl+"web/"+imageUrl;

    return {
      title: title,
      path: app.globalData.SharePath + '?type=lesson&LessonID=' + this.data.LessonID + '&LessonPage=' + this.data.LessonPage+'&UserID=' + app.globalData.userInfo.UserID,
      imageUrl: imageUrl,
    }
  },
});