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

const app = getApp();
var isClick = true;

Page({
  data: {
    IsSelect: false,
    ImagePath: app.globalData.uploadImageUrl,
  },
  onLoad: function (options) {
    var that = this;
    that.setData({
      BookID: options.id,
      Containnerheight: main.getWindowHeight(),
      Name1: options.name1,
    });
    isClick = true;
    main.getHanzi(function (arr) {
      if (!arr) {
        wx.removeStorageSync("HanziUpdateTime");
        wx.removeStorageSync("HanziAll");
        main.getHanzi(function (arr) {
          that.init(options);
        });
      } else {
        that.init(options);
      }
    });
  },
  init: function (options) {
    var that = this;
    var name = options.name1;
    if (name.indexOf(" ") > 0)
      name = name.substr(name.indexOf(" ") + 1);
    wx.setNavigationBarTitle({
      title: name
    });
    var arr = [];
    var listType="hanzi";
    if (that.data.BookID > 100){
      arr = wx.getStorageSync("EnglishAll");
      listType="english";
    }
    else
      arr = wx.getStorageSync("HanziAll");
    
    that.setData({
      ListType: listType,
    });
      
    arr = JSON.parse(arr);
    var id = Number(options.id);
    
    var testReportList = wx.getStorageSync("UserTestReport");
    
    for (var i = 0; i < arr.length; i++) {
      if (arr[i].ID == id) {
        var list = arr[i].Units;
        for (var j = 0; j < list.length; j++) {
          var str = "";
          for (var k = 0; k < list[j].Words.length; k++) {
            if (k < 27 && k < list[j].Words.length - 1)
              str += list[j].Words[k] + "、";
            else if (k == list[j].Words.length - 1)
              str += list[j].Words[k];
            else {
              str += list[j].Words[k] + "…";
              break;
            }
          }

          list[j].WordList = str;
          list[j].TestTypeReadFinished = true;
          list[j].TestTypeWriteFinished = true;

          for (var k = 0; k < testReportList.length; k++) {
            if (testReportList[k].Name[0] == options.name1 &&
              testReportList[k].Name[1] == list[j].Name) {

              var testRightCount = 0;
              if (testReportList[k].TestRightStr)
                testRightCount = testReportList[k].TestRightStr.length;
              var testWrongCount = 0;
              if (testReportList[k].testWrongStr)
                testWrongCount = testReportList[k].testWrongStr.length;
              var testSkipCount = 0;
              if (testReportList[k].TestSkipStr)
                testSkipCount = testReportList[k].TestSkipStr.length;


              if (testReportList[k].TestType == "read") {
                if (testReportList[k].IsFinished == 1)
                  list[j].TestTypeRead = 1;

                if ((testRightCount + testWrongCount + testSkipCount) < testReportList[k].TestTotal) {
                  list[j].TestTypeReadFinished = false;
                }

                list[j].ReadReportID = testReportList[k].ID;
              }
              if (testReportList[k].TestType == "write") {
                if (testReportList[k].IsFinished == 1)
                  list[j].TestTypeWrite = 1;

                if ((testRightCount + testWrongCount + testSkipCount) < testReportList[k].TestTotal) {
                  list[j].TestTypeWriteFinished = false;
                }

                list[j].WriteReportID = testReportList[k].ID;
              }
            }
          }
        }
        that.setData({
          List: list,
          UnitsID: id,
        });
      }
    }
  },
  onShowFrame: function (e) {
    var selectItem;
    var id = e.currentTarget.dataset.id;
    for (var i = 0; i < this.data.List.length; i++) {
      if (id == this.data.List[i].ID) {
        selectItem = this.data.List[i];
      }
    }
    this.setData({
      IsSelect: true,
      SelectItem: selectItem,
    });
  },
  onClose: function () {
    this.setData({
      IsSelect: false,
    });
  },
  goto: function (e) {
    var url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: url,
    });
    this.onClose();
  },
  gotoTestReportInfo: function (e) {
    var that = this;
    if (isClick) {
      isClick = false;
      setTimeout(function () {
        isClick = true;
      }, 2000);

      var id = e.currentTarget.dataset.id;
      var userid = app.globalData.userInfo.UserID;

      if (id) {
        var reporttype = e.currentTarget.dataset.type;
        if (reporttype=="restart"){
          //main.getData('RestartTestReport?ID='+id+'&UserID=' + app.globalData.userInfo.UserID, function (data) {});
          var testReportList = wx.getStorageSync("UserTestReport");
          for(var i=0;i<testReportList.length;i++){
            if (id==testReportList[i].ID){
              testReportList[i].TestRightStr="";
              testReportList[i].TestWrongStr="";
              testReportList[i].TestSkipStr="";
              testReportList[i].TestExistStr="";
              break;
            }
          }
          wx.setStorageSync("UserTestReport", testReportList);
        }

        main.GetTestReportInfo(id, function (data) {
          data.BookID=that.data.BookID;
          wx.setStorageSync("TestTask2", data); 
          that.onClose();
          wx.navigateTo({
            url: "./item?type=" + reporttype+"&bookid="+that.data.BookID,
          });
        });
      } else {
        that.onClose();
        var url = e.currentTarget.dataset.url+"&bookid="+that.data.BookID;
        wx.navigateTo({
          url: url,
        });
      }
    }
  },
  onShareAppMessage: function () {
    return {
      title: app.globalData.ShareTitle,
      path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
      imageUrl: app.globalData.ShareImage,
    }
  },
})