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 { clearStoreAll, getStoreValue, setStoreValue } from '~src/utils/storage'
import { removeRefreshToken, removeToken, setRefreshToken, setToken } from '~src/api/util/auth'

export const useAppStore = defineStore('main', {
    state: () => ({
        //系统信息
        title: getStoreValue('title') || appConfig.title,
        logoIcon: getStoreValue('logoIcon') || logoIcon,
        logoName: getStoreValue('logoName') || logoName,
        //主题信息
        theme: getStoreValue('theme') || appConfig.theme, //用户可选择类型：auto,light, dark
        themeVal: getStoreValue('themeVal') || '', //实际主题：light, dark
        color: getStoreValue('color') || appConfig.color,
        homeTheme: getStoreValue('homeTheme') || appConfig.homeTheme,
        indexModel:getStoreValue('indexModel') || appConfig.indexModel, //首页模式
        //用户信息
        token: getStoreValue('token') || '',
        refreshToken: getStoreValue('refreshToken') || '',
        tenantId: getStoreValue('tenantId') || '',
        userInfo: getStoreValue('userInfo') || {},
        //菜单信息
        menus: getStoreValue('menus') || [],
        buttons: getStoreValue('buttons') || {},
        //项目合同段数据
        projectContract: getStoreValue('projectContract') || [],
        projectInfo: getStoreValue('projectInfo') || {},
        contractInfo: getStoreValue('contractInfo') || {},
        projectId: getStoreValue('projectId') || '',
        contractId: getStoreValue('contractId') || '',
        //其他配置信息
        bubble: getStoreValue('bubble') || false,
        orderServiceTipModal: getStoreValue('orderServiceTipModal') ?? 1, //0不弹出，1弹出
        shotWebRtc: getStoreValue('shotWebRtc') || 0, //WebRtc截图方式: 0关闭，1开启
        fullScreen: getStoreValue('fullScreen') || 0, //全屏截图：0关闭，1开启
        isCollapse: getStoreValue('isCollapse') || false, //菜单折叠
        dragModalSortTop: [], //拖拽弹窗排序
        isScreenShort: false,
        barMenuName: '',
        isSource: getStoreValue('isSource') || '', //来源
    }),
    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,
        getHomeTheme: state => state.homeTheme,
        getIndexModel:state => state.indexModel,
        //用户信息
        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,
        getIsSource: state => state.isSource,
    },
    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)
        },
        setHomeTheme(value) {
            this.homeTheme = value
            setStoreValue('homeTheme', value)
        },
        //设置首页模式
        setIndexModel(value) {
            this.indexModel = value
            setStoreValue('indexModel', 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
        },
        //项目合同段数据
        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)
        },
        setOrderServiceTipModal(value) {
            this.orderServiceTipModal = value
            setStoreValue('orderServiceTipModal', value)
        },
        setScreenShort(value) {
            this.isScreenShort = value
        },
        setShotWebRtc(value) {
            this.shotWebRtc = value
            setStoreValue('shotWebRtc', value)
        },
        setFullScreen(value) {
            this.fullScreen = value
            setStoreValue('fullScreen', value)
        },
        setCollapse(value) { //菜单折叠
            this.isCollapse = value
            setStoreValue('isCollapse', value)
        },
        setDragModalSortTop(value) {
            this.dragModalSortTop = value
        },
        setIsSource(value) {
            this.isSource = value
            setStoreValue('isSource', 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.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 = []
            this.isSource = ''
            //清除缓存
            clearStoreAll()
            removeToken()
            removeRefreshToken()
        },
    },
})

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