import * as api from '../api/client'

const LS_KEY_RESULTS = 'analysis_results'
const LS_KEY_SELECTED_SKU = 'analysis_selectedSku'

function loadFromLocalStorage() {
  try {
    const raw = localStorage.getItem(LS_KEY_RESULTS)
    const res = raw ? JSON.parse(raw) : {}
    const sel = localStorage.getItem(LS_KEY_SELECTED_SKU) || ''
    return { res, sel }
  } catch (e) {
    return { res: {}, sel: '' }
  }
}

function saveToLocalStorage(results, selectedSku) {
  try {
    localStorage.setItem(LS_KEY_RESULTS, JSON.stringify(results || {}))
    localStorage.setItem(LS_KEY_SELECTED_SKU, selectedSku || '')
  } catch (e) {}
}

function pickFirstSku(obj) {
  const keys = Object.keys(obj || {})
  const first = keys.find(k => k !== '_analysis_summary_')
  return first || ''
}

const state = () => {
  const cached = loadFromLocalStorage()
  return {
    results: cached.res,
    loading: false,
    error: null,
    selectedSku: cached.sel
  }
}

const mutations = {
  SET_RESULTS(state, payload) {
    state.results = payload || {}
  },
  SET_LOADING(state, payload) {
    state.loading = !!payload
  },
  SET_ERROR(state, payload) {
    state.error = payload || null
  },
  SET_SELECTED_SKU(state, payload) {
    state.selectedSku = payload || ''
  }
}

const actions = {
  async uploadFile({ commit }, file) {
    commit('SET_LOADING', true)
    commit('SET_ERROR', null)
    try {
      const res = await api.uploadFile(file)
      return res
    } catch (e) {
      commit('SET_ERROR', (e && e.message) || '上传失败')
      throw e
    } finally {
      commit('SET_LOADING', false)
    }
  },
  async analyzeFile({ commit }, file) {
    commit('SET_LOADING', true)
    commit('SET_ERROR', null)
    try {
      const response = await api.analyzeFile(file)
      const results = response && response.data ? response.data : {}
      commit('SET_RESULTS', results)
      const firstSku = pickFirstSku(results)
      commit('SET_SELECTED_SKU', firstSku || '')
      saveToLocalStorage(results, firstSku || '')
    } catch (e) {
      if (e && (e.code === 'ECONNABORTED' || (e.message && e.message.indexOf('timeout') >= 0))) {
        commit('SET_ERROR', '分析超时，数据量较大，请稍后重试或联系管理员')
      } else {
        const msg = (e && e.response && e.response.data && e.response.data.message) || (e && e.message) || '分析失败'
        commit('SET_ERROR', msg)
      }
      throw e
    } finally {
      commit('SET_LOADING', false)
    }
  },
  async analyzeProductLifecycle({ commit }, file) {
    commit('SET_LOADING', true)
    commit('SET_ERROR', null)
    try {
      const response = await api.analyzeProductLifecycle(file)
      return response
    } catch (e) {
      commit('SET_ERROR', (e && e.message) || '商品生命周期分析失败')
      throw e
    } finally {
      commit('SET_LOADING', false)
    }
  },
  async fetchResults({ commit, state }) {
    commit('SET_LOADING', true)
    commit('SET_ERROR', null)
    try {
      const results = await api.getResults()
      if (results && !results.message) {
        commit('SET_RESULTS', results)
        let selectedSku = state.selectedSku
        if (!selectedSku || !results[selectedSku]) {
          selectedSku = pickFirstSku(results)
          commit('SET_SELECTED_SKU', selectedSku)
        }
        saveToLocalStorage(results, selectedSku)
      }
    } catch (e) {
      commit('SET_ERROR', (e && e.message) || '获取结果失败')
    } finally {
      commit('SET_LOADING', false)
    }
  },
  selectSku({ commit, state }, sku) {
    const next = sku || ''
    commit('SET_SELECTED_SKU', next)
    saveToLocalStorage(state.results, next)
  },
  clearResults({ commit }) {
    commit('SET_RESULTS', {})
    commit('SET_SELECTED_SKU', '')
    commit('SET_ERROR', null)
    saveToLocalStorage({}, '')
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
