<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Windows.Threading;
using TCC.Data;
using TCC.Interop.Proxy;
using TCC.ViewModels;
using TCC.Windows;

namespace TCC
{
    public sealed class KeyboardHook : IDisposable
    {
        private static KeyboardHook _instance;
        public static KeyboardHook Instance => _instance ?? (_instance = new KeyboardHook());

        private readonly Window _window;
        private readonly Dictionary<HotKey, Action> _callbacks;
        private int _currentId;
        private bool _isRegistered;
        private bool _isInitialized;

        public event Action<HotKey> KeyPressed;

        private KeyboardHook()
        {
            _callbacks = new Dictionary<HotKey, Action>();
            _window = new Window();
            // register the event of the inner native window.
            _window.KeyPressed += (key) => KeyPressed?.Invoke(key);
        }

        public void Enable()
        {
            if (_isInitialized) return;
            Console.WriteLine("Enabling keyhook");
            App.BaseDispatcher.Invoke(() =>
            {
                // register the event that is fired after the key press.
                Instance.KeyPressed += OnKeyPressed;
                Game.ChatModeChanged += BeginCheckEnable;
                FocusManager.ForegroundChanged += BeginCheckEnable;
                _isInitialized = true;
                BeginCheckEnable();

            });
        }

        public void Disable()
        {
            if (!_isInitialized) return;
            Console.WriteLine("Disabling keyhook");
            App.BaseDispatcher.Invoke(() =>
            {
                if (_isRegistered) ClearHotkeys();
                // register the event that is fired after the key press.
                Instance.KeyPressed -= OnKeyPressed;
                Game.ChatModeChanged -= BeginCheckEnable;
                FocusManager.ForegroundChanged -= BeginCheckEnable;
                _isInitialized = false;
            });
        }

        public void RegisterCallback(HotKey hk, Action callback)
        {
            _callbacks[hk] = callback;
        }

        public void ChangeHotkey(HotKey oldHk, HotKey newHk)
        {
            if (!_callbacks.TryGetValue(oldHk, out var cb)) return;
            Console.WriteLine($"Changing {oldHk} to {newHk}");
            _callbacks[newHk] = cb;
            _callbacks.Remove(oldHk);
            BeginCheckEnable();
        }

        private void CheckEnable(bool value)
        {
            if (value && !_isRegistered)
            {<commit_msg>remove prints from keyboardhook<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Windows.Threading;
using TCC.Data;
using TCC.Interop.Proxy;
using TCC.ViewModels;
using TCC.Windows;

namespace TCC
{
    public sealed class KeyboardHook : IDisposable
    {
        private static KeyboardHook _instance;
        public static KeyboardHook Instance => _instance ?? (_instance = new KeyboardHook());

        private readonly Window _window;
        private readonly Dictionary<HotKey, Action> _callbacks;
        private int _currentId;
        private bool _isRegistered;
        private bool _isInitialized;

        public event Action<HotKey> KeyPressed;

        private KeyboardHook()
        {
            _callbacks = new Dictionary<HotKey, Action>();
            _window = new Window();
            // register the event of the inner native window.
            _window.KeyPressed += (key) => KeyPressed?.Invoke(key);
        }

        public void Enable()
        {
            if (_isInitialized) return;
            App.BaseDispatcher.Invoke(() =>
            {
                // register the event that is fired after the key press.
                Instance.KeyPressed += OnKeyPressed;
                Game.ChatModeChanged += BeginCheckEnable;
                FocusManager.ForegroundChanged += BeginCheckEnable;
                _isInitialized = true;
                BeginCheckEnable();

            });
        }

        public void Disable()
        {
            if (!_isInitialized) return;
            App.BaseDispatcher.Invoke(() =>
            {
                if (_isRegistered) ClearHotkeys();
                // register the event that is fired after the key press.
                Instance.KeyPressed -= OnKeyPressed;
                Game.ChatModeChanged -= BeginCheckEnable;
                FocusManager.ForegroundChanged -= BeginCheckEnable;
                _isInitialized = false;
            });
        }

        public void RegisterCallback(HotKey hk, Action callback)
        {
            _callbacks[hk] = callback;
        }

        public void ChangeHotkey(HotKey oldHk, HotKey newHk)
        {
            if (!_callbacks.TryGetValue(oldHk, out var cb)) return;
            Console.WriteLine($"Changing {oldHk} to {newHk}");
            _callbacks[newHk] = cb;
            _callbacks.Remove(oldHk);
            BeginCheckEnable();
        }

        private void CheckEnable(bool value)
        {
            if (value && !_isRegistered)
            {
{'fraction_non_alphanumeric': 0.08363292336802271, 'fraction_numerical': 0.0, 'mean_word_length': 1.8759521218715995, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '204145', 'n_tokens_mistral': 1355, 'n_tokens_neox': 1320, 'n_words': 347}