import {SET_ASSESS_DATA,SET_INDEX_DATA,SET_SAVE_ASSESS_DATA,SET_INDEX_SUB_TYPE,ADD_ASSESS_ITEMS,
  REMOVE_ASSESS_ITEMS,CLEAR_INDEX_DATA,SET_CHRONIC_CALCU_RESULT,ADD_WHOLE_SCALE_ITEMS,SET_CALCU_VALUES,CLEAR_ASSESS_RESULT_VALUE} from '@types/assessResult';
import config from "@config";

const init = {
  data:{},        //评估模块数据
  indexData:{},     //评估图表数据
  indexNames:{},
  indexTimeTypes:{},    //单个图表选中的维度
  wholeAssess:[],     //整体评估数据
  wholeIndexs:{},     //已加入的评估数据的index,加入按钮状态用
  wholeAssessText:{},   //整体评估补充说明
  chooseSelecteds:{},   //选项勾选状态
  wholeResults:{
    possible:{},
    radioVal:{}
  },    //可能结果
  calcuResult:{},       //计算公式结果
  calcuValues:{},       //计算公式填的值
  wholeScaleItems:{},   //全部量表中添加的整体评估项
  addedScaleIds:[]
};
export default (state=init,action)=>{
  const res = Object.assign({},state);
  const {data,range,index,rangeType,pindex,getNew} = action;
  switch (action.type){
    case SET_ASSESS_DATA:
      res.data[action.name] = action.data;
      res.update1 = Math.random();
      return res;
    case SET_INDEX_DATA:
      res.indexData[range[0]+range[1]] = data.obj;
      res.indexNames[range[0]+range[1]] = data.nameObj;
      Object.keys(data.obj||{}).map((i)=>{
        if(getNew){
          res.indexTimeTypes[pindex]=Object.assign({},res.indexTimeTypes[pindex],{[i]:config.chartDismen});
        }else{
          if(i===index){
            res.indexTimeTypes[pindex][i]=rangeType;
          }
        }
      });
      res.update = Math.random();   //对象更新
      return res;
    case CLEAR_INDEX_DATA:      //删除图表数据，因为图表数据为记录，需实时更新
      res.indexData = {};
      res.indexTimeTypes = {};
      return res;
    case CLEAR_ASSESS_RESULT_VALUE:
      res.data = {};
      res.indexTimeTypes={};
      res.indexData={};
      res.indexNames = {};
      res.addedScaleIds = [];
      res.calcuResult = {};
      res.calcuValues = {};
      res.wholeAssess = [];
      res.wholeIndexs = {};
      res.wholeResults = {
        possible:{},
        radioVal:{}
        };
      res.chooseSelecteds={};
      res.wholeAssessText ={};
      res.wholeScaleItems={};
      //res = deepClone(init)     //量表信息仍在，原因未明
      return res;
    case SET_INDEX_SUB_TYPE:
      if(!index){
        return res;
      }
      res.indexTimeTypes[index] = rangeType;
      res.update = Math.random();   //对象更新
      return res;
    case 'SAVE_POSSIBLE_RESULT':
      res.wholeResults = action.wholeResults||{};
      return res;
    case SET_SAVE_ASSESS_DATA:
      res.wholeIndexs = action.wholeAssessItems||{};
      res.addedScaleIds = action.addedScaleIds||[];
      res.wholeResults = action.wholeResults||{};
      res.wholeScaleItems = action.wholeScaleItems||{};
      res.wholeAssessText = action.wholeAssessText||{};
      res.chooseSelecteds = action.chooseSelecteds||{};
      res.calcuResult = action.calcuResult||{};
      res.calcuValues = action.calcuValues||{};
      return res;
    case ADD_ASSESS_ITEMS:
      res.wholeAssess[action.index] = action.data;
      let arr = res.wholeIndexs[action.index];
      res.wholeIndexs[action.index] = arr?[...arr,action.subIndex]:[action.subIndex];
      //已从全部量表中加入过，加入新的，删除原来的
      if(res.addedScaleIds.includes(action.id)){
        delete res.wholeScaleItems[action.id];
      }else{
        action.id?res.addedScaleIds.push(action.id):'';
      }
      res.update1 = Math.random();   //对象更新,与其他字段名不同因为绑定在pushItems,避免不必要的渲染
      return res;
    case ADD_WHOLE_SCALE_ITEMS:        //从全部量表中加入量表
      res.wholeScaleItems[action.id] = action.data;
      //已从指标推送中添加过该量表，加入新的，删除原来的
      let inx = -1,subInx = -1;
      if(res.addedScaleIds.includes(action.id)){
        const inx = res.wholeAssess.findIndex((it)=>{
          subInx = it.details.findIndex((i)=>{
            return i.type==1&&i.content.conceptId==action.id;
          });
          return subInx!=-1;
        });
        if(inx!=-1){
          const trueInx = inx!=-1&&res.wholeIndexs[inx].findIndex((i)=>i==subInx);
          res.wholeIndexs[inx].splice(trueInx,1);
        }
      }else{
        res.addedScaleIds.push(action.id);
      }
      res.update1 = Math.random();
      return res;
    case REMOVE_ASSESS_ITEMS:
      const {index,pIndex} = action;
      const temp =[...res.wholeIndexs[pIndex]];
      res.wholeIndexs[pIndex].splice(temp.findIndex((i)=>i==index),1);
      return res;
    case SET_CHRONIC_CALCU_RESULT:
      if(action.id){
        res.calcuResult[action.id] = action.result;
      }else{
        res.calcuResult = action.data;
      }
      res.update1= Math.random();
      return res;
    case SET_CALCU_VALUES:
      if(action.id){
        res.calcuValues[action.id] = action.data;
      }else{
        res.calcuValues = action.data;
      }
      return res;
    default:
      return res;
  }
}