using Knoks.Api.Client.Invoker;
using Knoks.Test.Integrations.Api.Tools;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Xunit;
using static Knoks.Test.Integrations.Api.Tools.ApiMocker;

namespace Knoks.Test.Integrations.Api
{
    public class GeneralControllerTest : ApiTestClient
    {
        //[Theory(DisplayName = "MarketplaceLoans")]
        ////[InlineData("fromInterestRate=1100&toInterestRate=3000&toRiskScoring=30")]
        //[InlineData("")]
        //async Task Marketplace(string queryString)
        //{
        //    await ApiClient.AuthorizeApiConsumer();
        //    var res = await ApiClient.Request<MarketplaceLoanItems>($"Marketplace/Loans/LoanRequestId/1/1/4?{queryString}");
        //    Assert.NotNull(res.ApiResponse.Result);
        //}
        ////http://localhost:52281/v1/Marketplace/Loans/LoanRequestId/1/1/4?fromInterestRate=11000&toInterestRate=30000&toRiskScoring=30


        //[Theory(DisplayName = "Create User")]
        async Task CreateUser(bool isTest)
        {
            await ApiClient.AuthorizeApiConsumer();
            var user = (await ApiClient.InvokeCreateUser(GetCreateUserArgs(isTest))).Object;
            Assert.Equal(isTest, user.IsTest);
        }

        [Fact(DisplayName = "Try create new nser with UserToken")]
        async Task TryCreateNewUserWithUserToken()
        {
            await ApiClient.AuthorizeApiConsumer();
            var createUserArgs = GetCreateUserArgs();
            await ApiClient.InvokeCreateUser(createUserArgs);
            await ApiClient.InvokeSignInUser(createUserArgs.Email, createUserArgs.Password);

            var res = await ApiClient.InvokeCreateUser(new CreateUserArgs());

            Assert.Equal(HttpStatusCode.Forbidden, res.HttpResponse.StatusCode);
        }

        [Fact(DisplayName = "Try Create Exists User")]
        async Task TryCreateExistsUser()
        {
            await ApiClient.AuthorizeApiConsumer();
            var createUserArgs = GetCreateUserArgs();

            var user1 = await ApiClient.InvokeCreateUser(createUserArgs);
            Assert.Null(user1.ApiResponse.Error);

            var user2 = await ApiClient.InvokeCreateUser(createUserArgs);
            Assert.NotNull(user2.ApiResponse.Error);
        }
        
        [Theory(DisplayName = "Create Operator")]
        [InlineData(true)]
        [InlineData(false)]
        async Task CreateOperator(bool isTest)
        {
            await ApiClient.AuthorizeApiConsumer();
            var operatorRes = (await ApiClient.InvokeCreateOperator(GetCreateOperatorArgs(isTest))).Object;
            Assert.Equal(isTest, operatorRes.IsTest);
        }

        [Theory(DisplayName = "Countries")]
        [InlineData("", null)]
        [InlineData("IL", null)]
        [InlineData("IL123", ApiErrorType.Undefined)]
        [InlineData("1", ApiErrorType.Undefined)]
        [InlineData("L", ApiErrorType.Undefined)]
        async Task CountriesTest(string countryCode, ApiErrorType? expected = null)
        {
            await ApiClient.AuthorizeApiConsumer();
            var res = await ApiClient.InvokeCountries(countryCode);
            Assert.Equal(expected, res.ApiResponse.Error?.Id);
        }

        [Theory(DisplayName = "Countries Schema Test")]
        [InlineData("IL")]
        async Task CountrySchemaTest(string countryCode2)
        {
            await ApiClient.AuthorizeApiConsumer();
            var res = await ApiClient.InvokeCountries(countryCode2);

            Assert.Equal(countryCode2, res.Object.Items.First().CountrySymbol);
            Assert.True(res.IsValidJson(@".\Api\JsonSchemas\Cuntries.Schema.json"), "Unexpected JSON result format.");
        }


        [Theory(DisplayName = "ExchangeTest")]
        [InlineData("IL")]
        async Task ExcangeTest(string countryCode2)
        {
            await ApiClient.AuthorizeApiConsumer();
            var res = await ApiClient.InvokeCountries(countryCode2);

            Assert.Equal(countryCode2, res.Object.Items.First().CountrySymbol);
            Assert.True(res.IsValidJson(@".\Api\JsonSchemas\Cuntries.Schema.json"), "Unexpected JSON result format.");
        }
    }
}
