using System;
using System.Dynamic;
using Kairos.Ui.Data.Grid;

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;

namespace UT.Kairos.Ui.Data.Grid
{
    [TestClass]
    public class ColumnHelpersTests
    {
        private readonly string jsonSample = @"
                                                {
                                                    'DateCreation': '2020-02-19T07:16:04.44+01:00',
                                                    'Id': 'f63a02c7-4a37-491f-a13b-4d680429f1c7',
                                                    'Mel': 'brice.fromentin@askyl.com',
                                                    'Nom': 'FROMENTIN',
                                                    'Prenom': 'Brice',
                                                    'Service': {
                                                        'Nom': 'SERVICE LVL 1',
                                                        'Service': {
                                                            'Nom': 'SERVICE LVL 2'
                                                        }
                                                    }
                                                }";
        private readonly ExpandoObject sample;

        public ColumnHelpersTests()
        {
            sample = JsonConvert.DeserializeObject<ExpandoObject>(jsonSample);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void ValueParametersNullNullTest()
        {
            ColumnHelpers.ExtractValue(null, null);
            Assert.Fail("No exception");
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestValueParametersNullNotNull()
        {
            ColumnHelpers.ExtractValue(null, "Value");
            Assert.Fail("No exception.");
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestValueParametersNotNullNull()
        {
            ColumnHelpers.ExtractValue(new ExpandoObject(), null);
            Assert.Fail("No exception.");
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void TestValueParametersEmpty()
        {
            ColumnHelpers.ExtractValue(new ExpandoObject(), "Value");
            Assert.Fail("No exception.");
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void TestValueParametersPathEmpty()
        {
            ColumnHelpers.ExtractValue(sample, "      ");
            Assert.Fail("No exception.");
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void TestValueParametersPathNotExist()
        {
            ColumnHelpers.ExtractValue(sample, "NotExisting");
            Assert.Fail("No exception.");
        }

        [TestMethod]
        public void TestValuLevel1t()
        {
            var result = ColumnHelpers.ExtractValue(sample, "Service.Nom");
            Assert.AreEqual(result, "SERVICE LVL 1");
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void TestWrongPath()
        {
            var result = ColumnHelpers.ExtractValue(sample, "Moon.Nom");
            Assert.Fail();
        }

        [TestMethod]
        public void TestValuLevel12()
        {
            var result = ColumnHelpers.ExtractValue(sample, "Service.Service.Nom");
            Assert.AreEqual(result, "SERVICE LVL 2");
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void TestWrongValuest()
        {
            var result = ColumnHelpers.ExtractValue(sample, "Service.Test");
            Assert.Fail();
        }
    }
}
