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

const app = getApp();
var hasStart = {};

Page({
  data: {
    ImagePath: app.globalData.uploadImageUrl,
    IsShowHelp: 1,
    IsPoemRead: 0,
    ShowKaiti: 0,
    ShowSoundMark:1,
    CSS: {},
    IsHelp:false,
    HelpArr:["help_board_tips_zhidingduanluo","help_board_tips_tiaozhuanchaxun"],
  },
  onLoad: function (options) {
    var that = this;
    that.setData({
      Containnerheight: main.getWindowHeight(),
    });
    this.getList();

    hasStart = {};

  },
  getList: function () {
    if (app.globalData.SearchItem) {
      this.getListFinished(app.globalData.SearchItem);
    }
  },
  getListFinished: function (data) {
    var that = this;

    if (data.CHN) {
      if (data.CHN.PeomContent)
        data.TypeName = "S";
      else if (data.CHN.HanZi.length == 1)
        data.TypeName = "Z";
      else
        data.TypeName = "C";
    } else if (data.ENG) {
      data.TypeName = "D";
    }    
    
    if (data.CHN && data.CHN.KaitiArr) {
      if (data.CHN.KaitiArr.length==1){
        data.CHN.KaitiWidth="";
      }
      else{
        data.CHN.KaitiWidth="panelItemSmall";
      }
      
      if (data.CHN.BiShunArr2.length==1){
        data.CHN.BiShunWidth="";
      }
      else{
        data.CHN.BiShunWidth="panelItemSmall";
      }
    }

    if (data.ENG && data.ENG.Paraphrase && common.checkIsArray(data.ENG.Paraphrase)) {

      for (var i = 0; i < data.ENG.Paraphrase.length; i++) {
        var item = data.ENG.Paraphrase[i];
        item.PartOfSpeech = common.ReplaceAllString(item.PartOfSpeech, "释义", "");
      }
    }

    if (data.ENG && data.ENG.ExamplesSentences){
      for (var i = 0; i < data.ENG.ExamplesSentences.length; i++) {
        var item = data.ENG.ExamplesSentences[i];
        var obj=[{ContentType:0,Content:""},{ContentType:1,Content:item[0]},{ContentType:2,Content:""},{ContentType:3,Content:""}];
        var obj2=main.changeStringToView(obj);
        var str=obj2.Field[1][0].Content;
        item.push(str);
        
        
        item[1]=common.ReplaceAllString(item[1],"<b>","");
        item[1]=common.ReplaceAllString(item[1],"</b>","");
      }
    }

    that.setData({
      Result: data,
    })
  },
  getInputData: function (e) {
    //console.log("time1:"+new Date().getTime());
    common.getStorageValue(this, "IsShowHelp", 0, function () {

    });

    var type = e.currentTarget.dataset.type;
    var css = e.currentTarget.dataset.css;
    var tag = e.currentTarget.dataset.tag;
    var content = e.currentTarget.dataset.content;
    var soundmark = e.currentTarget.dataset.soundmark;
    //console.log(content);
    //console.log("time2:" + new Date().getTime());
    if (!this.data.CSS[css] || this.data.CSS[css].Number == "") {
      if (!this.data.CSS[css])
        this.data.CSS[css] = {};
      this.data.CSS[css].Css1 = "Select1";
      this.data.CSS[css].Css2 = "Select2";
      this.data.CSS[css].Content = content;
      this.data.CSS[css].Type = type;
      this.data.CSS[css].Tag = tag;
      if (tag == "发音")
        this.data.CSS[css].SoundMark = soundmark;

      if (tag == "默写" || tag=="拼音") {
        var soundmark = e.currentTarget.dataset.soundmark;
        if (soundmark)
          this.data.CSS[css].SoundMark = e.currentTarget.dataset.soundmark;
      }

      this.data.CSS[css].Number = "1";
      if (app.globalData.TempFieldNumber > 0 && !hasStart[css]) {
        this.data.CSS[css].Number = app.globalData.TempFieldNumber;
        hasStart[css] = true;
      }
    }
    else if (this.data.CSS[css].Number == "3") {
      this.data.CSS[css].Css1 = "";
      this.data.CSS[css].Css2 = "";
      this.data.CSS[css].Number = "";
      this.data.CSS[css].Content = "";
      this.data.CSS[css].Type = "";
      this.data.CSS[css].Tag = "";
    }
    else {
      this.data.CSS[css].Number = Number(this.data.CSS[css].Number) + 1;
    }
    //console.log("time3:" + new Date().getTime());
    this.setData({
      CSS: this.data.CSS,
    });
    //console.log("time4:" + new Date().getTime());
  },
  addData: function () {
    if (JSON.stringify(this.data.CSS) != "{}") {
      for (var item in this.data.CSS) {
        if (this.data.CSS[item].Number == "") {
          delete this.data.CSS[item];
        }
      }
      app.globalData.TempFieldNumber = 0;
      var id = app.globalData.TempMiaoguoCardID;
      var list = app.globalData.CardList;
      for (var i = 0; i < list.length; i++) {
        if (id == list[i].MiaoguoCardID) {
          for (var key in this.data.CSS) {
            var item = this.data.CSS[key];
            if (item.Tag) {
              var strArrTag = list[i].Content[0].Content + ",";
              if (list[i].Content[0].Content.constructor == Array)
                strArrTag = list[i].Content[0].Content.join(",") + ",";
              else
                list[i].Content[0].Content = list[i].Content[0].Content.split(",");

              var tags = item.Tag.split(",");
              for (var j = 0; j < tags.length; j++) {
                if (tags[i]) {
                  if (strArrTag.indexOf(tags[i] + ",") < 0)
                    list[i].Content[0].Content.push(tags[i]);
                }
              }
            }

            var str = "";
            if (item.Type == "image") {
              item.Content = main.encryptUrl(item.Content);

              str = "[图]" + item.Content + "[/图]";
            }
            else if (item.Type == "sound") {
              str = "[读]" + item.Content + "[/读]";
              if (item.Tag == "发音") {
                var strTemp=item.Content.substr(0,1);
                if (this.data.ShowSoundMark=="1")
                  str = "[读 src='" + item.SoundMark + "']" + item.Content + "[/读]";
                else
                  str = "[读 src='" + item.SoundMark + "']"+strTemp+"式[/读]";
              }
              else if (item.Tag == "拼音" || item.Tag == "默写") {
                if (item.SoundMark){
                  if (this.data.Result.CHN.HanZi)
                    str = "[读 src='" + this.data.Result.CHN.HanZi + "']" + item.Content + "[/读]";
                  else
                    str = "[读 src='" + item.SoundMark + "']" + item.Content + "[/读]";
                }
                else
                  str = "[读 src='"+this.data.Result.CHN.HanZi+"("+ main.getPinyinNormal(item.Content) +")"+"']" + item.Content + "[/读]";
              }
            }
            else {
              if (item.Tag == "诗词" || item.Tag == "译文"){
                if (common.checkIsArray(item.Content) 
                  && common.checkIsArray(item.Content[0])){
                  for (var j = 0; j < item.Content.length; j++) {
                    if (common.checkIsArray(item.Content[j])){
                      item.Content[j] = item.Content[j].join("");
                    }
                  }
                  if (common.checkIsArray(item.Content))
                    str = item.Content.join("\n");
                  else
                    str = item.Content;
                }
                else{
                  if (common.checkIsArray(item.Content))
                    str = item.Content.join("");
                  else
                    str = item.Content;
                }

                if (item.Tag == "诗词" && this.data.IsPoemRead==1){
                  var arr=str.split("\n");
                  var arr2=[];
                  for(var j=0;j<arr.length;j++){
                    arr2.push("[读]" +arr[j]+ "[/读]");
                  }
                  str=arr2.join("\n");
                }
              }
              else
                str = item.Content;
            }

            if (this.data.Result && this.data.Result.CHN && this.data.Result.CHN.PinYin && this.data.Result.CHN.PinYin.length == 1){
              if (item.Tag=="释义"){
                str=str.substr(str.indexOf("[/读]")+4);
              }
            }

            if (list[i].Content[item.Number].Content){
              if (item.Tag == "组词" || item.Tag == "近义词" || item.Tag == "反义词"){
                str = " " + str;
              }
              else{
                str = "\n\n" + str;
              }
            }

            list[i].Content[item.Number].Content += str;

            if (list[i].Content[item.Number].Content.indexOf("\n\n")==0)
              list[i].Content[item.Number].Content=list[i].Content[item.Number].Content.substr(2);
            
            if (item.Tag == "诗词"){
              if (list[i].Content[item.Number].Content.indexOf("\n")>0 && list[i].Content[item.Number].Content.indexOf("[")!=0){
                list[i].Content[item.Number].Content="\n\n"+list[i].Content[item.Number].Content;
              }
            }
            
          }
          break;
        }
      }
      app.globalData.CardList = list;

      wx.navigateBack({
        delta: app.globalData.TempSearchBackNumber,
      });
    }
    else {
      wx.showToast({
        title: '请选择内容',
      })
    }
  },
  changeBishun:function(e){
    var id=e.currentTarget.dataset.id;

    if (id === "0"){
      for (var i = 0; i < 6; i++) {
        delete this.data.CSS["CHN_BiShunArr2_" + i];
      }
    }
    else if (id === "1") {
      for (var i = 0; i < 6; i++) {
        delete this.data.CSS["CHN_KaitiArr_" + i];
      }
    }

    this.setData({
      ShowKaiti: id,
      CSS:this.data.CSS,
    });

  },
  changeSoundMark:function(e){
    var id=e.currentTarget.dataset.id;
    this.setData({
      ShowSoundMark: id,
    });
  },
  changeRead: function (e) {
    var id = e.currentTarget.dataset.id;
    var count=Object.getOwnPropertyNames(this.data.CSS).length;
    for (var i=0;i<count;i++) {
      delete this.data.CSS["CHN_PeomContent_" + i];
    }
    delete this.data.CSS["CHN_PeomContent"];
    this.setData({
      IsPoemRead: id,
      CSS:this.data.CSS,
    });
  },
  changeInfomation:function(e){
    var word=e.currentTarget.dataset.word;
    if (word.indexOf(";")>0){
      word=word.substr(0,word.indexOf(";"));
    }
    var url = 'GetMiaoguoAISearch2?UserID=' + app.globalData.userInfo.UserID;
    url += "&Word=" + word;
    if (!e.currentTarget.dataset.ischn)
      url += "&SearchType=zici";
    
    wx.showLoading();
    
    main.getData(url, function (data) {
      wx.hideLoading();
      if (data && (data.CHN || data.ENG)){
        var obj={};
        var len = 26;
        obj.Value=data;
          
        if (data.CHN){
          obj.Key=data.CHN.HanZi;
          obj.Content=data.CHN.PinYin[0].explain;

          if (obj.Key.length==1)
            obj.TypeName="Z";
          else
            obj.TypeName="C";
          
          obj.Url="&UserID="+app.globalData.userInfo.UserID+"&Word="+obj.Key;
        
        }
        else if (data.ENG){
          obj.Key=data.ENG.Word;
          obj.TypeName="D";
          if (data.ENG.Soundmark)
            obj.Remark=data.ENG.Soundmark.Eng;
          obj.Content=data.ENG.Paraphrase[0].ParaphraseList;

          obj.Url="&UserID="+app.globalData.userInfo.UserID+"&Word="+obj.Key;
        }
          
        
        if (data.CHN && data.CHN.PinYin && data.CHN.PinYin.length>0){
          obj.Remark=data.CHN.PinYin[0].pinyin;
        }

        if (obj.Content && obj.Content.length > len)
            obj.Content = obj.Content.substr(0, len) + "...";

        main.updateSearchList(obj);
        if (app.globalData.TempSearchBackNumber==0)
          app.globalData.TempSearchBackNumber=1;
        else if (app.globalData.TempSearchBackNumber>=1)
          app.globalData.TempSearchBackNumber=2;
        

        wx.redirectTo({
          url: './addInfomationDetail',
        });
      }
      else{
        wx.showToast({
          title: '无资料',
          image: "../images/universalpic_exclamation_white_120x120.png",
        });
      }
    });
  },
  openHelp: function () {
    this.setData({
      IsShowHelp: 0,
    });
  },
  showHelp: function () {
    var that = this;
    that.setData({
      IsHelp: true,
    });
  },
  closeHelp: function () {
    this.setData({
      IsHelp: false,
    });
  },
  onShareAppMessage: function () {
    return {
      title: app.globalData.ShareTitle,
      path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
      imageUrl: app.globalData.ShareImage,
    }
  },
})
