using Avalonia.Controls;
using Avalonia.Media;
using Avalonia.Threading;
using Mlaumcherb.Client.Avalonia.классы;

namespace Mlaumcherb.Client.Avalonia.зримое;

public abstract partial class GameVersionItemView : ListBoxItem
{
    private static readonly SolidColorBrush _avaliableColor = new(Color.FromRgb(30, 130, 40));
    private static readonly SolidColorBrush _unavaliableColor = new(Color.FromRgb(170, 70, 70));

    protected GameVersionItemView(string name, bool initialStatus)
    {
        InitializeComponent();
        text.Text = name;
        UpdateBackground(initialStatus);
    }

    protected void UpdateBackground(bool isInstalled)
    {
        Dispatcher.UIThread.Invoke(() => Background = isInstalled ? _avaliableColor : _unavaliableColor);
    }
}

// Background changes when props status changes
public class InstalledGameVersionItemView : GameVersionItemView
{
    public readonly InstalledGameVersionProps Props;
    
    public InstalledGameVersionItemView(InstalledGameVersionProps props) : base(props.Id, props.IsInstalled)
    {
        Props = props;
        props.StatusChanged += UpdateBackground;
    }
}

// Background is set once by checking if the version is present in InstalledVersionsCatalog
public class RemoteGameVersionItemView : GameVersionItemView
{
    public readonly RemoteVersionDescriptorProps Props;
    
    public RemoteGameVersionItemView(RemoteVersionDescriptorProps props) 
        : base(props.id, LauncherApp.InstalledVersionCatalog.versions.ContainsKey(props.id))
    {
        Props = props;
    }
}