using Elwig.Helpers;
using System.Windows.Controls;

namespace Tests.UnitTests.HelperTests {
    [TestFixture]
    [Apartment(ApartmentState.STA)]
    public class ValidatorTest {

        private static TextBox TB(string value, int caret = 0) {
            return new() {
                Text = value,
                CaretIndex = caret,
            };
        }

        [Test]
        public void Test_CheckInteger_Simple() {
            Assert.Multiple(() => {
                Assert.That(Validator.CheckInteger(TB(""), true).IsValid, Is.False);
                Assert.That(Validator.CheckInteger(TB(""), false).IsValid, Is.True);
                Assert.That(Validator.CheckInteger(TB("123"), true).IsValid, Is.True);
                Assert.That(Validator.CheckInteger(TB("456"), false).IsValid, Is.True);
                Assert.That(Validator.CheckInteger(TB("1234"), false, 4).IsValid, Is.True);
                Assert.That(Validator.CheckInteger(TB("4567"), false, 3).IsValid, Is.True);
            });
        }

        [Test]
        public void Test_CheckInteger_Caret() {
            var tb = TB("1a2b3c", 2);
            Assert.Multiple(() => {
                Assert.That(Validator.CheckInteger(tb, true).IsValid, Is.True);
                Assert.That(tb.Text, Is.EqualTo("123"));
                Assert.That(tb.CaretIndex, Is.EqualTo(1));
            });
        }

        [Test]
        public void Test_CheckInteger_MaxLen() {
            var tb = TB("1a2b3c4d5e", 4);
            Assert.Multiple(() => {
                Assert.That(Validator.CheckInteger(tb, true, 3).IsValid, Is.True);
                Assert.That(tb.Text, Is.EqualTo("123"));
                Assert.That(tb.CaretIndex, Is.EqualTo(2));
            });
        }

        [Test]
        public void Test_CheckPhoneNumber_Simple() {
            Assert.Multiple(() => {
                Assert.That(Validator.CheckPhoneNumber(TB(""), true).IsValid, Is.False);
                Assert.That(Validator.CheckPhoneNumber(TB("+43"), false).IsValid, Is.False);
                Assert.That(Validator.CheckPhoneNumber(TB("066412345678"), true).IsValid, Is.True);
                Assert.That(Validator.CheckPhoneNumber(TB("0ab66412cd345678"), true).IsValid, Is.True);
            });
        }

        [Test]
        public void Test_CheckPhoneNumber_Format_1() {
            var tb = TB("066412345678", 5);
            Assert.Multiple(() => {
                Assert.That(Validator.CheckPhoneNumber(tb, true).IsValid, Is.True);
                Assert.That(tb.Text, Is.EqualTo("+43 664 12345678"));
                Assert.That(tb.CaretIndex, Is.EqualTo(9));
            });
        }

        [Test]
        public void Test_CheckPhoneNumber_Format_2() {
            var tb = TB("0a2574b1c2d34..", 7);
            Assert.Multiple(() => {
                Assert.That(Validator.CheckPhoneNumber(tb, true).IsValid, Is.True);
                Assert.That(tb.Text, Is.EqualTo("+43 2574 1234"));
                Assert.That(tb.CaretIndex, Is.EqualTo(8));
            });
        }

        [Test]
        public void Test_CheckEmailAddress_Simple() {
            Assert.Multiple(() => {
                Assert.That(Validator.CheckEmailAddress(TB(""), true).IsValid, Is.False);
                Assert.That(Validator.CheckEmailAddress(TB("name"), false).IsValid, Is.False);
                Assert.That(Validator.CheckEmailAddress(TB("@"), false).IsValid, Is.False);
                Assert.That(Validator.CheckEmailAddress(TB("@test.com"), false).IsValid, Is.False);
                Assert.That(Validator.CheckEmailAddress(TB("name@a"), true).IsValid, Is.False);
                Assert.That(Validator.CheckEmailAddress(TB("name@a.com."), true).IsValid, Is.False);
                Assert.That(Validator.CheckEmailAddress(TB("name@a.com.a"), true).IsValid, Is.False);
                Assert.That(Validator.CheckEmailAddress(TB("name@a.com"), true).IsValid, Is.True);
                Assert.That(Validator.CheckEmailAddress(TB("my.name@hello#.com"), true).IsValid, Is.True);
            });
        }

        [Test]
        public void Test_CheckEmailAddress_Format_1() {
            var tb = TB("my . name . is @heinz#.com", 17);
            Assert.Multiple(() => {
                Assert.That(Validator.CheckEmailAddress(tb, true).IsValid, Is.True);
                Assert.That(tb.Text, Is.EqualTo("my.name.is@heinz.com"));
                Assert.That(tb.CaretIndex, Is.EqualTo(12));
            });
        }

        [Test]
        public void Test_CheckEmailAddress_Format_2() {
            var tb = TB("sabine.müsterfrau@heinz#.com.b", 30);
            Assert.Multiple(() => {
                Assert.That(Validator.CheckEmailAddress(tb, true).IsValid, Is.False);
                Assert.That(tb.Text, Is.EqualTo("sabine.müsterfrau@heinz.com.b"));
                Assert.That(tb.CaretIndex, Is.EqualTo(29));
            });
        }
    }
}
