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

const app = getApp();
let isFocus=true;

Page({
  behaviors: [commonBehavior],
  data: {
    Words:[],
    IsShowAlert:false,
    IsShowSetPanel:false,
    IsShowFirstOpen:false,
    IsShowExample:false,
    IsShowRemind:false,
    IsShowGuideContainer:false,
    IsShowGuideRemind:false,
  },
  onLoad: function (options) {
    let that = this;
    const IsShowFirstOpen=wx.getStorageSync('IsShowFirstOpen');
    const IsShowGuideContainer=wx.getStorageSync('IsShowGuideContainer');
    if (!IsShowGuideContainer && !IsShowFirstOpen){
      that.setData({
        IsShowFirstOpen:1,
      });
    }
    else{
      const IsShowGuideRemind=wx.getStorageSync('IsShowGuideRemind');
    
      if (!IsShowGuideRemind){
        that.setData({
          IsShowGuideRemind:1,
        });
      }
    }
    that.setData({
      Containnerheight: main.getWindowHeight(),
      KeyboardBtnName:"next",
    });
    that.initMenu();

    main.checkGenerating();

    if (options.goto=="selectword"){
      wx.navigateTo({
        url: options.goto,
      })
    }
  },
  onShow:function(e){
    let that = this;
    that.initWords();
    that.isShowAlert();
    that.initGuide(null);
  },
  onHide:function(e){
    this.getInputData();
  },
  
  initWords:function(){
    let that = this;
    app.globalData.SelectedWords=common.removeDuplicateAndTrimStrings(app.globalData.SelectedWords);
    that.data.Words=[];
    for(let i=0;i<10;i++){
      let obj={};
      obj.ID=i+1;
      if (app.globalData.SelectedWords[i]){
        obj.Word=app.globalData.SelectedWords[i];
        obj.CSS="txtWordFinished";
        if (obj.Word && !that.isValidInput(obj.Word)){
          obj.IsError=true;
        }
        else
          obj.IsError=false;
      }
      else{
        obj.Word="";
        obj.CSS="";
        obj.IsError=false;
      }
      that.data.Words.push(obj);
    }
    //console.log(app.globalData.SelectedWords);
    that.setData({
      Words:that.data.Words,
    });
  },
  initGuide:function(e){
    let that=this;
    if (e && e.currentTarget.dataset.isstart){
      wx.setStorageSync('IsShowGuideContainer', true);
      //wx.setStorageSync('IsShowGuideRemind', 2);
      wx.navigateBack({
        delta: 1,
      });
    }
    else if (wx.getStorageSync("IsShowGuideContainer")){
      that.setData({
        IsShowGuideContainer:true,
        IsShowGuideRemind:false,
      });
      app.globalData.SelectedWords=["penguin","asia","mammal","panda","","fish","bird","dolphin","",""];
      that.initWords();
      main.showGuideContainer(that,"#txtWord5",134,-238,"pic_ha02",0,2);
    }
  },
  initMenu:function(){
    let that = this;
    let level=app.globalData.GenerateConfig.Level;
    level[0].CSS="Selected";
    for(let i=1;i<level.length;i++){
      level[i].CSS="";
    }
    let articleStyle=app.globalData.GenerateConfig.ArticleStyle;
    articleStyle[0].CSS="Selected";
    for(let i=1;i<articleStyle.length;i++){
      articleStyle[i].CSS="";
    }
    that.setData({
      GradeArr:level,
      ArticleStyleArr:articleStyle,
      AIVersionArr:app.globalData.GenerateConfig.AIVersion,
    });  
  },
  bindKeyInput: function (e) {
    let that=this;
    let id=e.currentTarget.dataset.id;
    let word=e.detail.value;
    const inputWord="smart";
    if (wx.getStorageSync("IsShowGuideContainer") && word.toString().toLowerCase()==inputWord){
      that.data.Words[4].Focus=false;
      that.data.Words[4].Word=inputWord;
      that.setData({
        Words:that.data.Words,
      });
      main.showGuideContainer(that,"#btnNext",37,-130,"pic_ha03",0,2);
    }
    else if (that.data.IsShowGuideContainer){
      return;
    }
      
    for(let i=0;i<10;i++){
      if (i+1==id){
        that.data.Words[i].Word=word;
        that.data.Words[i].CSS="txtWordFinished";
        
        if (word && !that.isValidInput(word)){
          that.data.Words[i].IsError=true;
        }
        else{
          that.data.Words[i].IsError=false;
          if (!word)
            that.data.Words[i].CSS="";
        }
        break;
      }
    }
    
    that.setData({
      Words:that.data.Words, 
    });
    that.isShowAlert();
  },
  isShowAlert:function(){
    let that=this;
    let b=false;
    for(let i=0;i<10;i++){
      if (that.data.Words[i].IsError){
        b=true;
        break;
      }
    }
    that.setData({
      IsShowAlert:b,
      AlertContent:"请勿使用数字、符号、句子等非英语单词内容"
    });
  },
  setArticleParam:function(e){
    let that=this;
    let count=0;
    app.globalData.SelectedWords=[];
    for(let i=0;i<10;i++){
      if (that.data.Words[i].CSS=="txtWordFinished"){
        app.globalData.SelectedWords.push(that.data.Words[i].Word);
        count++
      }
    }
    if (count<5){
      that.setData({
        IsShowAlert:true,
        AlertContent:"请输入至少5个英语单词或词组"
      });
    }
    else{
      that.checkMsgSec(function(result){
        if (result){
          that.setData({
            IsShowAlert:false,
            IsShowSetPanel:true,
          });

          if (wx.getStorageSync("IsShowGuideContainer")){
            
            main.showGuideContainer(that,"#btnLevel1",150,-50,"pic_ha04",135,167);
          }
        }
      });
    }
  },
  //敏感词判断
  checkMsgSec:function(callback){
    let that=this;
    var content = app.globalData.SelectedWords.join(",");
    main.postData("MsgSecCheck2", {
      Content: content,
      ProgramID:app.globalData.ProgramID,
      UserID:app.globalData.userInfo.UserID,
    },
    function (data) {
      if (data && data.errcode == 0) {
        callback(true);
      } else {
        if (data.errmsg) {
          wx.showToast({
            title: data.errmsg,
            duration: 2000,
            image: "../images/sysIcon_b16.png",
          });
        }
        callback(false);
      }
    });
  },
  keyboardOK:function(e){
    if (wx.getStorageSync('IsShowGuideContainer')){
      return ;
    }
    let that=this;
    let id=e.currentTarget.dataset.id;
    id++;
    that.setFocus({currentTarget:{dataset:{id:id}}});
  },
  setFocus:function(e){
    let that=this;
    let id=e.currentTarget.dataset.id;
    
    for(let i=0;i<that.data.Words.length;i++){
      that.data.Words[i].Focus=false;
      if (that.data.Words[i].ID==id)
        that.data.Words[i].Focus=true;
    }
    that.setData({
      Words:that.data.Words,
    });
  },
  closeIsShowFirstOpen:function(){
    this.setData({
      IsShowFirstOpen:false,
      IsShowGuideRemind:true,
    });
    wx.setStorageSync('IsShowFirstOpen', 2);
  },
  selectBtn:function(e){
    let that=this;
    const index=e.currentTarget.dataset.index;
    const id=e.currentTarget.dataset.id;
    let arr=this.data.GradeArr;
    if (id==1)
      arr=this.data.ArticleStyleArr;
    else if (id==2)
      arr=this.data.AIVersionArr;
      
    for(let i=0;i<arr.length;i++){
      arr[i].CSS="";
      if (i==index)
        arr[i].CSS="Selected";
    }
    if (id==1){
      this.setData({
        ArticleStyleArr:arr,
      });
      wx.setStorageSync('ArticleStyle', index);
      if (wx.getStorageSync("IsShowGuideContainer")){
        main.showGuideContainer(that,"#btnBuild",20,-128,"pic_ha06",240,0);
      }
    }
    else if (id==2){
      this.setData({
        AIVersionArr:arr,
      });
      wx.setStorageSync('AIVersion', index);
    }
    else{
      this.setData({
        GradeArr:arr,
      });
      wx.setStorageSync('Grade', index);
      if (wx.getStorageSync("IsShowGuideContainer")){
        main.showGuideContainer(that,"#btnArticleStyle1",75,-12,"pic_ha05",100,145);
      }
    }
    
  },
  isValidInput:function(input) {
      // 正则表达式匹配：大小写字母、空格、单引号、减号
      const regex = /^[a-zA-Z\s'-]+$/;
      return regex.test(input);
  },
  goto: function (e) {
    let that=this;
    var url=e.currentTarget.dataset.url;

    that.getInputData();

    if (url=="article" && app.globalData.Generating){
      animation.toggleRemindWithAnimation(that, {
        remindKey:'IsShowRemindWait',
        showAnimation: 'remind-slide-up'
      });
      return ;
    }
    else if (url=="article"){
      let arr=this.data.GradeArr;
      for(let i=0;i<arr.length;i++){
        if (arr[i].CSS=="Selected"){
          url+="?Level="+i;
          break;
        }
      }
      arr=this.data.ArticleStyleArr;
      for(let i=0;i<arr.length;i++){
        if (arr[i].CSS=="Selected"){
          url+="&ArticleStyle="+arr[i].Name;
          break;
        }
      }
      arr=this.data.AIVersionArr;
      for(let i=0;i<arr.length;i++){
        if (arr[i].CSS=="Selected"){
          url+="&AIVersion="+arr[i].Version+"&BuildSecond="+arr[i].BuildSecond;
          break;
        }
      }
      if (wx.getStorageSync("IsShowGuideContainer")){
        wx.navigateTo({
          url: "article?ID=1&Type=Guide",
        });
        return;
      }
    }

    
    if (url=="../main/ocr" || url=="../main/selectword"){
      if ( app.globalData.SelectedWords.length>=10){
        that.showRemind();
        return;
      }
      if (app.globalData.OCRWords.length>0){
        url="../main/selectword";
      }
      else{
        url="../main/ocr"
      }
    }
    wx.navigateTo({
      url: url,
    });
  },
  getInputData:function(){
    let that=this;
    app.globalData.SelectedWords=[];
    for(let i=0;i<that.data.Words.length;i++){
      if (that.data.Words[i].Word)
        app.globalData.SelectedWords.push(that.data.Words[i].Word);
    }
    app.globalData.SelectedWords=common.removeDuplicateAndTrimStrings(app.globalData.SelectedWords);
    
  },
  clearInput:function(e){
    let that=this;
    const id=e.currentTarget.dataset.id;
    if (id=="0"){
      app.globalData.SelectedWords=[];
      for(let i=0;i<that.data.Words.length;i++){
        let obj=that.data.Words[i];
        obj.Word="";
        obj.CSS="";
        obj.IsError=false;
      }
      that.setData({
        Words:that.data.Words, 
      });
    }
    else{
      for(let i=0;i<10;i++){
        if (i+1==id){
          that.data.Words[i].Word="";
          that.data.Words[i].CSS="";
          that.data.Words[i].IsError=false;
          app.globalData.SelectedWords.splice(i,1);
          break;
        }
      }
      that.setData({
        Words:that.data.Words, 
      });
      that.isShowAlert();
    }
  },
  showRemind:function(e){
    let type1="IsShowRemind"
    if (e.currentTarget.dataset.type)
      type1=e.currentTarget.dataset.type;
    animation.toggleRemindWithAnimation(this, {
      remindKey:type1,
      showAnimation: 'remind-slide-down'
    });
  },
  catchTouchMove: main.catchTouchMove,
  onShareAppMessage: function () {
    return {
      title: app.globalData.ShareTitle,
      path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
      imageUrl: app.globalData.ShareImage,
    }
  },
})