// 假设这些类型已经在其他地方定义
interface THotKeyMessage {
    KeyName: string;
    KeyValue: string;
    KeyType: number;
    Tellernum: string;
}

interface TTellerHotKey {
    KeyValue: string;
    TellerNum: string;
    KeyName: string;
}

class HotKeyHelper {
    static SetHotKey(dicKeys: { [key: string]: string }): void {
        // 实现设置快捷键的逻辑
    }
}

class HotKeysHandle {
    static GetKeyByTeller(tellerNO: string): THotKeyMessage[] {
        // 实现获取柜员快捷键的逻辑
        return [];
    }

    static UpdateKeyMessage(tTellerHotKey: TTellerHotKey): void {
        // 实现更新快捷键信息的逻辑
    }

    static DelKeyValue(keyName: string, tellerNO: string, valueInt: string): void {
        // 实现删除键值的逻辑
    }

    static DelKeyMessage(keyName: string, tellerNO: string): void {
        // 实现删除快捷键信息的逻辑
    }
}

class HotkeyParames {
    static ParseKeyValue_FromIntToStr(keyValue: string): string {
        // 实现从整数到字符串的键值解析逻辑
        return '';
    }

    static ParseKeyValue_FromStrToInt(keyValue: string): string {
        // 实现从字符串到整数的键值解析逻辑
        return '';
    }

    static Separator: string = ';';
}

export class Hotkey {
    private _arrKeys: THotKeyMessage[] = [];
    private _tellerNO: string;

    constructor(tellerNO: string) {
        this._tellerNO = tellerNO;
    }

    private SetHotkey(th: THotKeyMessage[]): void {
        if (!th || th.length === 0) return;
        const dicKeys: { [key: string]: string } = {};
        th.forEach(item => {
            dicKeys[item.KeyName] = HotkeyParames.ParseKeyValue_FromIntToStr(item.KeyValue).replace(' ', '');
        });
        HotKeyHelper.SetHotKey(dicKeys);
    }

    public Register(): void {
        this._arrKeys = HotKeysHandle.GetKeyByTeller(this._tellerNO);
        this.SetHotkey(this._arrKeys);
    }

    public GetKeys(): THotKeyMessage[] {
        return this._arrKeys;
    }

    public UpdateValueByKey(keyName: string, keyValue: string): void {
        if (this._arrKeys.length === 0) return;
        const tTellerHotKey: TTellerHotKey = {
            KeyValue: '',
            TellerNum: this._tellerNO,
            KeyName: keyName
        };

        for (const item of this._arrKeys) {
            if (item.KeyName === keyName) {
                tTellerHotKey.KeyValue = HotkeyParames.ParseKeyValue_FromStrToInt(keyValue);
                if (item.KeyValue === tTellerHotKey.KeyValue && item.Tellernum === tTellerHotKey.TellerNum) {
                    break;
                }
                HotKeysHandle.UpdateKeyMessage(tTellerHotKey);
                // item.KeyValue = tTellerHotKey.KeyValue;
                break;
            }
        }
        // Register(); // 影响效率2013-8-23
    }

    public DelValue(keyName: string, keyValue: string): void {
        let valueInt = '';
        for (const item of this._arrKeys) {
            if (item.KeyName === keyName) {
                valueInt = HotkeyParames.ParseKeyValue_FromStrToInt(keyValue);
                HotKeysHandle.DelKeyValue(keyName, this._tellerNO, valueInt);
                item.KeyValue = item.KeyValue.replace(valueInt, '');
                break;
            }
        }
        this.Register();
    }

    public IsUniquenessKey(keyValue: string): boolean {
        let result = true;
        keyValue = HotkeyParames.ParseKeyValue_FromStrToInt(keyValue);

        const str = this._arrKeys
           .filter(x => x.KeyValue.startsWith(keyValue) || x.KeyValue.includes(HotkeyParames.Separator + keyValue))
           .map(x => x.KeyName);
        if (str.length > 0) {
            result = false;
        }
        return result;
    }

    public ResetKeys(keyType: number): void {
        if (this._arrKeys.length === 0) return;
        this._arrKeys.forEach(item => {
            if (item.KeyType === keyType) {
                HotKeysHandle.DelKeyMessage(item.KeyName, this._tellerNO);
            }
        });
        this.Register();
    }

    public ResetKeys(): void {
        if (this._arrKeys.length === 0) return;
        this._arrKeys.forEach(item => {
            if (item.Tellernum === this._tellerNO) {
                HotKeysHandle.DelKeyMessage(item.KeyName, this._tellerNO);
            }
        });
        this.Register();
    }
}