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

const app = getApp();

Page({
  data: {
  },
  onLoad: function (options) {
    this.setData({
      Height: common.getSystemHeight(),
      ExampleArray: this.getExampleArray(),
      IsSearchResult: false,
    });
  },
  getExampleArray: function () {
    var result = [];
    var arr = JSON.parse(wx.getStorageSync("HanziAll"));
    for (var i = 0; i < arr.length; i++) {
      var rnd1 = common.random(0, arr[i].Units.length - 1);
      var rnd2 = common.random(0, arr[i].Units[rnd1].Words.length - 1);

      var isfinished = false;
      var arrReview = wx.getStorageSync("ReviewList");
      for (var l = 0; l < arrReview.length; l++) {
        if (arrReview[l].Word == arr[i].Units[rnd1].Words[rnd2].Name) {
          isfinished = true;
          break;
        }
      }
      arr[i].Units[rnd1].Words[rnd2].BookID = arr[i].ID;
      arr[i].Units[rnd1].Words[rnd2].UnitID = arr[i].Units[rnd1].ID;
      arr[i].Units[rnd1].Words[rnd2].WordID = rnd2;
      arr[i].Units[rnd1].Words[rnd2].IsFinished = isfinished;
      result.push(arr[i].Units[rnd1].Words[rnd2]);
    }
    return result;
  },
  researchWord: function (e) {
    var that = this;
    that.setData({
      IsSearchResult: false,
    });
    var result = false;
    var word = e.detail.value;
    if (common.IsChinese(word)) {
      server.getData('SearchHanzi?Word=' + word, function (data) {
        if (data) {
          wx.setStorageSync("CurrentSearchWord", data);

          wx.setStorageSync('SelectedRandom', false);
          wx.setStorageSync('IsAutoPage', false);

          wx.navigateTo({
            url: "../main/detail?Type=search",
          });
        }
        else {
          that.setData({
            IsSearchResult: true,
          });
        }
      });
    }
    else {
      that.setData({
        IsSearchResult: true,
      });
    }
  },
  goto: function (e) {
    var word = e.currentTarget.dataset.word;
    this.researchWord({ detail: { value: word } });
  },
  onShareAppMessage: function () {
    return {
      title: app.globalData.ShareTitle,
      path: 'pages/index/index?UserID=' + app.globalData.userInfo.UserID,
      imageUrl: app.globalData.ShareImage,
    }
  },
})