import { getSysConf } from '@/api';
import defaultLOGOIcon from '@/assets/logo_icon.png';
import defaultLOGOBrand from '@/assets/logo_brand.png';
import { RESULT_CODE_SUCCESS } from '@/constants/result-constants';
import { validHttp } from '@/utils/regular-utils';
import { defineStore } from 'pinia';
import { sessionGet } from '@/utils/cache-session-utils';

/**应用参数类型 */
type AppStore = {
  /**应用名称 */
  appName: string;
  /**应用标识 */
  appCode: string;
  /**应用版本 */
  appVersion: string;
  /**应用版权声明 */
  copyright: string;
  /**LOGO显示类型 */
  logoType: 'brand' | 'icon';
  /**LOGO文件路径 */
  filePathIcon: string;
  filePathBrand: string;
  /**开启用户注册 */
  registerUser: boolean;
  /**登录界面背景 */
  loginBackground: string;
};

const useAppStore = defineStore('app', {
  state: (): AppStore => ({
    appName: import.meta.env.VITE_APP_NAME,
    appCode: import.meta.env.VITE_APP_CODE,
    appVersion: import.meta.env.VITE_APP_VERSION,
    copyright: 'Copyright ©2023 For AGrandTech',
    logoType: 'icon',
    filePathIcon: '',
    filePathBrand: '',
    registerUser: false,
    loginBackground: '',
  }),
  getters: {
    /**
     * 获取正确LOGO地址-icon
     * @param state 内部属性不用传入
     * @returns LOGO地址url
     */
    getLOGOIcon(state) {
      const path = state.filePathIcon;
      if (!path || path === '#') {
        return defaultLOGOIcon;
      }
      if (validHttp(path)) {
        return path;
      }
      // 兼容旧前端可改配置文件
      const baseUrl = import.meta.env.PROD
        ? sessionGet('baseUrl') || import.meta.env.VITE_API_BASE_URL
        : import.meta.env.VITE_API_BASE_URL;
      return `${baseUrl}${path}`;
    },
    /**
     * 获取正确LOGO地址-brand
     * @param state 内部属性不用传入
     * @returns LOGO地址url
     */
    getLOGOBrand(state) {
      const path = state.filePathBrand;
      if (!path || path === '#') {
        return defaultLOGOBrand;
      }
      if (validHttp(path)) {
        return path;
      }
      // 兼容旧前端可改配置文件
      const baseUrl = import.meta.env.PROD
        ? sessionGet('baseUrl') || import.meta.env.VITE_API_BASE_URL
        : import.meta.env.VITE_API_BASE_URL;
      return `${baseUrl}${path}`;
    },
    /**
     * 获取正确登录背景地址
     * @param state 内部属性不用传入
     * @returns 背景地址url
     */
    getLoginBackground(state) {
      const path = state.loginBackground;
      if (!path || path === '#') {
        return '#';
      }
      if (validHttp(path)) {
        return path;
      }
      // 兼容旧前端可改配置文件
      const baseUrl = import.meta.env.PROD
        ? sessionGet('baseUrl') || import.meta.env.VITE_API_BASE_URL
        : import.meta.env.VITE_API_BASE_URL;
      return `${baseUrl}${path}`;
    },
  },
  actions: {
    /**设置网页标题 */
    setTitle(title?: string) {
      if (title && title.indexOf('router.') === -1) {
        document.title = `${title} - ${this.appName}`;
      } else {
        document.title = this.appName;
      }
    },
    /**设置版权声明 */
    setCopyright(text: string) {
      this.copyright = text;
    },
    /**设置LOGO */
    setLOGO(type: 'brand' | 'icon', filePath: string) {
      this.logoType = type;
      if (type === 'brand') {
        this.filePathBrand = filePath;
      }
      if (type === 'icon') {
        this.filePathIcon = filePath;
      }
    },
    // 获取系统配置信息
    async fnSysConf() {
      const res = await getSysConf();
      if (res.code === RESULT_CODE_SUCCESS && res.data) {
        if (res.data.title) {
          this.appName = res.data.title;
        }
        if (res.data.copyright) {
          this.copyright = res.data.copyright;
        }
        this.filePathIcon = res.data.filePathIcon;
        this.filePathBrand = res.data.filePathBrand;
        if (res.data.logoType) {
          this.logoType = res.data.logoType;
          // 修改html内容-小图当作favicon.ico
          const iconDom = document.querySelector("link[rel~='icon']");
          if (iconDom) {
            iconDom.setAttribute('href', this.getLOGOIcon);
          }
        }
        this.registerUser = res.data.registerUser === 'true';
        if (res.data.loginBackground) {
          this.loginBackground = res.data.loginBackground;
        }
      }
      return res;
    },
  },
});

export default useAppStore;
