// 假设 ConfigManager 和 PlatformLogger 已经在其他地方定义
class ConfigManager {
    private static instance: ConfigManager;
    private constructor() { }

    public static GetInstance(): ConfigManager {
        if (!this.instance) {
            this.instance = new ConfigManager();
        }
        return this.instance;
    }

    public GetConfigValue(key: string, configType: ConfigType): string {
        // 这里需要实现从配置文件读取值的逻辑
        return '';
    }
}

enum ConfigType {
    Client,
    System
}

export default class PlatformLogger {
    public static SystemErrorInfo(message: string, error: Error): void {
        console.error(message, error);
    }
}

export class PlatformSettings {
    private static _encoding: BufferEncoding;

    /**
     * 字符编码，默认值是系统默认
     */
    public static get Encoding(): BufferEncoding {
        const encName = ConfigManager.GetInstance().GetConfigValue('Encoding', ConfigType.Client);
        if (!this._encoding) {
            this._encoding = 'utf8'; // 假设系统默认编码为 utf8
        }
        if (encName !== this._encoding) {
            try {
                // 若编码发生变化，则更新
                this._encoding = encName as BufferEncoding;
            } catch (e) {
                PlatformLogger.SystemErrorInfo(`PlatformSettings.Encoding系统参数转换异常！${encName}`, e as Error);
            }
        }
        return this._encoding;
    }

    /**
     * 是否启用调试模式,默认为不启用
     */
    public static get IsDebugMode(): boolean {
        let debugModeBoolean = false;
        const debugMode = ConfigManager.GetInstance().GetConfigValue('IsDebugMode', ConfigType.System);
        if (debugMode) {
            debugModeBoolean = debugMode.toLowerCase() === 'true';
        }
        return debugModeBoolean;
    }

    /**
     * 平台项目名称，默认为空
     */
    public static get PlatformTitle(): string {
        return ConfigManager.GetInstance().GetConfigValue('PlatformTitle', ConfigType.System);
    }

    /**
     * 打印偏移量,默认为（0,0）
     */
    public static get PrinterOffset(): { x: number; y: number } {
        let offsetPoint = { x: 0, y: 0 };
        const offset = ConfigManager.GetInstance().GetConfigValue('PrinterOffset', ConfigType.Client);
        if (offset) {
            const index = offset.indexOf(',');
            if (index > 0) {
                const offsetX = parseFloat(offset.substring(0, index));
                const offsetY = parseFloat(offset.slice(index + 1));
                if (!isNaN(offsetX) && !isNaN(offsetY)) {
                    offsetPoint = { x: offsetX, y: offsetY };
                }
            }
        }
        return offsetPoint;
    }

    /**
     * 服务器端通讯地址
     */
    public static get ServiceAddress(): string {
        return ConfigManager.GetInstance().GetConfigValue('SocketServiceAddress', ConfigType.Client);
    }

    /**
     * 通讯超时时间,默认是1分钟,单位是毫秒
     */
    public static get Timeout(): number {
        let timeout = 6000;
        const time = ConfigManager.GetInstance().GetConfigValue('SocketTimeOut', ConfigType.Client);
        const parsedTime = parseInt(time, 10);
        if (!isNaN(parsedTime)) {
            timeout = parsedTime;
        }
        return timeout;
    }

    /**
     * 交易文件dll路径，默认为Pages
     */
    public static get PagesDirectory(): string {
        let procName = ConfigManager.GetInstance().GetConfigValue('PagesDirectory', ConfigType.System);
        if (!procName) {
            procName = 'Pages';
        }
        return procName;
    }

    /**
     * 是否对报文加密标志
     */
    public static get IsEncrypt(): boolean {
        let EncryptBoolean = false;
        const Encrypt = ConfigManager.GetInstance().GetConfigValue('IsEncrypt', ConfigType.Client);
        if (Encrypt) {
            EncryptBoolean = Encrypt.toLowerCase() === 'true';
        }
        return EncryptBoolean;
    }

    /**
     * 服务端通讯报文加密KEY
     */
    public static get EncryptsKey(): string {
        let encryptskey = ConfigManager.GetInstance().GetConfigValue('EncryptsKey', ConfigType.Client);
        if (!encryptskey) {
            encryptskey = 'DHCCSoft';
        }
        return encryptskey;
    }
}
