//import { ILog, Level } from 'log4net'; // 假设 log4net 有对应的 TypeScript 定义//TODO日志信息可以先不登记

// 假设的 LoginUserInfo 类
class LoginUserInfo {
    static TellerNo: string;
}

// 假设的 PlatformSettings 类
class PlatformSettings {
    static IsDebugMode: boolean;
    static Encoding: {
        GetBytes: (str: string) => Uint8Array;
        GetString: (bytes: Uint8Array) => string;
    };
}

// 假设的 EncrypDecryptHelper 类
class EncrypDecryptHelper {
    static Instance: {
        DesEncryptFixKey: (bytes: Uint8Array) => Uint8Array;
    };
}

// 日志数据类
class LogData {
    constructor(
        //public LogObject: ILog,//TODO日志信息可以先不登记
        //public LogLevel: Level,//TODO日志信息可以先不登记
        public Content: string,
        public TradeNo: string = null,
        public LogException: Error = null
    ) {
        this.TellerNo = LoginUserInfo.TellerNo;
        this.LogDate = new Date().toISOString().replace('T', ' ').substring(0, 19);
    }

    //public LogObject: ILog;//TODO日志信息可以先不登记
    //ublic LogLevel: Level;
    //public TradeNo: string;
    public LogDate: string;
    public TellerNo: string;
    //public Content: string;
    //public LogException: Error;
    public AttachData: any;
}

// 日志记录器类
class LogWriter {
    private static _instance: LogWriter;
    private static _LogDataList: LogData[] = [];

    public static get Instance(): LogWriter {
        if (!this._instance) {
            this._instance = new LogWriter();
        }
        return this._instance;
    }

    private constructor() {
        // 定时任务部分暂时无法实现，因为 JavaScript 没有直接对应的 TimeSpan 和 TimerManager
    }

    public AddLogData(data: LogData) {
        LogWriter._LogDataList.push(data);
    }

    public SaveLog() {
        if (!LogWriter._LogDataList || LogWriter._LogDataList.length === 0) return;

        const list = [...LogWriter._LogDataList];
        LogWriter._LogDataList = [];

        list.forEach(item => {
            const target = item;
            let data = '';
            if (target.TradeNo) {
                data += `交易号:${target.TradeNo}\n`;
            }
            data += `操作柜员:${target.TellerNo}\n`;
            data += `发生时间:${target.LogDate}\n`;
            data += `日志信息:${target.Content}\n`;
            if (target.LogException) {
                data += `异常信息:${target.LogException}\n`;
            }

            let msg = data;
            if (!PlatformSettings.IsDebugMode) {
                const bytes = PlatformSettings.Encoding.GetBytes(msg);
                const encryptedBytes = EncrypDecryptHelper.Instance.DesEncryptFixKey(bytes);
                msg = PlatformSettings.Encoding.GetString(encryptedBytes);
            }

            let stringBuilder = 'Start\n';
            stringBuilder += msg;
            stringBuilder += 'End\n';

            // this.SendToLog4net(target.LogObject, target.LogLevel, stringBuilder, null);//TODO日志信息可以先不登记
        });
    }

    private SendToLog4net(log: "", level: "", msg: any, exception: Error) {//TODO日志信息可以先不登记(log: ILog, level: Level, msg: any, exception: Error)
        // switch (level.Name) {
        //     case 'DEBUG':
        //         log.Debug(msg, exception);
        //         break;
        //     case 'ERROR':
        //         log.Error(msg, exception);
        //         break;
        //     case 'FATAL':
        //         log.Fatal(msg, exception);
        //         break;
        //     case 'INFO':
        //         log.Info(msg, exception);
        //         break;
        //     case 'WARN':
        //         log.Warn(msg, exception);
        //         break;
        //     default:
        //         break;
        // }
    }
}

export { LogWriter, LogData };
