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

const app = getApp();
var currentIndex = 0;
var isRight = false;
var arrTrue = [];

Page({
  data: {
    CurrentIndex: 1,
    AnswerTrue: 0,
    AnswerError: 0,
    AnswerErrorMax: 3,
  },
  onLoad: function (options) {
    this.setData({
      Height: common.getSystemHeight(),
    });
    this.getList();
    this.audioCtx = wx.createAudioContext('myAudio');

  },
  getList: function () {
    var that = this;
    server.getData('GetHanziTestList', function (data) {
      if (data) {
        var list = data;
        var item = list[currentIndex];
        var pinyinList = pinyin.getPinyinArray();

        for (var i = 0; i < list.length; i++) {
          for (var k = 0; k < list[i].PinyinTest.length; k++) {
            for (var j = 0; j < pinyinList.length; j++) {
              if (list[i].PinyinTest[k].options == pinyinList[j][1]) {
                list[i].PinyinTest[k].name = pinyinList[j][2][0];

                break;
              }
            }
          }
        }
        arrTrue = [];
        that.setData({
          List: data,
          CurrentItem: item,
        });
      }
    });
  },
  selectedHanzi: function (e) {
    var id = e.currentTarget.dataset.id;
    var list = this.data.CurrentItem.PinyinTest;
    for (var i = 0; i < list.length; i++) {
      list[i].IsSelected = false;
    }
    list[id].IsSelected = true;
    if (list[id].IsAnswer)
      isRight = true;
    this.data.CurrentItem.PinyinTest = list;
    this.playAudio(list[id].options, list[id].name);

    this.setData({
      CurrentItem: this.data.CurrentItem,
    });
  },
  playAudio: function (pinyin, name) {
    
    if (pinyin) {

      //var str = name + "(" + pinyin + ")";
      var str = pinyin;
      //console.log(str);
      var url = app.globalData.audioUrlBaidu;
      url = url.replace("[token]", app.globalData.BaiduToken);
      url = url.replace("[word]", str);

      url = encodeURI(url);
      this.audioCtx.setSrc(url);
      this.audioCtx.play();
    }
  },
  gotoOK: function () {
    var that = this;

    var title = "错误";
    var icon = "none";
    if (isRight) {
      title = "正确";
      icon = "success";
      that.data.AnswerTrue++;
      for (var i = 0; i < arrTrue.length; i++) {
        if (arrTrue[i] == that.data.CurrentItem.HanziUnitID) {
          arrTrue.splice(i, 1);
          break;
        }
      }
      arrTrue.push(that.data.CurrentItem.HanziUnitID);
    }
    else {
      that.data.AnswerError++;
    }

    wx.showToast({
      title: title,
      icon: icon,
      duration: 1000,
      complete: function () {
        isRight = false;
        currentIndex++;
        if (currentIndex >= that.data.List.length) {
          currentIndex = 0;
          that.getResult(1);
        } 
        else if (that.data.AnswerError > 3) {
          currentIndex = 0;
          that.getResult(0);
        }
        else {
          that.setData({
            CurrentItem: that.data.List[currentIndex],
            CurrentIndex: currentIndex + 1,
            AnswerTrue: that.data.AnswerTrue,
            AnswerError: that.data.AnswerError,
          });
        }
      }
    })

  },
  getResult: function (isFinished) {
    var content = "";
    var arr = JSON.parse(wx.getStorageSync("HanziAll"));
    var count = 0;
    var book = arr[0].Name;
    for (var i = 0; i < arr.length; i++) {
      for (var j = 0; j < arr[i].Units.length; j++) {
        for (var k = 0; k < arrTrue.length; k++) {
          if (arrTrue[k] == arr[i].Units[j].ID) {
            count += arr[i].Units[j].Words.length;
            book=arr[i].Name;
          }
        }
      }
    }
    content = '您孩子的识字数大约是' + count + '个，建议从《' + book+'》学习起。';
    if (count == 0) {
      content = '您孩子的识字数未知，建议从《' + book +'》学习起。';
    }
    if (isFinished == 1) {
      content = '您孩子的识字数大约是' + count + '个，实在太优秀了！建议自行搜索文字学习。';
    }
    wx.showModal({
      title: '测试结果',
      content: content,
      showCancel: false,
      success: function () {
        wx.navigateBack({
          delta: 1
        })
      },
    });
  },
  onShareAppMessage: function () {
    return {
      title: app.globalData.ShareTitle,
      path: 'pages/index/index?UserID=' + app.globalData.userInfo.UserID,
      imageUrl: app.globalData.ShareImage,
    }
  },
});