/**
 * 设备状态管理
 */
import { defineStore } from 'pinia';
import {CLIENT_ID_CACHE_NAME} from "@/config/setting";
import { generateGUID } from "@/utils/common";

export enum LockScreenStatus {
  LOCK = 1,
  UNLOCK = 0
}

export interface ClientState {
  /** 客户端ID */
  clientId: string;
  /** 客户端名称 */
  clientName: string;
  /** 客户端版本号 */
  clientVersion: string;
  /** 是否锁定会话 */
  lockScreen: boolean;
}


export const useClientStore = defineStore('client', {
  state: (): ClientState => ({
    /** 客户端ID */
    clientId: getClientId(),
    /** 客户端名称 */
    clientName: 'PAdmin',
    /** 客户端版本号 */
    clientVersion: "1.0.0",
    /** 锁定会话 */
    lockScreen: false,
  }),
  actions: {
    /**
     * 设置锁屏状态
     * @param isLock
     */
    setLockScreen(isLock: LockScreenStatus = LockScreenStatus.UNLOCK) {
      this.lockScreen = isLock == 1;
    },
  }
});


/**
 * 获取客户端ID、没有的话则生成，格式为GUID格式
 */
function getClientId(): string {
  let clientId = localStorage.getItem(CLIENT_ID_CACHE_NAME);
  if (!clientId) {
    clientId = generateGUID();
    localStorage.setItem(CLIENT_ID_CACHE_NAME, clientId);
    return clientId
  }
  return clientId
}
