using e_suite.Database.Core.Tables.Glossaries;
using e_suite.Modules.GlossariesManager;
using NUnit.Framework;

namespace GlossariesManager.UnitTests;

[TestFixture]
public class GlossaryCustomFieldValueKeyComparerUnitTests
{
    private GlossaryCustomFieldValueKeyComparer _comparer = null!;

    [SetUp]
    public void Setup()
    {
        _comparer = new GlossaryCustomFieldValueKeyComparer();
    }

    [Test]
    public void Compare_BothInputsNull_ReturnsEqual()
    {
        //Arrange
        //Act
        var result = _comparer.Compare(null, null);
        //Assert
        Assert.That(result, Is.EqualTo(0));
    }

    [Test]
    public void Compare_FirstInputNull_ReturnsLow()
    {
        //Arrange
        //Act
        var result = _comparer.Compare(null, new GlossaryCustomFieldValue());
        //Assert
        Assert.That(result, Is.EqualTo(-1));
    }

    [Test]
    public void Compare_SecondInputNull_ReturnsHigh()
    {
        //Arrange
        //Act
        var result = _comparer.Compare(new GlossaryCustomFieldValue(), null);
        //Assert
        Assert.That(result, Is.EqualTo(1));
    }

    [Test]
    public void CompareGlossaryId_WhenXLower_ReturnsLow()
    {
        //Arrange
        var x = new GlossaryCustomFieldValue
        {
            GlossaryId = 1
        };
        var y = new GlossaryCustomFieldValue
        {
            GlossaryId = 2
        };

        //Act
        var result = _comparer.Compare(x, y);
        
        //Assert
        Assert.That(result, Is.EqualTo(-1));
    }

    [Test]
    public void CompareGlossaryId_WhenYLower_ReturnsHigh()
    {
        //Arrange
        var x = new GlossaryCustomFieldValue
        {
            GlossaryId = 2
        };
        var y = new GlossaryCustomFieldValue
        {
            GlossaryId = 1
        };

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

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

    [Test]
    public void CompareGlossaryId_WhenBothEqual_ReturnsEqual()
    {
        //Arrange
        var x = new GlossaryCustomFieldValue
        {
            GlossaryId = 2
        };
        var y = new GlossaryCustomFieldValue
        {
            GlossaryId = 2
        };

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

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

    [Test]
    public void CompareCustomFieldId_WhenXLower_ReturnsLow()
    {
        //Arrange
        var x = new GlossaryCustomFieldValue
        {
            GlossaryId = 1,
            CustomFieldId = 10
        };
        var y = new GlossaryCustomFieldValue
        {
            GlossaryId = 1,
            CustomFieldId = 20
        };

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

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

    [Test]
    public void CompareCustomFieldId_WhenYLower_ReturnsHigh()
    {
        //Arrange
        var x = new GlossaryCustomFieldValue
        {
            GlossaryId = 1,
            CustomFieldId = 20
        };
        var y = new GlossaryCustomFieldValue
        {
            GlossaryId = 1,
            CustomFieldId = 10
        };

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

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

    [Test]
    public void CompareCustomFieldId_WhenBothSame_ReturnsEqual()
    {
        //Arrange
        var x = new GlossaryCustomFieldValue
        {
            GlossaryId = 1,
            CustomFieldId = 10
        };
        var y = new GlossaryCustomFieldValue
        {
            GlossaryId = 1,
            CustomFieldId = 10
        };

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

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

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

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

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

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

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

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

    [Test]
    public void CompareIndex_WhenBothSame_ReturnsEqual()
    {
        //Arrange
        var x = new GlossaryCustomFieldValue
        {
            GlossaryId = 1,
            CustomFieldId = 10,
            Index = 100
        };
        var y = new GlossaryCustomFieldValue
        {
            GlossaryId = 1,
            CustomFieldId = 10,
            Index = 100
        };

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

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