import {
  RECEIVE_WORKFLOW,
  ADD_CONNECTION,
  ADD_NODE,
  MOVE_POSITION,
  DELETE_CONNECTION,
  DELETE_NODE,
  UPDATE_BASE_FIELD,
  UPDATE_LINE_FIELD,
  UPDATE_NODE_FIELD
  // DISABLED_ALLOW_REVOKE,
  // DISABLED_ALLOW_REBUT
} from '../mutation-types.js'

// initial state
const state = {
  id: '',
  name: '',
  notNeedApply: false,
  notifyNextStep: false,
  allowRebut: false,
  allowRevoke: false,
  allowReview: false,
  connectorStyle: 'Flowchart',
  topPositionFirst: 'Y',
  HDistance: 80,
  VDistance: 50,
  nodeWidth: 90,
  nodeHeight: 30,
  type: '',
  memo: '',
  nodes: [],
  disabledAllowRevoke: false,
  disabledAllowRebut: false,
  disabledAllowReview: false
}

// getters
const getters = {
  workflowType: state => state.type,
  workflowId: state => state.id,
  workflowNodes: state => state.nodes,
  workflowNode: state => nodeId => {
    return state.nodes.find(val => val.id === nodeId)
  },
  line: state => (sourceId, targetId) => {
    let sourceNode = state.nodes.find(val => val.id === sourceId)

    if (!sourceNode) {
      return null
    }

    return sourceNode.lines.find(val => val.target === targetId)
  },
  allLines: state => {
    let result = []

    for (let i = 0; i < state.nodes.length; i++) {
      const source = state.nodes[i].id

      if (!state.nodes[i].lines || !state.nodes[i].lines.length) {
        continue
      }

      for (let n = 0; n < state.nodes[i].lines.length; n++) {
        result.push({
          source,
          target: state.nodes[i].lines[n].target,
          action: state.nodes[i].lines[n].action,
          name: state.nodes[i].lines[n].name,
          script: state.nodes[i].lines[n].script
        })
      }
    }

    return result
  }
}

// actions
const actions = {
  getWorkflow ({ commit }, id) {
    // if (state.nodes.length !== 0) {
    //   return state
    // } else {
    return axios.get(`/ui/workflows/${id}`).then(res => {
      commit(RECEIVE_WORKFLOW, res.data.data)
    })
    // }
  },
  addConnection ({ commit }, { sourceId, targetId }) {
    commit(ADD_CONNECTION, {
      sourceId,
      targetId
    })
    return true
  },
  addNode ({ commit }, node) {
    let newNode = {
      postScript: '', // 后置脚本
      postAction: '', // 后置方法
      lines: []
    }

    if (node.type === 'ActionNode') {
      Object.assign(newNode, node, {
        allowSelectUser: false, // 是否允许指定下一步操作人
        allowAddActionMessage: false, // 是否允许添加动作消息
        allowRevoke: false, // 是否允许指定下一步操作人
        allowRebut: false // 是否允许添加动作消息
      })
    }

    if (node.type === 'Node') {
      Object.assign(newNode, node, {
        signJointly: false, // 是否会签模式
        organ: [], // 部门
        role: [], // 角色
        user: [], // 用户
        preScript: '', // 前置脚本
        preAction: '' // 前置方法
      })
    }

    commit(ADD_NODE, newNode)

    return true
  },
  movePosition ({ commit }, { id, left, top }) {
    commit(MOVE_POSITION, {
      id,
      left,
      top
    })
  },
  deleteConnection ({ commit, state }, { sourceId, targetId }) {
    const sourceNode = state.nodes.find(val => val.id === sourceId)
    if (!sourceNode) {
      return Promise.reject(new Error(false))
    }

    const lineIndex = sourceNode.lines.findIndex(val => val.target === targetId)

    if (lineIndex === -1) {
      return Promise.reject(new Error(false))
    }

    commit(DELETE_CONNECTION, {
      sourceId,
      targetId
    })

    return true
  },
  deleteNode ({ commit }, { id }) {
    commit(DELETE_NODE, {
      id
    })
  },
  updateBaseField ({ commit }, { fieldName, value }) {
    commit(UPDATE_BASE_FIELD, { fieldName, value })
  },
  updateLineField ({ commit }, { sourceId, targetId, fieldName, value }) {
    commit(UPDATE_LINE_FIELD, { sourceId, targetId, fieldName, value })
  },
  updateNodeField ({ commit }, { id, fieldName, value }) {
    commit(UPDATE_NODE_FIELD, { id, fieldName, value })
  }
}

// mutations
const mutations = {
  [RECEIVE_WORKFLOW] (state, workflow) {
    state.id = workflow.id
    state.name = workflow.name
    state.notNeedApply = workflow.notNeedApply
    state.notifyNextStep = workflow.notifyNextStep
    state.allowRebut = workflow.allowRebut
    state.allowRevoke = workflow.allowRevoke
    state.allowReview = workflow.allowReview
    state.connectorStyle = workflow.connectorStyle || 'Flowchart'
    state.topPositionFirst = workflow.topPositionFirst
    state.HDistance = workflow.HDistance
    state.VDistance = workflow.VDistance
    state.nodeWidth = workflow.nodeWidth
    state.nodeHeight = workflow.nodeHeight
    state.type = workflow.type
    state.memo = workflow.memo
    state.nodes = workflow.nodes

    state.disabledAllowRevoke = !workflow.allowRevoke
    state.disabledAllowRebut = !workflow.allowRebut
    state.disabledAllowReview = !workflow.allowReview
  },
  [ADD_CONNECTION] (state, { sourceId, targetId }) {
    const sourceNode = state.nodes.find(val => val.id === sourceId)

    sourceNode.lines.push({
      name: '', // 条件名称
      target: targetId, // 条件目标
      script: '', // 条件脚本
      action: '' // 条件方法
    })
  },
  [ADD_NODE] (state, node) {
    state.nodes.push(node)
  },
  [MOVE_POSITION] (state, { id, left, top }) {
    const node = state.nodes.find(val => val.id === id)

    node.top = top
    node.left = left
  },
  [DELETE_CONNECTION] (state, { sourceId, targetId }) {
    const sourceNode = state.nodes.find(val => val.id === sourceId)
    const lineIndex = sourceNode.lines.findIndex(val => val.target === targetId)

    sourceNode.lines.splice(lineIndex, 1)
  },
  [DELETE_NODE] (state, { id }) {
    const nodeIndex = state.nodes.findIndex(val => val.id === id)

    state.nodes.splice(nodeIndex, 1)
  },
  [UPDATE_BASE_FIELD] (state, { fieldName, value }) {
    state[fieldName] = value
    // 节点属性中‘允许撤销’和‘允许驳回’的禁用状态，随流程属性进行变更
    if (fieldName === 'allowRevoke' && value === false) {
      state.disabledAllowRevoke = true
    } else if (fieldName === 'allowRevoke' && value === true) {
      state.disabledAllowRevoke = false
    }
    if (fieldName === 'allowRebut' && value === false) {
      state.disabledAllowRebut = true
    } else if (fieldName === 'allowRebut' && value === true) {
      state.disabledAllowRebut = false
    }
    if (fieldName === 'allowReview' && value === false) {
      state.disabledAllowReview = true
    } else if (fieldName === 'allowReview' && value === true) {
      state.disabledAllowReview = false
    }
  },
  [UPDATE_LINE_FIELD] (state, { sourceId, targetId, fieldName, value }) {
    const sourceNode = state.nodes.find(val => val.id === sourceId)
    const line = sourceNode.lines.find(val => val.target === targetId)

    line[fieldName] = value
  },
  [UPDATE_NODE_FIELD] (state, { id, fieldName, value }) {
    const sourceNode = state.nodes.find(val => val.id === id)
    if (sourceNode.type === 'ActionNode' && !sourceNode[fieldName]) {
      let addNode = Object.assign(sourceNode, { fieldName: '' })
      addNode[fieldName] = value
      const nodeIndex = state.nodes.findIndex(val => val.id === id)
      state.nodes.splice(nodeIndex, 1)
      state.nodes.push(addNode)
    } else {
      sourceNode[fieldName] = value
    }
  }
}

export default {
  state,
  getters,
  actions,
  mutations
}
