using System;
using System.Collections.Generic;
using System.Linq;

namespace Elwig.Helpers.Billing {
    public class Graph : ICloneable {

        public readonly int Precision;
        public double[] DataX { get; set; }
        public double[] DataY { get; set; }
        public int MinX { get; set; }
        public int MaxX { get; set; }

        public Graph(int precision, int minX, int maxX) {
            Precision = precision;
            MinX = minX;
            MaxX = maxX;
            DataX = Enumerable.Range(minX, maxX - minX + 1).Select(n => (double)n).ToArray();
            DataY = new double[DataX.Length];
        }

        public Graph(Dictionary<double, decimal> data, int precision, int minX, int maxX) {
            Precision = precision;
            MinX = minX;
            MaxX = maxX;
            DataX = Enumerable.Range(minX, maxX - minX + 1).Select(n => (double)n).ToArray();
            DataY = DataX.Select(i => (double)BillingData.GetCurveValueAt(data, i)).ToArray();
        }

        public Graph(double[] values, int precision, int minX, int maxX) {
            Precision = precision;
            MinX = minX;
            MaxX = maxX;
            DataX = Enumerable.Range(MinX, MaxX - MinX + 1).Select(i => (double)i).ToArray();
            DataY = values;
        }

        private Graph(double[] dataX, double[] dataY, int precision, int minX, int maxX) {
            Precision = precision;
            MinX = minX;
            MaxX = maxX;
            DataX = dataX;
            DataY = dataY;
        }

        public double GetOechsleAt(int index) {
            return DataX[index];
        }

        public void SetOechsleAt(int index, double oechsle) {
            DataX[index] = oechsle;
        }

        public void SetPriceAt(int index, double price) {
            DataY[index] = price;
        }

        public double GetPriceAt(int index) {
            return DataY[index];
        }

        public double GetPriceAtOe(double oe) {
            return DataY[Array.IndexOf(DataX, oe)];
        }

        private void FlattenGraph(int begin, int end, double value) {
            for (int i = begin; i <= end; i++) {
                DataY[i] = value;
            }
        }

        public void FlattenGraphLeft(int pointIndex) {
            FlattenGraph(0, pointIndex, DataY[pointIndex]);
        }

        public void FlattenGraphRight(int pointIndex) {
            FlattenGraph(pointIndex, DataY.Length - 1, DataY[pointIndex]);
        }

        private void LinearIncreaseGraph(int begin, int end, double inc) {
            for (int i = begin; i < end; i++) {
                DataY[i + 1] = Math.Round(DataY[i] + inc, Precision);
            }
        }

        public void LinearIncreaseGraphToEnd(int begin, double inc) {
            LinearIncreaseGraph(begin, DataY.Length - 1, inc);
        }

        public void InterpolateGraph(int firstPoint, int secondPoint) {
            int steps = Math.Abs(firstPoint - secondPoint);
            if (firstPoint == -1 || secondPoint == -1 || steps < 2) {
                return;
            }
            var (lowIndex, highIndex) = firstPoint < secondPoint ? (firstPoint, secondPoint) : (secondPoint, firstPoint);
            double step = (DataY[highIndex] - DataY[lowIndex]) / steps;

            for (int i = lowIndex; i < highIndex - 1; i++) {
                DataY[i + 1] = Math.Round(DataY[i] + step, Precision);
            }
        }

        public object Clone() {
            return new Graph((double[])DataX.Clone(), (double[])DataY.Clone(), Precision, MinX, MaxX);
        }
    }
}
