import { defineStore } from 'pinia'
import pinia from '~src/store/init'
import website from '~src/config/index'
import logoIcon from '~src/assets/logo/icon.png'
import { clearStoreAll } from 'hc-vue3-ui'
import { getStoreValue, setStoreValue } from '~src/utils/storage'
import { removeRefreshToken, removeToken, setRefreshToken, setToken } from '~src/api/auth'

export const useAppStore = defineStore('main', {
    state: () => ({
        //系统信息
        title: getStoreValue('title') || website.title,
        logoIcon: getStoreValue('logoIcon') || logoIcon,
        logoName: getStoreValue('logoName') || website.name,
        //主题信息
        theme: getStoreValue('theme') || website.theme,
        color: getStoreValue('color') || website.color,
        //用户信息
        token: getStoreValue('token') || '',
        refreshToken: getStoreValue('refreshToken') || '',
        tenantId: getStoreValue('tenantId') || '',
        userInfo: getStoreValue('userInfo') || {},
        //菜单信息
        menus: getStoreValue('menus') || [],
        buttons: getStoreValue('buttons') || {},
        //其他配置信息
        isCollapse: getStoreValue('isCollapse') || false, //菜单折叠
    }),
    getters: {
        //系统信息
        getTitle: state => state.title,
        getLogoIcon: state => state.logoIcon,
        getLogoName: state => state.logoName,
        //主题信息
        getTheme: state => state.theme,
        getColor: state => state.color,
        //用户信息
        getToken: state => state.token,
        getRefreshToken: state => state.refreshToken,
        getTenantId: state => state.tenantId,
        getUserInfo: state => state.userInfo,
        //菜单信息
        getMenus: state => state.menus,
        getButtons: state => state.buttons,
        //其他配置信息
        getCollapse: state => state.isCollapse,
    },
    actions: {
        //系统信息
        setTitle(value) {
            this.title = value
            setStoreValue('title', value)
        },
        setLogoIcon(value) {
            this.logoIcon = value
            setStoreValue('logoIcon', value)
        },
        setLogoName(value) {
            this.logoName = value
            setStoreValue('logoName', value)
        },
        //主题信息
        setTheme(value) {
            this.theme = value
            setStoreValue('theme', value)
        },
        setColor(value) {
            this.color = value
            setStoreValue('color', value)
        },
        //用户信息
        setTokenVal(value) {
            this.token = value
            setToken(value)
            setStoreValue('token', value)
        },
        setRefreshTokenVal(value) {
            this.refreshToken = value
            setRefreshToken(value)
            setStoreValue('refreshToken', value)
        },
        setTenantId(value) {
            this.tenantId = value
            setStoreValue('tenantId', value)
        },
        setUserInfo(value) {
            this.userInfo = value
            setStoreValue('userInfo', value)
        },
        //菜单信息
        setMenus(value) {
            this.menus = value
            setStoreValue('menus', value)
        },
        setButtons(value) {
            this.buttons = value
            setStoreValue('buttons', value)
        },
        getButtonsVal(value) {
            return this.buttons[value] || false
        },
        //其他配置信息
        setCollapse(value) { //菜单折叠
            this.isCollapse = value
            setStoreValue('isCollapse', value)
        },
        //清除缓存和token
        clearStoreData() {
            this.title = null
            this.logoIcon = null
            this.logoName = null
            this.theme = website.theme
            this.color = website.color
            this.token = null
            this.refreshToken = null
            this.tenantId = null
            this.userInfo = null
            this.menus = null
            this.buttons = null
            this.isCollapse = false
            //清除缓存
            clearStoreAll()
            removeToken()
            removeRefreshToken()
        },
    },
})

export default function useUserStoreWidthOut() {
    return useAppStore(pinia)
}
