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

const app = getApp();

Page({
  data: {
    ListOther: [{
      Key: '愛', Type: 'zici', TypeName: '字词', Remark: 'ài', Content:'对人或事物有很深的感情'
    }, {
        Key: '饕餮', Type: 'zici', TypeName: '字词', Remark: 'tāo tiè', Content: '传说中的一种凶恶贪食的野兽'
    }, {
        Key: '鸿鹄之志', Type: 'zici', TypeName: '字词', Remark: 'hóng hú zhī zhì', Content:'比喻远大的志向'
    }, {
        Key: '咏鹅', Type: 'shici', Author: '骆宾王', Dynasty: '唐', TypeName: '诗词', Content: '鹅鹅鹅，曲项向天歌。白毛浮绿水，红掌拨清波。'
    }, {
        Key: '天净沙·秋思', Type: 'shici', Author: '马致远', Dynasty: '元', TypeName: '诗词', Content: '枯藤老树昏鸦，小桥流水人家，古道西风瘦马......'
    }, {
        Key: 'success', Type: 'eng', TypeName: '翻译', Remark:'[səkˈses]', Content: 'n.成功，成就'
    },{
        Key: 'Nice to meet you', Type: 'eng', TypeName: '翻译', Content: '很高兴见到你！'
    },
    ]
  },
  onLoad: function () {
    var that = this;
    that.setData({
      Containnerheight: main.getWindowHeight(),
      IsStart: true,
    });
    common.getStorageValue(that, "SearchTextList", [], function () {
    });
  },
  onKeyInput: function (e) {
    var search = e.detail.value;
    var that = this;
    that.setData({
      SearchInfo: search,
    });
  },
  onBindfocus:function(){
    this.setData({
      IsStart:true,
      List:[],
    });
    wx.setNavigationBarTitle({
      title: "搜索中"
    });
  },
  onSearch: function (e) {
    var that = this;
    if (e.currentTarget.dataset.search)
      this.data.SearchInfo = e.currentTarget.dataset.search;
    if (this.data.SearchInfo && this.data.SearchInfo.length > 0) {
      var search = this.data.SearchInfo;

      that.searchResult(search);

      setTimeout(function(){
        var arr = that.data.SearchTextList;
        for (var i = 0; i < arr.length; i++) {
          if (arr[i] == search) {
            arr.splice(i, 1);
            break;
          }
        }
        arr.unshift(search);

        while (arr.length > 10){
          arr.pop();
        }

        wx.setStorageSync("SearchTextList", arr);
        that.setData({
          SearchTextList: arr,
        });
      },2000);
      
    }
    else {
      wx.showToast({
        title: '请输搜索内容',
      })
    }
  },
  onSearchHistory: function (e) {
    var index = e.currentTarget.dataset.index;
    var listtype = e.currentTarget.dataset.type;
    var list = this.data.List;
    if (listtype=="null")
      list=this.data.ListOther;
    var obj={};
    for(var i=0;i<list.length;i++){
      if (index==i){
        obj=list[i];
        break;
      }
    }
    this.searchResult(obj.Key,obj.Type,obj.Author,obj.Url);
    
  },
  close: function (e) {
    wx.navigateBack({
      delta: 1,
    });
  },

  deleteItem: function (e) {
    var that = this;
    that.setData({
      SearchTextList: [],
    });
    wx.removeStorageSync("SearchTextList");
  },
  searchResult: function (search,searchType,author,shiciUrl) {

    wx.showLoading({
      title: '查询中',
    });
    setTimeout(function () {
      wx.hideLoading();
    }, 5000);
    
    var that = this;

    var url = 'GetMiaoguoAISearch2?UserID=' + app.globalData.userInfo.UserID;
    url += "&Word=" + search;
    if (searchType)
      url += "&SearchType=" + searchType;
    if (author)
      url += "&Author=" + author;
    if (shiciUrl)
      url += "&ShiciUrl=" + shiciUrl;
    main.getData(url, function (data) {
      wx.hideLoading();
      wx.setNavigationBarTitle({
        title: "搜索结果"
      });
      searchType = "";
      author = "";
      if (data) {
        //console.log(data);

        if (data.List) {
          var len = 26;
          var list = data.List;
          for (var i = 0; i < list.length; i++) {
            var item = list[i];
            if (item.Content && item.Content.length > len)
              item.Content = item.Content.substr(0, len) + "...";
          }
          that.setData({
            List: list,
            IsStart: false,
          });
        }
        else {
          var obj = {};
          obj.Key=search;
          obj.Value = data;
          if (data.CHN && data.CHN.Author)
            obj.Author = data.CHN.Author;
          if (data.CHN && data.CHN.Dynasty)
            obj.Dynasty = data.CHN.Dynasty;
          if (data.CHN && data.CHN.PeomContent) {
            obj.Type = "shici";
            obj.TypeName = "诗词";
            obj.Content = data.CHN.PeomContent.join("").substr(0, 26);
            obj.ShiciUrl=shiciUrl;
          }
          else if (data.CHN){
            obj.Type = "zici";
            if (data.CHN.PinYin && data.CHN.PinYin[0] && data.CHN.PinYin[0].pinyin)
              obj.Remark = data.CHN.PinYin[0].pinyin;
            if (data.CHN.PinYin && data.CHN.PinYin[0] && data.CHN.PinYin[0].explain){
              obj.Content = data.CHN.PinYin[0].explain.substr(0, 26);
              obj.Content = obj.Content.replace("<p>", "");
              obj.Content = obj.Content.replace("</p>", "");
              if (obj.Content.length>=26)
                obj.Content += "..."
            }
          }
          if (data.ENG && !data.CHN) {
            obj.Type = "eng";
            obj.TypeName ="翻译";
            if (data.ENG.Soundmark && data.ENG.Soundmark.Eng)
              obj.Remark = data.ENG.Soundmark.Eng;
            if (data.ENG.Paraphrase.length>0){
              if (common.checkIsArray(data.ENG.Paraphrase) && data.ENG.Paraphrase[0].ParaphraseList)
                obj.Content = data.ENG.Paraphrase[0].ParaphraseList.join("; ").substr(0, 26);
              else
                obj.Content = data.ENG.Paraphrase.substr(0, 26)+"...";
            }
          }
          
          main.updateSearchList(obj);

          wx.navigateTo({
            url: './searchWeb2',
          });
        }
      }
      else {
        that.setData({
          List: [],
          IsStart: false,
        });
      }
    });

  },
  onShareAppMessage: function () {
    return {
      title: app.globalData.ShareTitle,
      path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
      imageUrl: app.globalData.ShareImage,
    }
  },
})
