// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Globalization;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Xunit;

namespace Microsoft.AspNetCore.Mvc.Internal
{
    public class ElementalValueProviderTest
    {
        [Theory]
        [InlineData("MyProperty", "MyProperty")]
        [InlineData("MyProperty.SubProperty", "MyProperty")]
        [InlineData("MyProperty[0]", "MyProperty")]
        public void ContainsPrefix_ReturnsTrue_IfElementNameStartsWithPrefix(
            string elementName, 
            string prefix)
        {
            // Arrange
            var culture = new CultureInfo("en-US");
            var elementalValueProvider = new ElementalValueProvider(
                elementName,
                "hi",
                culture);

            // Act
            var containsPrefix = elementalValueProvider.ContainsPrefix(prefix);

            // Assert
            Assert.True(containsPrefix);
        }

        [Theory]
        [InlineData("MyProperty", "MyProperty1")]
        [InlineData("MyPropertyTest", "MyProperty")]
        [InlineData("Random", "MyProperty")]
        public void ContainsPrefix_ReturnsFalse_IfElementCannotSpecifyValuesForPrefix(
            string elementName, 
            string prefix)
        {
            // Arrange
            var culture = new CultureInfo("en-US");
            var elementalValueProvider = new ElementalValueProvider(
                elementName,
                "hi",
                culture);

            // Act
            var containsPrefix = elementalValueProvider.ContainsPrefix(prefix);

            // Assert
            Assert.False(containsPrefix);
        }

        [Fact]
        public void GetValue_NameDoesNotMatch_ReturnsEmptyResult()
        {
            // Arrange
            var culture = new CultureInfo("fr-FR");
            var valueProvider = new ElementalValueProvider("foo", "hi", culture);

            // Act
            var result = valueProvider.GetValue("bar");

            // Assert
            Assert.Equal(ValueProviderResult.None, result);
        }

        [Theory]
        [InlineData("foo")]
        [InlineData("FOO")]
        [InlineData("FoO")]
        public void GetValue_NameMatches_ReturnsValueProviderResult(string name)
        {
            // Arrange
            var culture = new CultureInfo("fr-FR");
            var valueProvider = new ElementalValueProvider("foo", "hi", culture);

            // Act
            var result =  valueProvider.GetValue(name);

            // Assert
            Assert.Equal("hi", (string)result);
            Assert.Equal(culture, result.Culture);
        }
    }
}
