import { Map, fromJS, List } from 'immutable'
// 所有的reducers函数在框架初始化时都会被框架自动调用一次，以初始化state数据。调用时action的值为{type:0.022444457651 },type是一个小于1的随机值
export function tempState(state = Map({
  isEdit: false,
  loading: false
}), action) {
  switch (action.type) {
    case 'setEdit':
      return state.sf(['isEdit'], action.data)
    case 'setLoading':
      return state.sf(['loading'], action.data)
    default:
      return state
  }
}

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

export function validateState(state = Map({
  code: { state: 'success', message: '' },          // 编码
  name: { state: 'success', message: '' },          // 名称
  sortNo: { state: 'success', message: '' },        // 顺序
  busTypeId: { state: 'success', message: '' },     // 业务类型
  url: { state: 'success', message: '' }            // 路径
}), action) {
  switch (action.type) {
    case 'update':
      return state.sfs(fromJS(action.data))
    default:
      return state
  }
}

const defaultForm = {
  parentName: null,   // 上级菜单
  functionType: 'menu', // 类型
  code: null,         // 编码
  name: null,         // 名称
  sortNo: null,       // 顺序
  busTypeId: null, // 业务类型
  url: null, //路径
  imageUrl: null, // icon
  isShow: 'Y', // 是否可见
  isValid: 'Y', //状态
  remark: null, // 备注
  accessAuthorityPaths: null, //允许访问接口
  applicationId: 0,
  target: '_top'
}
export function formObj(state = Map(fromJS(defaultForm)
), action) {
  switch (action.type) {
    case 'update':
      return state.sfs(fromJS(action.data))
    case 'reset':
      if (!action.data)
        return Map(fromJS(defaultForm))
      return Map(fromJS({ ...defaultForm, ...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
  })
}
