import storage from 'store'
import {
  SIDEBAR_TYPE,
  TOGGLE_MOBILE_TYPE,
  TOGGLE_NAV_THEME,
  TOGGLE_LAYOUT,
  TOGGLE_FIXED_HEADER,
  TOGGLE_FIXED_SIDEBAR,
  TOGGLE_CONTENT_WIDTH,
  TOGGLE_HIDE_HEADER,
  TOGGLE_PRIMARY_COLOR,
  TOGGLE_HEADER_STYLE,
  TOGGLE_WEAK,
  TOGGLE_MULTI_TAB,
  // i18n
  TOGGLE_LANAGUAGE,
  TOGGLE_ZOOM

} from '@/store/mutation-types'
import { loadLanguageAsync, loadLanguageFormMem } from '@/locales'
import defaultSettings from '@/config/defaultSettings';
const { fixedHeader,multiTab,primaryColor,headerStyle,lang,zoom}= defaultSettings

const app = {
  state: {
    sideCollapsed: false,
    isMobile: false,
    theme: 'dark',
    layout: '',
    contentWidth: '',
    fixedHeader,
    fixedSidebar: false,
    autoHideHeader: false,
    primaryColor,
    headerStyle,
    weak: false,
    multiTab,
    lang,
    zoom,
    _antLocale: {},
    langData: null,
    unAuthorization: false,
    expiresTimeStamp:'',
    licenseType:'',
  },
  mutations: {
    SET_UN_AUTHORIZATION:(state,payload)=>{
      state.unAuthorization = payload || false
    },
    SET_EXPIRES_TIME_STAMP:(state,payload)=>{
      state.expiresTimeStamp = payload || ''
    },
    SET_LICENSE_TYPE:(state,payload)=>{
      state.licenseType = payload || ''
    },
    [SIDEBAR_TYPE]: (state, type) => {
      state.sideCollapsed = type
      storage.set(SIDEBAR_TYPE, type)
    },
    [TOGGLE_MOBILE_TYPE]: (state, isMobile) => {
      state.isMobile = isMobile
    },
    [TOGGLE_NAV_THEME]: (state, theme) => {
      state.theme = theme
      storage.set(TOGGLE_NAV_THEME, theme)
    },
    [TOGGLE_LAYOUT]: (state, mode) => {
      state.layout = mode
      storage.set(TOGGLE_LAYOUT, mode)
    },
    [TOGGLE_FIXED_HEADER]: (state, mode) => {
      state.fixedHeader = mode
      storage.set(TOGGLE_FIXED_HEADER, mode)
    },
    [TOGGLE_FIXED_SIDEBAR]: (state, mode) => {
      state.fixedSidebar = mode
      storage.set(TOGGLE_FIXED_SIDEBAR, mode)
    },
    [TOGGLE_CONTENT_WIDTH]: (state, type) => {
      state.contentWidth = type
      storage.set(TOGGLE_CONTENT_WIDTH, type)
    },
    [TOGGLE_HIDE_HEADER]: (state, type) => {
      state.autoHideHeader = type
      storage.set(TOGGLE_HIDE_HEADER, type)
    },
    [TOGGLE_PRIMARY_COLOR]: (state, color) => {
      state.primaryColor = color
      storage.set(TOGGLE_PRIMARY_COLOR, color)
    },
    [TOGGLE_HEADER_STYLE]:(state, type) => {
      state.headerStyle = type
      storage.set(TOGGLE_HEADER_STYLE, type)
    },
    [TOGGLE_WEAK]: (state, mode) => {
      state.weak = mode
      storage.set(TOGGLE_WEAK, mode)
    },
    [TOGGLE_LANAGUAGE]: (state, lang, antd = {}) => {
      state.lang = lang
      state._antLocale = antd
      storage.set(TOGGLE_LANAGUAGE, lang)
    },
    [TOGGLE_MULTI_TAB]: (state, bool) => {
      state.multiTab = bool
      storage.set(TOGGLE_MULTI_TAB, bool)
    },
    SET_LANG_DATA: (state, data) => {
      state.langData = data
    },
    [TOGGLE_ZOOM]: (state, data) => {
      state.zoom = data
      storage.set(TOGGLE_ZOOM, data)

      document.body.style.zoom = state.zoom

    }
  },
  actions: {
    setLang({ commit }, lang) {
      commit(TOGGLE_LANAGUAGE, lang)
      loadLanguageAsync(lang)
    },
    setLangFromMem({ commit }, lang) {
      commit(TOGGLE_LANAGUAGE, lang)
      loadLanguageFormMem(lang)
    }
  }
}

export default app
