import {defineStore} from 'pinia'
import pinia from "~src/store/init"
import appConfig from '~src/config/app';
import {getStoreData, setStoreData, clearStoreAll} from '~src/utils/storage'
import {setToken, setRefreshToken, removeToken, removeRefreshToken} from '~src/api/util/auth'

export const useAppStore = defineStore('main', {
    state: () => ({
        //主题信息
        theme: getStoreData('theme') || appConfig.theme,    //用户可选择类型：auto,light, dark
        themeVal: getStoreData('themeVal') || '',           //实际主题：light, dark
        color: getStoreData('color') || appConfig.color,
        homeTheme: getStoreData('homeTheme') || appConfig.homeTheme,
        //用户信息
        token: getStoreData('token') || '',
        refreshToken: getStoreData('refreshToken') || '',
        tenantId: getStoreData('tenantId') || '',
        userInfo: getStoreData('userInfo') || {},
        //菜单信息
        menus: getStoreData('menus') || [],
        buttons: getStoreData('buttons') || {},
        //项目合同段数据
        projectContract: getStoreData('projectContract') || [],
        projectInfo: getStoreData('projectInfo') || {},
        contractInfo: getStoreData('contractInfo') || {},
        projectId: getStoreData('projectId') || '',
        contractId: getStoreData('contractId') || '',
        //其他配置信息
        bubble: getStoreData('bubble') || false,
        orderServiceTipModal: getStoreData('orderServiceTipModal') ?? 1, //0不弹出，1弹出
        shotWebRtc: getStoreData('shotWebRtc') || 0, //WebRtc截图方式: 0关闭，1开启
        fullScreen: getStoreData('fullScreen') || 0, //全屏截图：0关闭，1开启
        isCollapse: getStoreData('isCollapse') || false, //菜单折叠
        dragModalSortTop: [], //拖拽弹窗排序
        isScreenShort: false,
        barMenuName: '',
    }),
    getters: {
        //主题信息
        getTheme: state => state.theme,
        getThemeVal: state => state.themeVal,
        getColor: state => state.color,
        getHomeTheme: state => state.homeTheme,
        //用户信息
        getToken: state => state.token,
        getRefreshToken: state => state.refreshToken,
        getTenantId: state => state.tenantId,
        getUserInfo: state => state.userInfo,
        //菜单信息
        getMenus: state => state.menus,
        getButtons: state => state.buttons,
        //项目合同段数据
        getProjectContract: state => state.projectContract,
        getProjectInfo: state => state.projectInfo,
        getContractInfo: state => state.contractInfo,
        getProjectId: state => state.projectId,
        getContractId: state => state.contractId,
        //其他配置信息
        getBubble: state => state.bubble,
        getScreenShort: state => state.isScreenShort,
        getOrderServiceTipModal: state => state.orderServiceTipModal,
        getShotWebRtc: state => state.shotWebRtc,
        getFullScreen: state => state.fullScreen,
        getCollapse: state => state.isCollapse,
        getDragModalSortTop: state => state.dragModalSortTop,
    },
    actions: {
        //主题信息
        setTheme(value) {
            this.theme = value
            setStoreData('theme', value)
        },
        setThemeVal(value) {
            this.themeVal = value
            setStoreData('themeVal', value)
        },
        setColor(value) {
            this.color = value
            setStoreData('color', value)
        },
        setHomeTheme(value) {
            this.homeTheme = value
            setStoreData('homeTheme', value)
        },
        //用户信息
        setTokenVal(value) {
            this.token = value
            setToken(value)
            setStoreData('token', value)
        },
        setRefreshTokenVal(value) {
            this.refreshToken = value
            setRefreshToken(value)
            setStoreData('refreshToken', value)
        },
        setTenantId(value) {
            this.tenantId = value
            setStoreData('tenantId', value)
        },
        setUserInfo(value) {
            this.userInfo = value
            setStoreData('userInfo', value)
        },
        //菜单信息
        setMenus(value) {
            this.menus = value
            setStoreData('menus', value)
        },
        setButtons(value) {
            this.buttons = value
            setStoreData('buttons', value)
        },
        getButtonsVal(value) {
            return this.buttons[value] || false;
        },
        //项目合同段数据
        setProjectContract(value) {
            this.projectContract = value
            setStoreData('projectContract', value)
        },
        setProjectInfo(value) {
            this.projectInfo = value
            setStoreData('projectInfo', value)
        },
        setContractInfo(value) {
            this.contractInfo = value
            setStoreData('contractInfo', value)
        },
        setProjectId(value) {
            this.projectId = value
            setStoreData('projectId', value)
        },
        setContractId(value) {
            this.contractId = value
            setStoreData('contractId', value)
        },
        //其他配置信息
        setBubble(value) {
            this.bubble = value
            setStoreData('bubble', value)
        },
        setOrderServiceTipModal(value) {
            this.orderServiceTipModal = value
            setStoreData('orderServiceTipModal', value)
        },
        setScreenShort(value) {
            this.isScreenShort = value
        },
        setShotWebRtc(value) {
            this.shotWebRtc = value
            setStoreData('shotWebRtc', value)
        },
        setFullScreen(value) {
            this.fullScreen = value
            setStoreData('fullScreen', value)
        },
        setCollapse(value) { //菜单折叠
            this.isCollapse = value
            setStoreData('isCollapse', value)
        },
        setDragModalSortTop(value) {
            this.dragModalSortTop = value
        },
        //清除缓存和token
        clearStoreData() {
            //清除状态
            this.token = null
            this.refreshToken = null
            this.tenantId = null
            this.userInfo = null
            this.menus = null
            this.buttons = null
            this.projectContract = null
            this.projectInfo = null
            this.contractInfo = null
            this.projectId = null
            this.contractId = null
            this.orderServiceTipModal = null
            this.shotWebRtc = null
            this.fullScreen = null
            this.isCollapse = false
            this.dragModalSortTop = []
            //清除缓存
            clearStoreAll()
            removeToken()
            removeRefreshToken()
        },
    }
})

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