import {defineStore} from 'pinia'
import pinia from "~src/store/init"
import appConfig from '~src/config/app';
import webConfig from '~src/config/index';
import logopng from "~src/assets/logo/logo.png";
import {getStoreValue, setStoreValue, clearStoreAll} from '~src/utils/storage'
import {setToken, setRefreshToken, removeToken, removeRefreshToken} from '~src/api/util/auth'

export const useAppStore = defineStore('main', {
    state: () => ({
        //系统信息
        title: getStoreValue('title') || appConfig.title,
        appLogo: getStoreValue('appLogo') || logopng,
        //主题信息
        theme: getStoreValue('theme') || appConfig.theme,    //用户可选择类型：auto,light, dark
        themeVal: getStoreValue('themeVal') || '',           //实际主题：light, dark
        color: getStoreValue('color') || appConfig.color,
        //用户信息
        token: getStoreValue('token') || '',
        refreshToken: getStoreValue('refreshToken') || '',
        tenantId: getStoreValue('tenantId') ?? webConfig.tenantId,
        userInfo: getStoreValue('userInfo') || {},
        //菜单信息
        menus: getStoreValue('menus') || [],
        buttons: getStoreValue('buttons') || {},
        //其他配置信息
        isCollapse: getStoreValue('isCollapse') || false, //菜单折叠
        dragModalSortTop: [], //拖拽弹窗排序
        barMenuName: '', //当前菜单名称
    }),
    getters: {
        //系统信息
        getTitle: state => state.title,
        getAppLogo: state => state.appLogo,
        //主题信息
        getTheme: state => state.theme,
        getThemeVal: state => state.themeVal,
        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,
        getDragModalSortTop: state => state.dragModalSortTop,
    },
    actions: {
        //系统信息
        setTitle(value) {
            this.title = value
            setStoreValue('title', value)
        },
        setAppLogo(value) {
            this.appLogo = value
            setStoreValue('appLogo', value)
        },
        //主题信息
        setTheme(value) {
            this.theme = value
            setStoreValue('theme', value)
        },
        setThemeVal(value) {
            this.themeVal = value
            setStoreValue('themeVal', 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)
        },
        setDragModalSortTop(value) {
            this.dragModalSortTop = value
        },
        //清除缓存和token
        clearStoreData() {
            //清除状态
            this.title = null
            this.appLogo = logopng
            this.token = null
            this.refreshToken = null
            this.tenantId = null
            this.userInfo = null
            this.menus = null
            this.buttons = null
            this.isCollapse = false
            this.dragModalSortTop = []
            //清除缓存
            clearStoreAll()
            removeToken()
            removeRefreshToken()
        },
    }
})

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