import {connect} from 'react-redux';
import {EditableSpan} from '@commonComp';
import {SETCHECKTEXT,SETSEARCHDATA,CHECK_FOCUS_INDEX,CHECKBODY_CLEAR,DEL_CHECKBODY,DEL_CHECKBODY_LABLE} from '@types/checkBody';
import {SET_SEARCH,MAIN_FOCUS_INDEX,CLEAR_SEARCH,SETMAINTEXT,DEL_MAIN,REMOVE_MAIN_ID,DEL_MAIN_LABLE} from '@types/mainSuit';
import {SETOTHERTEXT,SETOTHERSEARCHDATA,OTHER_FOCUS_INDEX,OTHERHIS_CLEAR,DEL_OTHERHIS,REMOVE_OTHER_ID,DEL_OTHERHIS_LABLE} from '@types/otherHistory';
import {SET_CURRENT_SEARCH,CURRENT_CLEAR,SETCURRENTTEXT,CURRENT_FOCUS_INDEX,DEL_CURRENT,REMOVE_CURR_ID,DEL_CURRENT_LABLE} from '@types/currentIll';
import {getSearch} from '@store/async-actions/fetchModules.js';
import {billing} from '@store/async-actions/pushMessage';
import {didPushParamChange} from '@utils/tools.js';
import {SETTEXTFOCUS,SEARCH_DROP_LOCATION} from '@types/homePage';
import {RESET,SETDROPSHOW,ISREAD} from "@store/types/homePage";
import config from '@config/index.js';

function mapStateToProps(state){//console.log(state)
  return {
    mainSaveText:state.mainSuit.saveText,
    isRead:state.homePage.isRead,
  }
}

function mainSuitSearch(obj){
  return (dispatch)=>{
    const text = obj.text.replace(/\，/,'');
    getSearch({boxMark:1,inpStr:text,mainIds:obj.mainIds,itemType:0}).then((res)=>{
      if(res.data.code=='0'){
        dispatch({
          type:SET_SEARCH,
          data:res.data.data,
          isEnd:obj.isEnd,      //true为在末尾插入搜索结果否则在前面插入
          inpStr:text //搜索输入的值
        })
      }
    });
  }
}

function currentSearch(obj){
  return (dispatch)=>{
    const text = obj.text.replace(/\，/,'');
    getSearch({boxMark:1,inpStr:text,mainIds:obj.mainIds}).then((res)=>{
      if(res.data.code=='0'){
        dispatch({
          type:SET_CURRENT_SEARCH,
          data:res.data.data,
          isEnd:obj.isEnd,      //true为在末尾插入搜索结果否则在前面插入
          inpStr:text
        })
      }
    });
  }
}

function checkBodySearch(obj){
  return (dispatch)=>{
    const text = obj.text.replace(/\，/,'');
    getSearch({boxMark:4,inpStr:text}).then((res)=>{
      if(res.data.code=='0'){
        dispatch({
          type:SETSEARCHDATA,
          data:res.data.data,
          isEnd:obj.isEnd,      //true为在末尾插入搜索结果否则在前面插入
          inpStr:text
        });
      }
    });
  }
}

function otherHisSearch(obj) {
  return (dispatch)=>{
    const text = obj.text.replace(/\，/,'');
    getSearch({boxMark:3,inpStr:text}).then((res)=>{
      if(res.data.code=='0'){
        dispatch({
          type:SETOTHERSEARCHDATA,
          data:res.data.data,
          isEnd:obj.isEnd,      //true为在末尾插入搜索结果否则在前面插入
          inpStr:text
        });
      }
    });
  }
}

function mapDispatchToProps(dispatch,state) {
  const dis = JSON.stringify(state.saveText) == '[]'?0:60;
  return {
    setSearchLocation(top,left){
      dispatch({
        type:SEARCH_DROP_LOCATION,
        top:top,
        left:left,
        dis:dis
      })
    },
    setFocusIndex(obj){//将选中值插入相应位置

      const {i,boxMark} = obj;
      switch (+obj.boxMark){
        case 1:
          dispatch({
            type:MAIN_FOCUS_INDEX,
            index:i,
            $span:obj.dom
          });
          break;
        case 2:
          dispatch({
            type:CURRENT_FOCUS_INDEX,
            index:i,
            $span:obj.dom
          });
          break;
        case 3:
          dispatch({
            type:OTHER_FOCUS_INDEX,
            index:i,
            $span:obj.dom
          });
          break;
        case 4:
          dispatch({
            type:CHECK_FOCUS_INDEX,
            index:i,
            $span:obj.dom
          });
          break;
        default:
          dispatch({
            type:SETTEXTFOCUS,
            index:i
          });
          break;
        }

      //显示搜索框
      dispatch({
        type:SETDROPSHOW,
        data:{ikey:boxMark+i+'0'}
      });
      dispatch({
        type: RESET
      });
    },
    handleChange(obj){//存data值
      switch (+obj.boxMark){
        case 1:
          dispatch({
            type:SETMAINTEXT,
            text:obj.text1,
            i:obj.i
          });
          dispatch({
            type: RESET
          });
          break;
        case 2:
          dispatch({
            type:SETCURRENTTEXT,
            text:obj.text1,
            i:obj.i
          });
          break;
        case 3:
          dispatch({
            type:SETOTHERTEXT,
            text:obj.text1,
            i:obj.i
          });
          break;
        case 4:
          dispatch({
            type:SETCHECKTEXT,
            text:obj.text1,
            i:obj.i
          });
          break;
        default:
          break;
      }
      //右侧推送
      clearTimeout(timer);
      let timer = setTimeout(function(){      //延迟待确定后的数据更新后推送，避免获取的参数还是旧的
        if(didPushParamChange()){     //操作后内容有变化才推送
          dispatch(billing());
          clearTimeout(timer);
        }
      },config.delayPushTime);
    },
    handleSearch(obj){
      switch (+obj.boxMark){
        case 1:
          dispatch(mainSuitSearch(obj));
          break;
        case 2:
          dispatch(currentSearch(obj));
          break;
        case 3:
          dispatch(otherHisSearch(obj));
          break;
        case 4:
          dispatch(checkBodySearch(obj));
          break;
        default:
          break;
      }
    },
    handleClear(obj){//清空搜索结果
      switch (+obj.boxMark){
        case 1:
          dispatch({type:CLEAR_SEARCH});
          break;
        case 2:
          dispatch({type:CURRENT_CLEAR});
          break;
        case 3:
          dispatch({type:OTHERHIS_CLEAR});
          break;
        case 4:
          dispatch({type:CHECKBODY_CLEAR});
          break;
        default:
          break;
      }
    },
    handleKeydown(obj){//backspace删除
      if(obj.i==null){return}
      switch (+obj.boxMark){
        case 1:
          dispatch({type:DEL_MAIN,delIndex:obj.i,text:obj.text});
          break;
        case 2:
          dispatch({type:DEL_CURRENT,delIndex:obj.i,text:obj.text});
          break;
        case 3:
          dispatch({type:DEL_OTHERHIS,delIndex:obj.i});
          break;
        case 4:
          dispatch({type:DEL_CHECKBODY,delIndex:obj.i});
          break;
        default:
          break;
      }
      // 更新文本标签
      dispatch({
        type:ISREAD
      })
    },
    removeId(obj){//删除时移除id
      switch (+obj.boxMark){
        case 1:
          dispatch({type:REMOVE_MAIN_ID,index:obj.i,text:obj.text});
          break;
        case 2:
          dispatch({type:REMOVE_CURR_ID,index:obj.i,text:obj.text});
          break;
        case 3:
          dispatch({type:REMOVE_OTHER_ID,index:obj.i});//移除多余空标签
          break;
        default:
          break;
      }
    dispatch({
      type:ISREAD
    })
    },
    delSingleLable(obj){//光标在第一位时 只删除标签
      switch (+obj.boxMark){
        case 1:
          dispatch({type:DEL_MAIN_LABLE,index:obj.i,text:obj.text});
          break;
        case 2:
          dispatch({type:DEL_CURRENT_LABLE,index:obj.i});
          break;
        case 3:
          dispatch({type:DEL_OTHERHIS_LABLE,index:obj.i});
          break;
        case 4:
          dispatch({type:DEL_CHECKBODY_LABLE,index:obj.i});
          break;
        default:
          break;
      }
      dispatch({
        type:ISREAD
      })
    }
  }
}

const EditableSpanCont = connect(
  mapStateToProps,
  mapDispatchToProps
)(EditableSpan);

export default EditableSpanCont;