using Bogus;
using Knoks.Api.Client.Invoker;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace Knoks.Test.Integrations.Api.Tools
{
    public static partial class ApiMocker
    {
        public const string AdminOperatorName = "Admin";
        public const string AdminOperatorPassword = "aaaBBBcccDDDeeeFFFggg";

        public readonly static Faker Faker = new Faker();

        //public static T Value<T>(this IEnumerable<PropertyData> properties, LoanRequestProperty propertyId, bool defaultIfPropertyNotFound = false)
        //{
        //    var items = properties.Where(p => p.PropertyId == propertyId);
        //    var itemValue = (defaultIfPropertyNotFound ? items.SingleOrDefault() : items.Single()).Value;

        //    return (T)Convert.ChangeType(itemValue, typeof(T));
        //}

        public static string DisposableEmail(this Person person, string domainName = "mailinator.com")
        {
            return person.Email.Split('@')[0] + '@' + domainName;
        }
        
        public static CreateUserArgs GetCreateUserArgs(bool isTest = true)
        {
            var person = new Person();
            return new CreateUserArgs
            {
                FirstName = person.FirstName,
                LastName = person.LastName,
                Email = person.DisposableEmail(),
                Phone = person.Phone,
                Password = Guid.NewGuid().ToString("N"),
                Gender = Faker.PickRandom(new[] { "1", "2" }),
                CountryId = 1,
                LanguageId = 1,
            };
        }

        public static CreateOperatorArgs GetCreateOperatorArgs(bool isTest = true)
        {
            var person = new Person();
            return new CreateOperatorArgs
            {
                FirstName = person.FirstName,
                LastName = person.LastName,
                Email = person.DisposableEmail(),
                Phone = person.Phone,
                OperatorName = person.UserName,
                OperatorPassword = Guid.NewGuid().ToString("N"),
                IsActive = true,
                IsReadOnly = false,
                IsTest = isTest,
                OperatorCreationDate = DateTime.UtcNow
            };
        }

        public static IEnumerable<decimal> SplitValue(decimal value, int count)
        {
            if (count <= 0)
                throw new ArgumentException("Count must be greater than zero.", "count");

            var result = new decimal[count];

            decimal runningTotal = 0M;
            for (int i = 0; i < count; i++)
            {
                var remainder = value - runningTotal;
                var share = remainder > 0M ? Math.Max(Math.Round(remainder / ((decimal)(count - i)), 2), .01M) : 0M;
                result[i] = share;
                runningTotal += share;
            }

            if (runningTotal < value) result[count - 1] += value - runningTotal;

            return result;
        }

        public static AddAccountTransactionArgs GetAddAccountTransactionArgs(long userId, long accountId, int operatorId)
        {
            return new AddAccountTransactionArgs
            {
                UserId = userId,
                AccountId= accountId,
                //AccountTransactionTypeId
                Amount = 1,
                AmountUSD =11,
                CurrentBalance =1,
                CurrentBonusBalance =1,
                ExternalReferenceId = Guid.NewGuid().ToString(),
                OperatorId = operatorId,
                Comment = "Test",
                WalletAddress = "TestWalletAddress",
                PendingId = null
            };
        }
        public class AccountTransaction
        {
            public long TransId { get; set; }
        }
    }
}
