using Knoks.Api.Client;
using Knoks.Api.Nswag.Client;
using Knoks.Test.Integrations.Api.Tools;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Xunit;
using System.Linq;
using Knoks.Api.Nswag.ClientApp;
using Xunit.Abstractions;

namespace Knoks.Test.Integrations.Nswag.Api
{
    public class KnokControllerTests
        : IAsyncLifetime
    {
        public ApiClient ApiClient { get; }
        public HttpClient HttpClient { get; }
        public KnokClient Client { get; }
        private readonly ITestOutputHelper output;
        public KnokControllerTests(ITestOutputHelper output)
        {
            this.output = output;
            HttpClient = ApiTestServer.CreateClient();
            ApiClient = new ApiClient(ApiTestServer.ApiIndetifier, HttpClient);

            Client = new KnokClient(HttpClient);
            Client.BaseUrl = "http://localhost:52281/";
        }
        public async Task InitializeAsync()
        {
            await ApiClient.AuthorizeApiUser("CompanyWallet@knoks.com", "XXXXXX");
        }

        public Task DisposeAsync()
        {
            HttpClient.Dispose();
            return Task.CompletedTask;
        }
        private CreateKnokArgs GetDefault()
        {
            return new CreateKnokArgs() { EntryPriceFrom = 1, EntryPriceTo = 1.005, ExitPriceFrom = 2, ExitPriceTo = 2.005, StopLoss = 0.95, Duration = 1, Currency1 = "BTC", Currency2 = "USDT", ExchangeId = 1 };
        }
        [Theory(DisplayName = "Create knok prices")]
        [InlineData(1.0, 2.0, 2.0, 2.005, 0.95, 1, ApiErrorId.Validation)]
        [InlineData(1.0, 1.005, 2.0, 2.005, 0.95, 1, null)]

        void TryCreateKnok(double? entryPriceFrom, double? entryPriceTo, double? exitPriceFrom, double? exitPriceTo, double? stopLoss, int? duration, ApiErrorId? errorType)
        {
            var cr = new CreateKnokArgs() { Currency1 = "BTC", Currency2 = "USDT", ExchangeId = 1 };

            cr.EntryPriceFrom = entryPriceFrom;
            cr.EntryPriceTo = entryPriceTo;
            cr.ExitPriceFrom = exitPriceFrom;
            cr.ExitPriceTo = exitPriceTo;
            cr.StopLoss = stopLoss;
            cr.Duration = duration;
            var result = Client.CreateKnok(cr);
            if (errorType == null)
            {
                Assert.Null(result?.Error?.Msg);
                Assert.Null(result?.Error?.Errors);
                Assert.Equal(cr.EntryPriceFrom, result.Result.EntryPriceFrom);
                Assert.Equal(cr.EntryPriceTo, result.Result.EntryPriceTo);
                Assert.Equal(cr.ExitPriceFrom, result.Result.ExitPriceFrom);
                Assert.Equal(cr.ExitPriceTo, result.Result.ExitPriceTo);
                Assert.Equal(cr.StopLoss, result.Result.StopLoss);
                Assert.Equal(cr.Duration, result.Result.Duration);
            }
            Assert.Equal(errorType, result?.Error?.Id);
        }
        [Fact(DisplayName = "Create knok tags")]

        void CreateKnokTags()
        {
            var cr = GetDefault();
            cr.Technical = new KnokAnalysis() { Tags = new System.Collections.ObjectModel.ObservableCollection<AnalysisTag>() { new AnalysisTag() { AnalysisTagId = 1 }, new AnalysisTag() { AnalysisTagId = 2 }, new AnalysisTag() { AnalysisTagId = -1, Name = "CustomValue" }, new AnalysisTag() { AnalysisTagId = -1, Name = "Custom Value 2" } } };
            var result = Client.CreateKnok(cr);
            //output.WriteLine("Error = " + result?.Error?.Msg);
            Assert.Null(result?.Error?.Msg);
            Assert.Null(result?.Error?.Id);
            Func<IEnumerable<AnalysisTag>, IEnumerable<string>> process = (list) => list.Select(it => it.AnalysisTagId == -1 ? it.Name : it.AnalysisTagId.ToString()).OrderBy(it => it);
            Assert.Equal(
                process(cr.Technical.Tags),
                process(result.Result.Technical.Tags)
                );
        }
        [Fact(DisplayName = "Get knoks feed")]

        void GetKnoksFeed()
        {
            var cr = GetDefault();
            cr.Technical = new KnokAnalysis() { Tags = new System.Collections.ObjectModel.ObservableCollection<AnalysisTag>() { new AnalysisTag() { AnalysisTagId = 1 }, new AnalysisTag() { AnalysisTagId = 2 }, new AnalysisTag() { AnalysisTagId = -1, Name = "CustomValue" }, new AnalysisTag() { AnalysisTagId = -1, Name = "Custom Value 2" } } };
            var result = Client.CreateKnok(cr);
            Assert.Null(result?.Error?.Msg);
            //output.WriteLine("Error = " + result?.Error?.Msg);
            //var answer = Client.Publish((int)result.Result.KnokId);
            //Assert.Null(answer?.Error?.Msg);
            var feed = Client.FeedGet();
            Assert.Null(feed?.Error?.Msg);
            Assert.NotEmpty(feed.Result.Items);
        }
    }
}
