import { Map, fromJS, List } from 'immutable'
// 所有的reducers函数在框架初始化时都会被框架自动调用一次，以初始化state数据。调用时action的值为{type:0.022444457651 },type是一个小于1的随机值

export function searchForm(state = Map({
  currentPage: 1,
  pageSize: 10,
  isContainChild: "N"
}), action) {
  switch (action.type) {
    case 'init':
      state = fromJS({
        currentPage: 1,
        pageSize: 10,
        isContainChild: "N"
      })
      return state
    case 'update':
      return state.sfs(fromJS(action.data))
    default:
      return state
  }
}

export function tableData(state = List(), action) {
  switch (action.type) {
    case 'update':
      state = List(fromJS(action.data))
      return state
    default:
      return state
  }
}
export function pagination(state = Map(), action) {
  switch (action.type) {
    case 'update':
      state = fromJS(action.data)
      return state
    default:
      return state
  }
}

export function validateState(state = Map({
  loginName: { state: 'success', message: '' },
  userName: { state: 'success', message: '' },
  mobilePhone: { state: 'success', message: '' },
}), action) {
  switch (action.type) {
    case 'clear':
      return state.sfs(fromJS({
        loginName: { state: 'success', message: '' },
        userName: { state: 'success', message: '' },
        mobilePhone: { state: 'success', message: '' }
      }))
    case 'update':
      return state.sfs(fromJS(action.data))
    default:
      return state
  }
}

export function formObj(state = Map(), action) {
  switch (action.type) {
    case 'init':
      state = fromJS(action.data)
      return state
    case 'update':
      return state.sfs(fromJS(action.data))
    default:
      return state
  }
}

export function resetValidateState(state = Map({
  password: { state: 'success', message: '' },
  password2: { state: 'success', message: '' },
}), action) {
  switch (action.type) {
    case 'clear':
      return state.sfs(fromJS({
        password: { state: 'success', message: '' },
        password2: { state: 'success', message: '' }
      }))
    case 'update':
      return state.sfs(fromJS(action.data))
    default:
      return state
  }
}

export function resetFormObj(state = Map(), action) {
  switch (action.type) {
    case 'init':
      state = fromJS(action.data)
      return state
    case 'update':
      return state.sfs(fromJS(action.data))
    default:
      return state
  }
}

export function tempState(state = Map({
  showModal: false,
  showResetModal: false,
  isEdit: false,
  loading: false,
  editId: null
}), action) {
  switch (action.type) {
    case 'setModal':
      return state.sf(['showModal'], action.data)
      case 'setResetModal':
        return state.sf(['showResetModal'], action.data)
    case 'setLoading':
      return state.sf(['loading'], action.data)
    case 'setEditId':
      return state.sf(['editId'], action.data)
    default:
      return state
  }
}

export function selectKey(state = List([
  {id: "0"}
]), action) {
  switch (action.type) {
    case 'init':
      return state.set('0', action.data)
    default:
      return state
  }
}

export function busType(state = List(), action) {
  switch (action.type) {
    case 'init':
      return List(fromJS(JSON.parse(action.data)))
    default:
      return state
  }
}

export function treeData(state = List(), action) {
  let temp
  switch (action.type) {
    case 'update':
      return List(fromJS(action.data))
    case 'updateChildren':
      temp = updateChildren(state.toJS(), action.data)
      return List(fromJS(temp))
    case 'addChildren':
      return addChildren(state, action.data)
    default:
      return state
  }
}

function updateChildren(list, { functioinId, children, subNodeFlag, ...orther }) {
  return list.map(item => {
    if (item.functioinId === functioinId) {
      item = Object.assign(item, orther)
      if (children !== undefined)
        item.children = children
      if (subNodeFlag !== undefined)
        item.subNodeFlag = subNodeFlag
      return item
    } else {
      if (item && item.children) {
        let temp = item
        temp.children = updateChildren(item.children, { functioinId, children, subNodeFlag, ...orther })
        return temp
      } else {
        return item
      }
    }
  })
}

function addChildren(state, { parentId, ...orther }) {
  return state.map((value, key) => {
    let temp
    if (value.gf(['functioinId']) === parentId) {
      if (value.has('children')) {
        temp = value.update('children', children => children.push({ parentId, ...orther }))
      } else {
        temp = value.set('children', fromJS([{ parentId, ...orther }]))
      }
      temp = temp.set('subNodeFlag', '1')
      return temp
    } else {
      if (value.has('children')) {
        temp = value.update('children', children=>{
          // console.log('jjjjjjechil dren: ', t, '---------', children, '==>>>>', children.toJS())
          return addChildren(children, { parentId, ...orther })
        } )
      }else{
        temp = value
      }
    }
    return temp
  })
}

