using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Shapes;
using Avalonia.Markup.Xaml;
using DesertPaintCodex.Models;
namespace DesertPaintCodex.Views
{
public class ReactionUnitView : UserControl
{
public static readonly DirectProperty<ReactionUnitView, Reaction?> ReactionProperty = AvaloniaProperty.RegisterDirect<ReactionUnitView, Reaction?>( nameof(Reaction),
o => o.Reaction,
(o, v) => o.Reaction = v);
private Reaction? _reaction;
public Reaction? Reaction { get => _reaction; set => SetAndRaise(ReactionProperty, ref _reaction, value); }
private readonly Border _inertBox;
private readonly Border _redBox;
private readonly Border _greenBox;
private readonly Border _blueBox;
private readonly TextBlock _redQty;
private readonly TextBlock _greenQty;
private readonly TextBlock _blueQty;
private readonly Path _redArrow;
private readonly Path _greenArrow;
private readonly Path _blueArrow;
public ReactionUnitView()
{
InitializeComponent();
_inertBox = this.FindControl<Border>("InertBox");
_redBox = this.FindControl<Border>("RedBox");
_greenBox = this.FindControl<Border>("GreenBox");
_blueBox = this.FindControl<Border>("BlueBox");
_redQty = _redBox.FindControl<TextBlock>("RedQty");
_greenQty = _greenBox.FindControl<TextBlock>("GreenQty");
_blueQty = _blueBox.FindControl<TextBlock>("BlueQty");
_redArrow = _redBox.FindControl<Path>("RedArrow");
_greenArrow = _greenBox.FindControl<Path>("GreenArrow");
_blueArrow = _blueBox.FindControl<Path>("BlueArrow");
ReactionProperty.Changed.AddClassHandler<ReactionUnitView>((x, _) => x.UpdateReaction());
UpdateReaction();
}
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}
private void UpdateReaction()
{
if (_reaction == null)
{
_inertBox.IsVisible = false;
_redBox.IsVisible = false;
_greenBox.IsVisible = false;
_blueBox.IsVisible = false;
return;
}
_inertBox.IsVisible = (_reaction.Red == 0) && (_reaction.Green == 0) && (_reaction.Blue == 0);
UpdateColor(_redBox, _redQty, _redArrow, _reaction.Red);
UpdateColor(_greenBox, _greenQty, _greenArrow, _reaction.Green);
UpdateColor(_blueBox, _blueQty, _blueArrow, _reaction.Blue);
}
private void UpdateColor(IControl box, TextBlock qty, IControl arrow, int value)
{
switch (value)
{
case > 0:
arrow.Classes.Remove("DownArrow");
arrow.Classes.Add("UpArrow");
qty.Text = value.ToString();
box.IsVisible = true;
break;
case < 0:
arrow.Classes.Remove("UpArrow");
arrow.Classes.Add("DownArrow");
qty.Text = value.ToString();
box.IsVisible = true;
break;
default:
box.IsVisible = false;
break;
}
}
}
}