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

const COUNT_MAX=10;
const app = getApp();

let EntryWords="";//进来的单词，用于返回恢复
let IsEdit=false;  //是否编辑过，编辑过不恢复

Page({
  behaviors: [commonBehavior],
  data: {
    Words:"",
    importWordStr:"",//输入过的单词
    inputThreshold: 5000, // 时间阈值，单位毫秒，可根据实际情况调整
  },
  onLoad: function (options) {
    let that = this;
    EntryWords=app.globalData.SelectedWords;
    let words=app.globalData.SelectedWords.join("\n");
    that.setData({
      Containnerheight: main.getWindowHeight(),
      Words:words, 
    });
    main.checkGenerating();
  },
  handlePaste: function(e) {
    let that = this;
    // 获取剪贴板内容
    wx.getClipboardData({
      success: function(res) {
        let str="";
        if (that.data.Words)
          str+=that.data.Words+"\n";
        //str+=that.setWordsArr(res.data).join("\n");
        let arr=res.data.split(",");
        if (arr.length>3){
          str+=arr.join("\n");
        }
        else
          str+=res.data;
        
        that.setData({
          Words: str,
          importWordStr:str,
        });

        IsEdit=true;
      },
      fail: function(err) {
        console.error('获取剪贴板失败', err);
      }
    });
  },
  bindKeyInput:function(e){
    let that=this;
    let words = e.detail.value;
    let currentTime = new Date().getTime();
    let timeDiff = currentTime - this.data.lastInputTime;

    if (timeDiff > this.data.inputThreshold && words.length>that.data.Words.length+2) {
        console.log("可能是粘贴操作");
        let str=that.data.importWordStr;
        let str2=words.substring(str.length);
        str+="\n"+str2;
        // let arr=that.setWordsArr(str);
        // words=arr.join("\n");
        word=str;
    } else {
        console.log("可能是键盘输入");
    }

    this.setData({
        Words: words,
        //importWordStr:words,
        //lastInputTime: currentTime
    });
    IsEdit=true;
  },
  onUnload:function(e){
    if (!IsEdit){
      app.globalData.SelectedWords=EntryWords;
    }
  },
  setWordsArr:function(data){
    let that=this;
    let words=[];
    words=data.split(/[\r\n]+/);
    if (words.length==0)
        words = data.split(" ");  // 按空格分割
    
    // 过滤掉空字符串和非单词内容
    words = common.filterWordsWithSpecialChars(words);
    words = common.removeDuplicateAndTrimStrings(words);
    //debugger;
    let arr2=[];
    if (words.length>10){
      for(let i=0;i<10;i++){
        arr2.push(words[i]);
      }
      words=arr2;
    }

    return words;
  },
  submit:function(){
    if (IsEdit){
      let words=this.setWordsArr(this.data.Words);
      app.globalData.SelectedWords=words;
    }
    wx.navigateBack({
      delta: 1
    });
  },
  clearInput:function(){
    app.globalData.SelectedWords=[];
    this.setData({
      Words:"", 
    });
    IsEdit=false;
  },
  catchTouchMove: main.catchTouchMove,
  onShareAppMessage: function () {
    return {
      title: app.globalData.ShareTitle,
      path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
      imageUrl: app.globalData.ShareImage,
    }
  },
  
})