import {defineStore} from 'pinia'
import pinia from "~src/store/init"
import appConfig from '~src/config/app';
import logoIcon from "~src/assets/logo/icon.png";
import logoName from "~src/assets/logo/name.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,
        logoIcon: logoIcon,
        logoName: logoName,
        //主题信息
        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') || '',
        userInfo: getStoreValue('userInfo') || {},
        //菜单信息
        menus: getStoreValue('menus') || [],
        buttons: getStoreValue('buttons') || {},
        homeUrl: getStoreValue('homeUrl') || 'home', //首页路由
        //项目合同段数据
        projectContract: getStoreValue('projectContract') || [],
        projectInfo: getStoreValue('projectInfo') || {},
        contractInfo: getStoreValue('contractInfo') || {},
        projectId: getStoreValue('projectId') || '',
        contractId: getStoreValue('contractId') || '',
        //其他配置信息
        bubble: getStoreValue('bubble') || false,
        isCollapse: getStoreValue('isCollapse') || false, //菜单折叠
    }),
    getters: {
        //系统信息
        getTitle: state => state.title,
        getLogoIcon: state => state.logoIcon,
        getLogoName: state => state.logoName,
        //主题信息
        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,
        getHomeUrl: state => state.homeUrl,
        //项目合同段数据
        getProjectContract: state => state.projectContract,
        getProjectInfo: state => state.projectInfo,
        getContractInfo: state => state.contractInfo,
        getProjectId: state => state.projectId,
        getContractId: state => state.contractId,
        //其他配置信息
        getBubble: state => state.bubble,
        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)
        },
        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;
        },
        setHomeUrl(value) {
            this.homeUrl = value
            setStoreValue('homeUrl', value)
        },
        //项目合同段数据
        setProjectContract(value) {
            this.projectContract = value
            setStoreValue('projectContract', value)
        },
        setProjectInfo(value) {
            this.projectInfo = value
            setStoreValue('projectInfo', value)
        },
        setContractInfo(value) {
            this.contractInfo = value
            setStoreValue('contractInfo', value)
        },
        setProjectId(value) {
            this.projectId = value
            setStoreValue('projectId', value)
        },
        setContractId(value) {
            this.contractId = value
            setStoreValue('contractId', value)
        },
        //其他配置信息
        setBubble(value) {
            this.bubble = value
            setStoreValue('bubble', value)
        },
        setCollapse(value) { //菜单折叠
            this.isCollapse = value
            setStoreValue('isCollapse', value)
        },
        //清除缓存和token
        clearStoreData() {
            //清除状态
            this.title = null
            this.logoIcon = null
            this.logoName = null
            this.token = null
            this.refreshToken = null
            this.tenantId = null
            this.userInfo = null
            this.menus = null
            this.buttons = null
            this.homeUrl = null
            this.projectContract = null
            this.projectInfo = null
            this.contractInfo = null
            this.projectId = null
            this.contractId = null
            this.isCollapse = false
            //清除缓存
            clearStoreAll()
            removeToken()
            removeRefreshToken()
        },
    }
})

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