// 假设 Key 枚举类型已经定义
enum Key {
    A, B, C, D, E, F, G, H, I, J, K, M, L, N, O, P, Q, R, S, T, U, V, W, X, Y, Z,
    D0, D1, D2, D3, D4, D5, D6, D7, D8, D9,
    Up, Down, Left, Right,
    Space, Enter, Tab, Delete, Insert,
    PageDown, PageUp, Home, End,
    Escape, LeftShift, LeftCtrl,
    F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12,
    Add, Subtract,
    OemTilde, OemMinus, OemPlus, OemOpenBrackets, OemCloseBrackets, Oem5, OemComma, OemPeriod, OemSemicolon, OemQuotes, OemQuestion
}

class HotkeyParames {
    static Separator: string = '@';
    private static _keyList: { [key: string]: Key } = {
        "A": Key.A,
        "B": Key.B,
        "C": Key.C,
        "D": Key.D,
        "E": Key.E,
        "F": Key.F,
        "G": Key.G,
        "H": Key.H,
        "I": Key.I,
        "J": Key.J,
        "K": Key.K,
        "M": Key.M,
        "L": Key.L,
        "N": Key.N,
        "O": Key.O,
        "P": Key.P,
        "Q": Key.Q,
        "R": Key.R,
        "S": Key.S,
        "T": Key.T,
        "U": Key.U,
        "V": Key.V,
        "W": Key.W,
        "X": Key.X,
        "Y": Key.Y,
        "Z": Key.Z,

        "0": Key.D0,
        "1": Key.D1,
        "2": Key.D2,
        "3": Key.D3,
        "4": Key.D4,
        "5": Key.D5,
        "6": Key.D6,
        "7": Key.D7,
        "8": Key.D8,
        "9": Key.D9,

        "Up": Key.Up,
        "Down": Key.Down,
        "Left": Key.Left,
        "Right": Key.Right,

        "Space": Key.Space,
        "Enter": Key.Enter,
        "Tab": Key.Tab,
        "Del": Key.Delete,
        "Insert": Key.Insert,

        "PgDn": Key.PageDown,
        "PgUp": Key.PageUp,
        "Home": Key.Home,
        "End": Key.End,

        "Esc": Key.Escape,
        "Shift": Key.LeftShift,
        "Ctrl": Key.LeftCtrl,

        "F1": Key.F1,
        "F2": Key.F2,
        "F3": Key.F3,
        "F4": Key.F4,
        "F5": Key.F5,
        "F6": Key.F6,
        "F7": Key.F7,
        "F8": Key.F8,
        "F9": Key.F9,
        "F10": Key.F10,
        "F11": Key.F11,
        "F12": Key.F12,

        "加号": Key.Add,
        "减号": Key.Subtract,
        "`": Key.OemTilde,
        "-": Key.OemMinus,
        "=": Key.OemPlus,
        "[": Key.OemOpenBrackets,
        "]": Key.OemCloseBrackets,
        "\\": Key.Oem5,
        ",": Key.OemComma,
        ".": Key.OemPeriod,
        ";": Key.OemSemicolon,
        "'": Key.OemQuotes,
        "/": Key.OemQuestion
    };

    private static _enableSelfKey: string[] = [
        "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12",
        "Tab", "Enter", "Esc", "Up", "Down", "Left", "Right", "PgUp", "PgDn", "Home", "End",
        "Del", "Insert", "减号", "加号"
    ];

    static ParseKeyValue_FromIntToStr(keyValue: string): string {
        keyValue = keyValue.replace("amp;", "");
        let result = "";
        if (keyValue === "") return result;
        const parts = keyValue.split(HotkeyParames.Separator);
        for (const item1 of parts) {
            if (item1.trim().length <= 0) continue;
            const subParts = item1.split('+');
            for (const item2 of subParts) {
                if (item2.trim().length <= 0) break;
                result += HotkeyParames.GetNameByValue(Key[parseInt(item2)]) + " + ";
            }
            result = result.trim().substring(0, result.trim().length - 1).trim() + HotkeyParames.Separator;
        }
        return result;
    }

    static ParseKeyValue_FromStrToInt(keyValue: string): string {
        let result = "";
        if (keyValue === "") return "0;";
        const parts = keyValue.split(HotkeyParames.Separator);
        for (const item1 of parts) {
            if (item1.trim().length <= 0) continue;
            const subParts = item1.split('+');
            for (const item of subParts) {
                if (item.trim().length <= 0) break;
                result += HotkeyParames.GetValueByName(item.trim()) + "+";
            }
            result = result.substring(0, result.length - 1) + HotkeyParames.Separator;
        }
        return result;
    }

    static GetNameByValue(keyValue: Key): string {
        keyValue = HotkeyParames.UniKey(keyValue);
        const keyName = Object.keys(HotkeyParames._keyList).find(key => HotkeyParames._keyList[key] === keyValue);
        return keyName || "";
    }

    static GetValueByName(keyName: string): Key {
        if (keyName === "") return Key.None;
        return HotkeyParames._keyList[keyName] || Key.None;
    }

    static UniKey(keyValue: Key): Key {
        switch (keyValue) {
            case Key.RightCtrl:
                return Key.LeftCtrl;
            case Key.RightShift:
                return Key.LeftShift;
            case Key.Return:
                return Key.Enter;
            case Key.NumPad0:
                return Key.D0;
            case Key.NumPad1:
                return Key.D1;
            case Key.NumPad2:
                return Key.D2;
            case Key.NumPad3:
                return Key.D3;
            case Key.NumPad4:
                return Key.D4;
            case Key.NumPad5:
                return Key.D5;
            case Key.NumPad6:
                return Key.D6;
            case Key.NumPad7:
                return Key.D7;
            case Key.NumPad8:
                return Key.D8;
            case Key.NumPad9:
                return Key.D9;
            default:
                return keyValue;
        }
    }

    static CombHotKey(cont: string): string {
        if (cont === "") return "";
        let result: string;

        if (cont === "Shift" || cont === "Ctrl") cont = "";
        // 假设在 TypeScript 中可以通过某种方式获取键盘修饰键状态
        const modifiers = ""; // 这里需要根据实际情况实现获取键盘修饰键状态的逻辑
        if (modifiers.toLowerCase().includes("alt")) return "";
        if (modifiers.toLowerCase() !== "none") {
            result = modifiers.replace(',', '+') + "+" + cont;
        } else if (HotkeyParames._enableSelfKey.includes(cont)) {
            result = cont;
        } else {
            result = "Ctrl + " + cont;
        }

        result = result.replace("Control", "Ctrl").replace(" ", "").replace("+", " + ");
        return result;
    }
}

export default HotkeyParames;