using Knoks.Core.Entities;
using Knoks.Core.Entities.Args;
using Knoks.Core.Logic.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Knoks.Core.Logic.Managers
{
    public class KnokPriceLogic : IKnokPriceLogic
    {
        public decimal CalculateExpectancy(decimal probabilityOfWinning, decimal avarageWin, decimal avarageLoss)
        {
            return (probabilityOfWinning * avarageWin) + ((1m - probabilityOfWinning) * avarageLoss);
        }

        public decimal CalculateObjectivePrice(decimal expectancy, decimal paidPercent, decimal objectiveWeight, int multiplier)
        {
            return expectancy * paidPercent * objectiveWeight * multiplier;
        }

        //TODO - remove this from the logic to settings
        public int RankPayoff(UserRank value)
        {
            if (value == UserRank.Jedi)
            {
                return 50;
            }
            else if (value == UserRank.Knight)
            {
                return 30;
            }
            else if (value == UserRank.Master)
            {
                return 40;
            }
            else if (value == UserRank.Ranger)
            {
                return 20;
            }
            else if (value == UserRank.Padawan)
            {
                return 10;
            }
            else 
                return 0;
        }

        public decimal CalcAnalysisPrice(string header, string description, IEnumerable<AnalysisTag> tags, decimal analysisMultiplier)
        {
            var analysisPrice = 0m;
            if (!string.IsNullOrEmpty(header))
                analysisPrice++;
            if (!string.IsNullOrEmpty(description))
                analysisPrice++;
            if (tags != null && tags.Count() > 0)
                analysisPrice++;

            return analysisPrice * analysisMultiplier;
        }
        
        public decimal CalculatePotentialProfitPrice(decimal potentialProfit, decimal potentialProfitMultiplier)
        {
            return potentialProfit * potentialProfitMultiplier / 100m;
        }

        public decimal CalculateProfitTimeRatioPrice(decimal potentialProfit, int duration, decimal profitTimeRatioMultiplier)
        {
            return potentialProfit / duration * profitTimeRatioMultiplier / 100m;
        }

        public decimal CalculateProfitLossRatioPrice(decimal potentialProfit, decimal entryFrom,decimal entryTo,decimal stopLoss, decimal profitLossRatioMultiplier)
        {
            var entryMid = (entryFrom + entryTo) / 2m;
            var potentialLoss = (stopLoss - entryMid) / entryMid * -1m * 100m;
            return potentialProfit / potentialLoss * profitLossRatioMultiplier / 100m;
        }

        public decimal CalculateSubjectivePrice(decimal faPrice, decimal taPrice, decimal potentialProfit, decimal ProfTimePrice, decimal ProfLossPrice)
        {
            return faPrice + taPrice + potentialProfit + ProfTimePrice + ProfLossPrice;
        }

        public decimal CalculatePotentialProfit(decimal entryFrom, decimal entryTo, decimal exitFrom, decimal exitTo)
        {
            var entryMid = (entryFrom + entryTo) / 2m;
            var exitMid = (exitFrom + exitTo) / 2m;
            return (exitMid - entryMid) / entryMid * 100m;
        }
    }
}
