<commit_before>﻿using Nostrum;
using System.Collections.Generic;
using System.Windows.Threading;
using TCC.Interop.Moongourd;

namespace TCC.UI.Controls.Chat
{
    public class MoongourdPopupViewModel : TSPropertyChanged
    {
        private readonly IMoongourdManager _manager;

        private string _playerName = "";

        public string PlayerName
        {
            get => _playerName;
            set
            {
                if (_playerName == value) return;
                _playerName = value;
                N();
            }
        }

        private string _emptyText = "No data.";

        public string EmptyText
        {
            get => _emptyText;
            set
            {
                if (_emptyText == value) return;
                _emptyText = value;
                N();
            }
        }

        public TSObservableCollection<MoongourdEncounter> Encounters { get; }

        public MoongourdPopupViewModel()
        {
            Dispatcher = Dispatcher.CurrentDispatcher;

            Encounters = new TSObservableCollection<MoongourdEncounter>(Dispatcher);

            _manager = new KabedonManager();
            _manager.Started += OnSearchStarted;
            _manager.Finished += OnSearchFinished;
            _manager.Failed += OnSearchFailed;
        }

        private void OnSearchStarted()
        {
            Dispatcher?.Invoke(() => EmptyText = "Loading...");
        }

        private void OnSearchFinished(List<MoongourdEncounter> list)
        {
            Dispatcher.Invoke(() =>
            {
                EmptyText = "No entries.";
                Encounters.Clear();
                list.ForEach(Encounters.Add);
            });
        }

        private void OnSearchFailed(string error)
        {
            Dispatcher?.Invoke(() =>
            {
                EmptyText = $"Failed to retrieve data.\n{error}";
            });
        }

        public void RequestInfo(string name, string region)
        {
            Dispatcher?.Invoke(() =>
            {
                PlayerName = name;
                Encounters.Clear();
            });

            if (region.StartsWith("EU"))
            {
                region = "EU";
            }
            _manager.GetEncounters(name, region, Game.Server.Name);
        }
    }

    public partial class MoongourdPopup
    {
        public MoongourdPopup()
        {
            MouseLeave += (_, _) => WindowManager.ViewModels.PlayerMenuVM.Close();
            InitializeComponent();
        }
    }
}<commit_msg>refactor MoongourdPopup control<commit_after>﻿using Nostrum;
using System.Collections.Generic;
using System.Windows.Input;
using System.Windows.Threading;
using TCC.Interop.Moongourd;

namespace TCC.UI.Controls.Chat
{
    public class MoongourdPopupViewModel : TSPropertyChanged
    {
        private readonly IMoongourdManager _manager;

        private string _playerName = "";

        public string PlayerName
        {
            get => _playerName;
            set
            {
                if (_playerName == value) return;
                _playerName = value;
                N();
            }
        }

        private string _emptyText = "No data.";

        public string EmptyText
        {
            get => _emptyText;
            set
            {
                if (_emptyText == value) return;
                _emptyText = value;
                N();
            }
        }

        public TSObservableCollection<EncounterViewModel> Encounters { get; }

        public MoongourdPopupViewModel()
        {
            Dispatcher = Dispatcher.CurrentDispatcher;

            Encounters = new TSObservableCollection<EncounterViewModel>(Dispatcher);

            _manager = new KabedonManager();
            _manager.Started += OnSearchStarted;
            _manager.Finished += OnSearchFinished;
            _manager.Failed += OnSearchFailed;
        }

        private void OnSearchStarted()
        {
            Dispatcher?.Invoke(() => EmptyText = "Loading...");
        }

        private void OnSearchFinished(List<IMoongourdEncounter> list)
        {
            Dispatcher.Invoke(() =>
            {
                EmptyText = "No entries.";
                Encounters.Clear();
                list.ForEach(e => Encounters.Add(new EncounterViewModel(e.AreaId, e.BossId, e.LogId) { PlayerDeaths = e.PlayerDeaths, PlayerDps = e.PlayerDps }));
            });
        }

        private void OnSearchFailed(string error)
        {
            Dispatcher?.Invoke(() =>
            {
                EmptyText = $"Failed to retrieve data.\n{error}";
            });
        }

        public void RequestInfo(string name, string region)
        {
            Dispatcher?.Invoke(() =>
            {
                PlayerName = name;
                Encounters.Clear();
            });

            if (region.StartsWith("EU"))
            {
                region = "EU";
            }
            _manager.GetEncounters(name, region, Game.Server.Name);
        }
    }

    public class EncounterViewModel
    {
        public string BossName { get; }
        public string DungeonName { get; }
        public int PlayerDps { get; init; }
        public int PlayerDeaths { get; init; }

        public ICommand Browse { get; }

        public EncounterViewModel(int areaId, int bossId, long logId)
        {
            DungeonName = Game.DB!.RegionsDatabase.GetZoneName((uint)areaId);
            BossName = Game.DB!.MonsterDatabase.GetMonsterName((uint)bossId, (uint)areaId);

            Browse = new RelayCommand(_ =>
            {
                var reg = App.Settings.LastLanguage.ToLower();
                reg = reg.StartsWith("eu") ? "eu" : reg;
                reg = reg == "na" ? "" : reg + "/";
                Utils.Utilities.OpenUrl("https://" + $"moongourd.com/{reg}upload/{areaId}/{bossId}/1/{logId}");
            });
        }
    }

    public partial class MoongourdPopup
    {
        public MoongourdPopup()
        {
            MouseLeave += (_, _) => WindowManager.ViewModels.PlayerMenuVM.Close();
            InitializeComponent();
        }
    }
}
{'fraction_non_alphanumeric': 0.09949683492939458, 'fraction_numerical': 0.00016231131309852296, 'mean_word_length': 1.5223086369218175, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 25, 'https://': 1, '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': '2394932', 'n_tokens_mistral': 1675, 'n_tokens_neox': 1591, 'n_words': 313}