import Cookies from 'js-cookie'
import config from '@/config'


const { cookieExpires } = config

const STORAGE_PREFIX = 'mall'
const TOKEN_KEY = `${STORAGE_PREFIX}-token`
const ACCOUNT_INFO_KEY = `${STORAGE_PREFIX}-accountInfo`
const SEARCH_HISTORY_KEY = `${STORAGE_PREFIX}-searchHistory`
const ORDER_INFO_KEY = `${STORAGE_PREFIX}-orderInfo`
const WITHDRAW_GOODS_INFO_KEY = `${STORAGE_PREFIX}-pickUpGoodsInfo`
const REWARD_RULE_KEY = `${STORAGE_PREFIX}-rewardRule`
const SHOPPING_CART_CHECKED_LIST_KEY = `${STORAGE_PREFIX}-shopping-cart-checked-list`
const APP_VERSION_VALUE = `${STORAGE_PREFIX}-appversionvalue`


function setCookie(key, val) {
    Cookies.set(key, JSON.stringify(val), { expires: cookieExpires || 1 })
}

function getCookie(key) {
    let val = Cookies.get(key)
    if (val) { return JSON.parse(val) }
    return null
}

function clearCookie(key) {
    Cookies.remove(key)
}

function setLocalStorage(key, val) {
    localStorage.setItem(key, JSON.stringify(val))
}

function getLocalStorage(key) {
    let valStr = localStorage.getItem(key)
    if (valStr) {
        return JSON.parse(valStr)
    }
    return null
}

function clearLocalStorage(key) {
    localStorage.removeItem(key)
}

export function setToken(token) {
    setCookie(TOKEN_KEY, token)
}
export function setAccountInfo(accountInfo) {
    setCookie(ACCOUNT_INFO_KEY, accountInfo)
}
export function setSearchHistory(searchHistory) {
    setLocalStorage(SEARCH_HISTORY_KEY, searchHistory)
}
export function setOrderInfo(orderInfo) {
    setLocalStorage(ORDER_INFO_KEY, orderInfo)
}
export function setWithdrawGoodsInfo(pickUpGoodsInfo) {
    setLocalStorage(WITHDRAW_GOODS_INFO_KEY, pickUpGoodsInfo)
}
export function setRewardRule(rewardRule) {
    setLocalStorage(REWARD_RULE_KEY, rewardRule)
}
export function setShoppingCartCheckedList(list) {
    setLocalStorage(SHOPPING_CART_CHECKED_LIST_KEY, list)
}
export function setAppVersion(value) {
    setLocalStorage(APP_VERSION_VALUE, value)
}


export function getToken() {
    return getCookie(TOKEN_KEY) || ''
}
export function getAccountInfo() {
    return getCookie(ACCOUNT_INFO_KEY) || {}
}
export function getSearchHistory() {
    return getLocalStorage(SEARCH_HISTORY_KEY) || []
}
export function getOrderInfo() {
    return getLocalStorage(ORDER_INFO_KEY) || {}
}
export function getWithdrawGoodsInfo() {
    return getLocalStorage(WITHDRAW_GOODS_INFO_KEY) || {}
}
export function getRewardRule() {
    return getLocalStorage(REWARD_RULE_KEY) || {}
}
export function getShoppingCartCheckedList() {
    return getLocalStorage(SHOPPING_CART_CHECKED_LIST_KEY) || []
}
export function getAppVersion() {
    return getLocalStorage(APP_VERSION_VALUE) || ""
}


export function clearToken() {
    clearCookie(TOKEN_KEY)
}
export function clearAccountInfo() {
    clearCookie(ACCOUNT_INFO_KEY)
}
export function clearSearchHistory() {
    clearLocalStorage(SEARCH_HISTORY_KEY)
}
export function clearOrderInfo() {
    clearLocalStorage(ORDER_INFO_KEY)
}
export function clearWithdrawGoodsInfo() {
    clearLocalStorage(WITHDRAW_GOODS_INFO_KEY)
}
export function clearRewardRule() {
    clearLocalStorage(REWARD_RULE_KEY)
}
export function clearShoppingCartCheckedList() {
    clearLocalStorage(SHOPPING_CART_CHECKED_LIST_KEY)
}
