import { Map, List, fromJS } from 'immutable'
import {fetch} from '@ttk/utils'
import moment from 'moment'

export function dateRange(state = Map({
  SystemDate: moment(),
  res: null,
  EnableDate: moment(),
  loginInfo: sessionStorage['userInfo'] ? JSON.parse(sessionStorage['userInfo']) : null
}), action) {
  state = state.sfs(fromJS(action))
  return state
}

export function functionNav(state = List(), action) {
  switch (action.type) {
    case "concatItem":
      return state.concat(action.data)
    case 'clearItem':
      return List()
    default:
      return state
  }
}

export function iframeRoutes(state = List(
    sessionStorage['iframeRoutes'] ? JSON.parse(sessionStorage['iframeRoutes']) : []
  ), action) {
  switch (action.type) {
    case "addItem":
      let existItem = state.some((item) => {
        return action.data.url === item.url
      })
      if (!existItem) {
        state = state.concat(action.data)
      }
      // 进行session缓存，避免页面刷新
      sessionStorage['iframeRoutes'] = JSON.stringify(state.toJS())
      return state
    case "removeItem":
      state = state.filter((item) => {
        return action.data.url !== item.url
      })
      // 进行session缓存，避免页面刷新
      sessionStorage['iframeRoutes'] = JSON.stringify(state.toJS())
      return state
    case "clearItem":
      sessionStorage['iframeRoutes'] = JSON.stringify([])
      return List()
    default:
      return state
  }
}

const { setAccessToken, getAccessToken } = fetch
export function loginInfo(state = Map({
  token: getAccessToken(),
  depId: sessionStorage['depId']
}), action) {
  if (action && (action.token || action.token === '')) {
    state = state.sfs(fromJS(action))
    setAccessToken(action.token)
    sessionStorage['depId'] = action.depId
    sessionStorage['userInfo'] = JSON.stringify(action)
  }
  return state
}

export function tags(state = Map(fromJS({
  currentTag: {},
  list: []
})), action) {
  let tag = null
  if (action.data) {
    tag = state.getIn(['list']).find((tag, index, array) => {
      return tag.url === action.data.url
    })
  }
  let list = state.getIn(['list'])
  switch (action.type) {
    case 'addTag':
      const temp = list.find((item, index, array) => {
        return item.url === action.data.url
      })
      if (temp) {
        // 如果tab已经存在
        // return state.setIn(['currentTag'], fromJS(action.data))
        return state
      } else {
        return state.setIn(['list'], list.push(action.data))
        // // 否则添加tab并设置当前tab
        // return Map(fromJS({ currentTag: action.data, list: list.push(action.data) }))
      }
    case "removeTag":
      // const index = state.getIn(['list']).find((tag, index, array)=>{
      //   return tag.url = action.data.url
      // })
      const tags = state.getIn(['list']).filter((item, index, array) => {
        return item.url !== action.data.url
      })
      // console.log('3333311', tags, state.setIn(['list'], tags), state.get(['list']))
      const t = state.setIn(['list'], tags)
      // console.log('jjjjj3', t.getIn(['currentTag']).toJS())

      return t// state.setIn(['list'], tags)
    case "removeLeftTags":
      let currentIndex1 = state.getIn(['list']).findIndex((item) => {
        return item.url === tag.url
      })
      let rightTags = state.getIn(['list']).filter((item, index, array) => {
        return currentIndex1 <= index
      })
      return state.setIn(['list'], rightTags)
    case "removeRightTags":
      let currentIndex2 = state.getIn(['list']).findIndex((item) => {
        return item.url === tag.url
      })
      let leftTags = state.getIn(['list']).filter((item, index, array) => {
        return currentIndex2 >= index
      })
      return state.setIn(['list'], leftTags)
    case "removeOtherTags":
      list = new List()
      return state.setIn(['list'], list.push(tag))
    case "removeAllTags":
      list = new List()
      return state.setIn(['list'], list)
    case "setCurrentTag":
      return state.setIn(['currentTag'], fromJS(tag))
    default:
      return state
  }
}

export function tempState(state = Map(fromJS({
  openKeys: [],
  selectedKeys: []
})), action) {
  switch(action.type){
    case 'setOpenKeys':
      return state.sf(['openKeys'], action.data)
    case 'setSelectedKeys':
      return state.sf(['selectedKeys'], action.data)
    default:
    return state
  }
}