using Avalonia.Collections;
using Avalonia.Media;
using Avalonia.Media.Immutable;
using Xunit;

namespace Avalonia.Base.UnitTests.Media
{
    public class PenTests
    {
        [Fact]
        public void Changing_Thickness_Raises_Invalidated()
        {
            var target = new Pen();
            var raised = false;

            target.Invalidated += (s, e) => raised = true;
            target.Thickness = 18;

            Assert.True(raised);
        }

        [Fact]
        public void Changing_Brush_Color_Raises_Invalidated()
        {
            var brush = new SolidColorBrush(Colors.Red);
            var target = new Pen { Brush = brush };
            var raised = false;

            target.Invalidated += (s, e) => raised = true;
            brush.Color = Colors.Green;

            Assert.True(raised);
        }

        [Fact]
        public void Changing_DashStyle_Dashes_Raises_Invalidated()
        {
            var dashes = new DashStyle();
            var target = new Pen { DashStyle = dashes };
            var raised = false;

            target.Invalidated += (s, e) => raised = true;
            dashes.Dashes = new AvaloniaList<double> { 0.1, 0.2 };

            Assert.True(raised);
        }

        [Fact]
        public void Adding_DashStyle_Dashes_Raises_Invalidated()
        {
            var dashes = new DashStyle();
            var target = new Pen { DashStyle = dashes };
            var raised = false;

            target.Invalidated += (s, e) => raised = true;
            dashes.Dashes.Add(0.3);

            Assert.True(raised);
        }

        [Fact]
        public void Equality_Is_Implemented_Between_Immutable_And_Mutable_Pens()
        {
            var brush = new ImmutableSolidColorBrush(Colors.Red);
            var target1 = new ImmutablePen(
                brush: brush,
                thickness: 2,
                dashStyle: (ImmutableDashStyle)DashStyle.Dash,
                lineCap: PenLineCap.Round,
                lineJoin: PenLineJoin.Round,
                miterLimit: 21);
            var target2 = new Pen(
                brush: brush,
                thickness: 2,
                dashStyle: DashStyle.Dash,
                lineCap: PenLineCap.Round,
                lineJoin: PenLineJoin.Round,
                miterLimit: 21);

            Assert.True(Equals(target1, target2));
        }

        [Fact]
        public void Equality_Is_Implemented_Between_Mutable_And_Immutable_DashStyles()
        {
            var brush = new ImmutableSolidColorBrush(Colors.Red);
            var target1 = new ImmutablePen(
                brush: brush,
                thickness: 2,
                dashStyle: new ImmutableDashStyle(new[] { 0.1, 0.2 }, 5),
                lineCap: PenLineCap.Round,
                lineJoin: PenLineJoin.Round,
                miterLimit: 21);
            var target2 = new Pen(
                brush: brush,
                thickness: 2,
                dashStyle: new DashStyle(new[] { 0.1, 0.2 }, 5),
                lineCap: PenLineCap.Round,
                lineJoin: PenLineJoin.Round,
                miterLimit: 21);

            Assert.True(Equals(target1, target2));
        }
    }
}
