using EscapeRoomEngine.Engine.Runtime.Utilities;
using JetBrains.Annotations;
using NaughtyAttributes;
using UnityEngine;

namespace Test_Assets
{
    /// <summary>
    /// This class is used to test the <<see cref="Probability"/> class.
    /// </summary>
    public class ProbabilityTest : MonoBehaviour
    {
        public NormalDistribution distribution = NormalDistribution.Standard;
        public int steps = 24;
        public int n = 1000000;
        public float sample;
        public float percentile = 0.5f;

        [Button]
        [UsedImplicitly]
        public void ShowDistribution()
        {
            var samples = Samples();

            var step = 6f / steps;
            for (var i = -3f; i < 3f; i += step)
            {
                var count = 0;
                for (var j = 0; j < n; j++)
                {
                    var sample = samples[j];
                    if (sample >= i && sample < i + step)
                    {
                        count++;
                    }
                }

                var row = "";
                for (var j = 0; j < 1000f*count/n; j++)
                {
                    row += "*";
                }
                Debug.Log(row);
            }
        }

        [Button]
        [UsedImplicitly]
        public void CalculateMean()
        {
            Debug.Log(Probability.Mean(Samples()));
        }

        [Button]
        [UsedImplicitly]
        public void CalculateStandardDeviation()
        {
            Debug.Log(Probability.StandardDeviation(Samples()));
        }

        [Button]
        [UsedImplicitly]
        public void SamplePercentile()
        {
            Debug.Log(distribution.Cumulative(sample));
        }

        [Button]
        [UsedImplicitly]
        public void InversePercentile()
        {
            Debug.Log(distribution.InverseCumulative(percentile));
        }

        private float[] Samples()
        {
            var samples = new float[n];
            for (var i = 0; i < n; i++)
            {
                samples[i] = distribution.Sample();
            }
            return samples;
        }
    }
}