using Avalonia;
using Avalonia.Media.Imaging;
using Avalonia.Styling;
using Avalonia.Threading;
using CommunityToolkit.Mvvm.ComponentModel;
using Ryujinx.Ava.Common;
using Ryujinx.Ava.Common.Locale;
using Ryujinx.Ava.Systems.Configuration;
using System;

namespace Ryujinx.Ava.UI.ViewModels
{
    public partial class AboutWindowViewModel : BaseModel, IDisposable
    {
        [ObservableProperty] public partial Bitmap GitLabLogo { get; set; }

        [ObservableProperty] public partial Bitmap DiscordLogo { get; set; }

        [ObservableProperty] public partial string Version { get; set; }

        public static string Developers => "GreemDev, LotP";

        public static string FormerDevelopers => LocaleManager.Instance.UpdateAndGetDynamicValue(
            LocaleKeys.AboutPageDeveloperListMore,
            "gdkchan, Ac_K, marysaka, rip in peri peri, LDj3SNuD, emmaus, Thealexbarney, GoffyDude, TSRBerry, IsaacMarovitz");

        public AboutWindowViewModel()
        {
            Version = RyujinxApp.FullAppName + "\n" + Program.Version;
            UpdateLogoTheme(ConfigurationState.Instance.UI.BaseStyle.Value);

            RyujinxApp.ThemeChanged += Ryujinx_ThemeChanged;
        }

        private void Ryujinx_ThemeChanged()
        {
            Dispatcher.UIThread.Post(() => UpdateLogoTheme(ConfigurationState.Instance.UI.BaseStyle.Value));
        }

        private void UpdateLogoTheme(string theme)
        {
            bool isDarkTheme = theme == "Dark" ||
                               (theme == "Auto" && RyujinxApp.DetectSystemTheme() == ThemeVariant.Dark);

            DiscordLogo = UIImages.GetLogoByNameAndTheme("Discord", isDarkTheme)
                .CreateScaledBitmap(new PixelSize(32, 24));
            GitLabLogo = UIImages.GetLogoByNameAndTheme("GitLab", isDarkTheme)
                .CreateScaledBitmap(new PixelSize(32, 31));
        }

        public void Dispose()
        {
            RyujinxApp.ThemeChanged -= Ryujinx_ThemeChanged;

            GitLabLogo.Dispose();
            DiscordLogo.Dispose();

            GC.SuppressFinalize(this);
        }
    }
}
