using e_suite.Database.Core.Tables.Forms;
using e_suite.Modules.FormsManager;
using NUnit.Framework;

namespace e_suite.Modules.FormsManagerUnitTests.FormFieldValueKeyComparerUnitTests;

[TestFixture]
public class CompareUnitTests
{
    private FormFieldValueKeyComparer _formFieldValueKeyComparer = null!;

    [SetUp]
    public void SetUp()
    {
        _formFieldValueKeyComparer = new FormFieldValueKeyComparer();
    }

    [Test]
    public void Compare_WhenBothNull_ReturnsEqual()
    {
        //Arrange

        //Act
        var result = _formFieldValueKeyComparer.Compare(null, null);
        
        //Assert
        Assert.That(result, Is.EqualTo(0));
    }

    [Test]
    public void Compare_WhenXNull_ReturnsLower()
    {
        //Arrange

        //Act
        var result = _formFieldValueKeyComparer.Compare(null, new FormFieldInstance());

        //Assert
        Assert.That(result, Is.EqualTo(-1));
    }

    [Test]
    public void Compare_WhenYNull_ReturnsHigher()
    {
        //Arrange

        //Act
        var result = _formFieldValueKeyComparer.Compare(new FormFieldInstance(), null);

        //Assert
        Assert.That(result, Is.EqualTo(1));
    }

    [Test]
    public void Compare_WhenXFormInstanceIdLower_ReturnsLower()
    {
        //Arrange
        var x = new FormFieldInstance
        {
            FormInstanceId = 1
        };
        var y = new FormFieldInstance
        {
            FormInstanceId = 2
        };

        //Act
        var result = _formFieldValueKeyComparer.Compare(x,y);

        //Assert
        Assert.That(result, Is.EqualTo(-1));
    }

    [Test]
    public void Compare_WhenXFormInstanceIdHigher_ReturnsHigher()
    {
        //Arrange
        var x = new FormFieldInstance
        {
            FormInstanceId = 2
        };
        var y = new FormFieldInstance
        {
            FormInstanceId = 1
        };

        //Act
        var result = _formFieldValueKeyComparer.Compare(x, y);

        //Assert
        Assert.That(result, Is.EqualTo(1));
    }

    [Test]
    public void Compare_WhenXCustomFieldIdLower_ReturnsLower()
    {
        //Arrange
        var x = new FormFieldInstance
        {
            FormInstanceId = 1,
            CustomFieldId = 10
        };
        var y = new FormFieldInstance
        {
            FormInstanceId = 1,
            CustomFieldId = 20
        };

        //Act
        var result = _formFieldValueKeyComparer.Compare(x, y);

        //Assert
        Assert.That(result, Is.EqualTo(-1));
    }

    [Test]
    public void Compare_WhenXCustomFieldIdHigher_ReturnsHigher()
    {
        //Arrange
        var x = new FormFieldInstance
        {
            FormInstanceId = 1,
            CustomFieldId = 20
        };
        var y = new FormFieldInstance
        {
            FormInstanceId = 1,
            CustomFieldId = 10
        };

        //Act
        var result = _formFieldValueKeyComparer.Compare(x, y);

        //Assert
        Assert.That(result, Is.EqualTo(1));
    }

    [Test]
    public void Compare_WhenXIndexLower_ReturnsLower()
    {
        //Arrange
        var x = new FormFieldInstance
        {
            FormInstanceId = 1,
            CustomFieldId = 10,
            Index = 100
        };
        var y = new FormFieldInstance
        {
            FormInstanceId = 1,
            CustomFieldId = 10,
            Index = 200
        };

        //Act
        var result = _formFieldValueKeyComparer.Compare(x, y);

        //Assert
        Assert.That(result, Is.EqualTo(-1));
    }

    [Test]
    public void Compare_WhenXIndexHigher_ReturnsHigher()
    {
        //Arrange
        var x = new FormFieldInstance
        {
            FormInstanceId = 1,
            CustomFieldId = 10,
            Index = 200
        };
        var y = new FormFieldInstance
        {
            FormInstanceId = 1,
            CustomFieldId = 10,
            Index = 100
        };

        //Act
        var result = _formFieldValueKeyComparer.Compare(x, y);

        //Assert
        Assert.That(result, Is.EqualTo(1));
    }

    [Test]
    public void Compare_WhenXAllEqual_ReturnsEqual()
    {
        //Arrange
        var x = new FormFieldInstance
        {
            FormInstanceId = 1,
            CustomFieldId = 10,
            Index = 100
        };
        var y = new FormFieldInstance
        {
            FormInstanceId = 1,
            CustomFieldId = 10,
            Index = 100
        };

        //Act
        var result = _formFieldValueKeyComparer.Compare(x, y);

        //Assert
        Assert.That(result, Is.EqualTo(0));
    }
}