import constants from "@/utils/constants";
import Api from '@/api'
import { getSourceName } from "@/utils/chart";

export default {
  state: {
    patentKey: null,
    matrix: {},
    count: {},
    myChart: [],
    form: {},
    treeList: [],
    reloadData: true,
    changeNum: 0,
    selected: {
      x: [],
      y: [],
    },
    treeKey: {
      x: [],
      y: [],
    },
    selectedLoad: {
      x: false,
      y: false
    }
  },

  mutations: {
    SET_CHANGE_NUM: (state, changeNum) => {
      state.changeNum = new Date().getTime()
    },
    SET_PATENT_KEY: (state, patentKey) => {
      state.patentKey = patentKey
    },
    SET_RELOAD_DATA: (state, reloadData) => {
      state.reloadData = reloadData
    },
    SET_CHART_FORM: (state, form) => {
      state.form = form
    },
    SET_CHART_TREE: (state, treeList) => {
      state.treeList = treeList
    },
    SET_CHART_SELECTED: (state, selected) => {
      state.selected = selected
    },
    SET_CHART_SELECTED_LOAD: (state, selectedLoad) => {
      state.selectedLoad = selectedLoad
    },
    SET_DATA_COUNT: (state, count) => {
      state.count = count
    },
    SET_TREE_KEY: (state, treeKey) => {
      state.treeKey = treeKey
    },
    SET_DATA_MATRIX: (state, matrix) => {
      state.matrix = matrix
    },
    SET_MY_CHART: (state, myChart) => {
      if (myChart) {
        state.myChart.push(myChart)
      } else {
        state.myChart = []
      }
    },
  },

  actions: {
    async getSourceDataList({ commit, state, getters, dispatch }, dimension) {
      let form = { ...state.form }
      state.selectedLoad[dimension] = true
      if (form.schema[dimension].ptype !== 0) {
        let params = {
          uid: form.uid || '',
          patentKey: state.patentKey,
          dimension: dimension,
          expandId: form.schema[dimension].expand || 0,
          fieldId: form.schema[dimension].field,
          type: form.schema[dimension].type,
          ptype: form.schema[dimension].ptype,
          num: form.schema[dimension].num,
          projectId: getters.projectId
        }
        try {
          const response = await Api.getCustomItemSourceDataList(params)
          form.source[dimension] = response.data
          switch (dimension) {
            case 'x':
              form.setting.title1 = getSourceName(form.schema.x.field, form.schema.x.expand, form.schema.x.num)
              break
            case 'y':
              form.setting.title2 = getSourceName(form.schema.y.field, form.schema.y.expand, form.schema.x.num)
              break
          }
          dispatch('getItemSettingColor', [])
          commit('SET_RELOAD_DATA', true)
          state.selectedLoad[dimension] = false
        } catch (e) {
          form.source[dimension] = []
          state.selectedLoad[dimension] = false
        }
      } else {
        form.source[dimension] = []
        state.selectedLoad[dimension] = false
      }
      state.treeKey[dimension] = []
      if (form.schema[dimension].type === 6) {
        state.selected[dimension] = []
      }
      commit('SET_CHART_FORM', form)
    },
    getSelected({ commit, state, getters }, schema = ['x', 'y']) {
      let selected = state.selected
      schema.map(c => {
        selected[c] = state.form.source[c].filter(item => item.selected).map(item => item.name)
      })
      commit('SET_CHART_SELECTED', selected)
      return selected
    },
    resetSelected({ commit, state, getters }, schema = ['x', 'y']) {
      let selected = state.selected
      schema.map(c => {
        selected[c] = []
      })
      commit('SET_CHART_SELECTED', selected)
    },
    resetSettingColor({ commit, state }) {
      state.form.setting.config.color = []
    },
    getItemSettingColor({ commit, state }, data) {
      switch (state.form.setting.type) {
        case 1:
        case 3:
        case 4:
        case 7:
        case 15:
          initColorData(state)
          break
        case 10:
        case 11:
        case 13:
          initColorData(state, 'x')
          break
        case 2:
        case 5:
        case 6:
        case 8:
        case 9:
        case 12:
        case 19:
          initColorData(state, 'y')
          break
        case 17:
        case 22:
          if (state.form.schema.y.field) {
            initColorData(state, 'y')
          } else {
            initColorData(state)
          }
          break
        case 14:
          state.form.setting.config.color = [{
            name: '低数值',
            color: '#F0D99C'
          }, {
            name: '高数值',
            color: '#BF444C'
          }]
          break
        default:
          state.form.setting.config.color = []
      }
    }
  }
}

const initColorData = (state, dimension) => {
  const defaultColor = [
    '#5470c6',
    '#91cc75',
    '#fac858',
    '#ee6666',
    '#73c0de',
    '#3ba272',
    '#fc8452',
    '#9a60b4',
    '#ea7ccc',
  ]
  let index = 0
  if (dimension) {
    state.form.setting.config.color = state.selected[dimension].map(item => {
      if (index >= defaultColor.length) {
        index = 0
      }
      return {
        name: item,
        color: defaultColor[index++]
      }
    })
  } else {
    if (state.form.setting.config.color && state.form.setting.config.color.length !== 0) {

    } else {
      state.form.setting.config.color = [{
        name: '主题颜色',
        color: defaultColor[index]
      }]
    }
  }
}
