<commit_before>﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Input;
using System.Windows.Threading;
using Dragablz;
using FoglioUtils;
using FoglioUtils.Extensions;
using TCC.Controls;
using TCC.Data;
using TCC.Data.Chat;
using TCC.Settings;
using TCC.Windows;

namespace TCC.ViewModels.Widgets
{


    public class ImportantRemovedArgs : EventArgs
    {
        public ActionType Action { get; }
        public ChatMessage Item { get; }
        public enum ActionType
        {
            Remove,
            Clear
        }

        public ImportantRemovedArgs(ActionType action, ChatMessage item = null)
        {
            Action = action;
            Item = item;
        }

    }
    public class ChatViewModel : TSPropertyChanged
    {
        public event Action ForceSizePosUpdateEvent;

        private bool _paused;
        private bool _visible = true;
        private DispatcherTimer _hideTimer;
        private ChatWindowSettings _windowSettings;
        private bool _collapsed;
        private Tab _currentTab;

        public bool Paused
        {
            get => _paused;
            set
            {
                if (_paused == value) return;
                _paused = value;
                N();
            }
        }
        public bool Visible
        {
            get => _visible;
            set
            {
                if (_visible == value) return;
                _visible = value;
                N();
            }
        }
        public bool Collapsed
        {
            get => WindowSettings.CanCollapse && _collapsed;
            set
            {
                if (_collapsed == value) return;
                _collapsed = value;
                if (_collapsed && WindowSettings.StaysCollapsed)
                {
                    WindowSettings.H = 84;
                    WindowSettings.Y += 84;
                    ForceSizePosUpdateEvent?.Invoke();
                }
                N();
            }
        }
        public Tab CurrentTab
        {
            get => _currentTab;
            set
            {
                if (_currentTab == value) return;
                _currentTab = value;
                N();
            }
        }
        public ICommand MakeGlobalCommand { get; }
        public ICommand OpenSysMsgSettingsCommand { get; }
        public ICommand UnpinMessageCommand { get; }


        public ChatWindowSettings WindowSettings
        {
            get => _windowSettings;
            set
            {
                if (_windowSettings == value) return;
                if (_windowSettings != null)
                {
                    _windowSettings.TimeoutChanged -= ChangeTimerInterval;
                }
                _windowSettings = value;
                if (_windowSettings != null)
                {
                    _hideTimer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(WindowSettings.HideTimeout) };
                    _hideTimer.Tick += OnHideTimerTick;
                    _hideTimer.Start();
                    _windowSettings.TimeoutChanged += ChangeTimerInterval;
                }
            }
        }

        public TSObservableCollection<TabViewModel> TabVMs { get; set; }
        public TSObservableCollection<LFG> LFGs => ChatWindowManager.Instance.LFGs;
        public IInterTabClient InterTabClient { get; }
        public List<Tab> Tabs
        {
            get
            {
                var ret = new List<Tab>();
                TabVMs.ToList().ForEach(x => ret.Add(x.Content as Tab));
                return ret;
            }
        }
        public Func<TabViewModel> AddNewTabCommand
        {
            get
            {
                return () =>
                {
                    // invoked on main thread because TabData is created there by JSON settings deserialization
                    //return App.BaseDispatcher.Invoke(() =>
                    //{
                    var t = new TabViewModel();
                    var content = new Tab(new TabInfo("New tab"));
                    content.PropertyChanged += (_, ev) =>
                    {
                        if (ev.PropertyName == nameof(TabInfoVM.TabName)) t.Header = content.TabInfoVM.TabName;
                    };
                    t.Content = content;
                    return t;
                    //});
                };
            }
        }

        public ChatViewModel(ChatWindowSettings s)
        {
            WindowSettings = s;
            InterTabClient = new ChatTabClient();
            TabVMs = new TSObservableCollection<TabViewModel>();

            MakeGlobalCommand = new RelayCommand(_ => WindowSettings.MakePositionsGlobal());
            OpenSysMsgSettingsCommand = new RelayCommand(_ => new SystemMessagesConfigWindow { ShowActivated = true, Topmost = true }.Show());

            ChatWindowManager.Instance.NewMessage += CheckAttention;
            Game.GameUiModeChanged += CheckCollapsed;
            Game.ChatModeChanged += CheckCollapsed;
            WindowSettings.CanCollapseChanged += () => N(nameof(Collapsed));
            WindowSettings.StaysCollapsedChanged += () => N(nameof(Collapsed));
            if (WindowSettings.StaysCollapsed) _collapsed = true;
        }

        private void CheckCollapsed()
        {
            Collapsed = !(Game.InGameUiOn || Game.InGameChatOpen) || WindowSettings.StaysCollapsed;
        }

        private void ChangeTimerInterval()
        {
            _hideTimer.Interval = TimeSpan.FromSeconds(WindowSettings.HideTimeout);
            _hideTimer.Refresh();
        }
        private void OnHideTimerTick(object sender, EventArgs e)
        {
            if (!_windowSettings.FadeOut)
            {
                if (!Visible) Visible = true;
                return;
            }
            Visible = false;
            _hideTimer.Stop();
        }
        private void CheckAttention(ChatMessage chatMessage)
        {
            if (!WindowSettings.ShowImportant) return;
            if (!chatMessage.ContainsPlayerName && chatMessage.Channel != ChatChannel.ReceivedWhisper) return;
            TabVMs.Where(x => ((Tab)x.Content).GetDispatcher()
                                .Invoke(() => ((Tab)x.Content).Messages.Contains(chatMessage)))
                        .ToList()
                        .ForEach(t =>
                        {
                            ((Tab)t.Content).AddImportantMessage(chatMessage);
                            //((Tab) t.Content).Attention = true;<commit_msg>fix mouseover thing in chat windows<commit_after>﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Input;
using System.Windows.Threading;
using Dragablz;
using FoglioUtils;
using FoglioUtils.Extensions;
using TCC.Controls;
using TCC.Data;
using TCC.Data.Chat;
using TCC.Settings;
using TCC.Windows;

namespace TCC.ViewModels.Widgets
{


    public class ImportantRemovedArgs : EventArgs
    {
        public ActionType Action { get; }
        public ChatMessage Item { get; }
        public enum ActionType
        {
            Remove,
            Clear
        }

        public ImportantRemovedArgs(ActionType action, ChatMessage item = null)
        {
            Action = action;
            Item = item;
        }

    }
    public class ChatViewModel : TSPropertyChanged
    {
        public event Action ForceSizePosUpdateEvent;

        private bool _paused;
        private bool _visible = true;
        private DispatcherTimer _hideTimer;
        private ChatWindowSettings _windowSettings;
        private bool _collapsed;
        private bool _mouseOver;
        private Tab _currentTab;

        public bool Paused
        {
            get => _paused;
            set
            {
                if (_paused == value) return;
                _paused = value;
                N();
            }
        }
        public bool Visible
        {
            get => _visible;
            set
            {
                if (_visible == value) return;
                _visible = value;
                N();
            }
        }
        public bool Collapsed
        {
            get => WindowSettings.CanCollapse && _collapsed;
            set
            {
                if (_collapsed == value) return;
                _collapsed = value;
                if (_collapsed && WindowSettings.StaysCollapsed)
                {
                    WindowSettings.H = 84;
                    WindowSettings.Y += 84;
                    ForceSizePosUpdateEvent?.Invoke();
                }
                N();
            }
        }
        public bool MouseOver
        {
            get => _mouseOver;
            set
            {
                if (_mouseOver == value) return;
                _mouseOver = value;
                N();
            }
        }
        public Tab CurrentTab
        {
            get => _currentTab;
            set
            {
                if (_currentTab == value) return;
                _currentTab = value;
                N();
            }
        }
        public ICommand MakeGlobalCommand { get; }
        public ICommand OpenSysMsgSettingsCommand { get; }
        public ICommand UnpinMessageCommand { get; }


        public ChatWindowSettings WindowSettings
        {
            get => _windowSettings;
            set
            {
                if (_windowSettings == value) return;
                if (_windowSettings != null)
                {
                    _windowSettings.TimeoutChanged -= ChangeTimerInterval;
                }
                _windowSettings = value;
                if (_windowSettings != null)
                {
                    _hideTimer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(WindowSettings.HideTimeout) };
                    _hideTimer.Tick += OnHideTimerTick;
                    _hideTimer.Start();
                    _windowSettings.TimeoutChanged += ChangeTimerInterval;
                }
            }
        }

        public TSObservableCollection<TabViewModel> TabVMs { get; set; }
        public TSObservableCollection<LFG> LFGs => ChatWindowManager.Instance.LFGs;
        public IInterTabClient InterTabClient { get; }
        public List<Tab> Tabs
        {
            get
            {
                var ret = new List<Tab>();
                TabVMs.ToList().ForEach(x => ret.Add(x.Content as Tab));
                return ret;
            }
        }
        public Func<TabViewModel> AddNewTabCommand
        {
            get
            {
                return () =>
                {
                    // invoked on main thread because TabData is created there by JSON settings deserialization
                    //return App.BaseDispatcher.Invoke(() =>
                    //{
                    var t = new TabViewModel();
                    var content = new Tab(new TabInfo("New tab"));
                    content.PropertyChanged += (_, ev) =>
                    {
                        if (ev.PropertyName == nameof(TabInfoVM.TabName)) t.Header = content.TabInfoVM.TabName;
                    };
                    t.Content = content;
                    return t;
                    //});
                };
            }
        }

        public ChatViewModel(ChatWindowSettings s)
        {
            WindowSettings = s;
            InterTabClient = new ChatTabClient();
            TabVMs = new TSObservableCollection<TabViewModel>();

            MakeGlobalCommand = new RelayCommand(_ => WindowSettings.MakePositionsGlobal());
            OpenSysMsgSettingsCommand = new RelayCommand(_ => new SystemMessagesConfigWindow { ShowActivated = true, Topmost = true }.Show());

            ChatWindowManager.Instance.NewMessage += CheckAttention;
            Game.GameUiModeChanged += CheckCollapsed;
            Game.ChatModeChanged += CheckCollapsed;
            WindowSettings.CanCollapseChanged += () => N(nameof(Collapsed));
            WindowSettings.StaysCollapsedChanged += () => N(nameof(Collapsed));
            if (WindowSettings.StaysCollapsed) _collapsed = true;
        }

        private void CheckCollapsed()
        {
            Collapsed = !(Game.InGameUiOn || Game.InGameChatOpen) || WindowSettings.StaysCollapsed;
        }

        private void ChangeTimerInterval()
        {
            _hideTimer.Interval = TimeSpan.FromSeconds(WindowSettings.HideTimeout);
            _hideTimer.Refresh();
        }
        private void OnHideTimerTick(object sender, EventArgs e)
        {
            if (!_windowSettings.FadeOut)
            {
                if (!Visible) Visible = true;
                return;
            }

            if (MouseOver) return;
            Visible = false;
            _hideTimer.Stop();
        }
        private void CheckAttention(ChatMessage chatMessage)
        {
            if (!WindowSettings.ShowImportant) return;
            if (!chatMessage.ContainsPlayerName && chatMessage.Channel != ChatChannel.ReceivedWhisper) return;
            TabVMs.Where(x => ((Tab)x.Content).GetDispatcher()
                                .Invoke(() => ((Tab)x.Content).Messages.Contains(chatMessage)))
                        .ToList()
                        .ForEach(t =>
                        {
                            ((Tab)t.Content).AddImportantMessage(chatMessage);
                            //((Tab) t.Content).Attention = true;
{'fraction_non_alphanumeric': 0.08169451426881562, 'fraction_numerical': 0.0005914534969688008, 'mean_word_length': 1.380256906563435, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 50, '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': '5097614', 'n_tokens_mistral': 3344, 'n_tokens_neox': 3156, 'n_words': 713}