using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.Profiling;

namespace Ingvar.LiveWatch.Editor
{
    [Serializable]
    public class VariableGraphGUI
    {
        public int RowIndex { get; set; }
        public WatchVariable Variable { get; set; }
        public List<int> IndicesToDisplay { get; set; } = new();
        public int StartIndex { get; set; }
        public int IndicesCount { get; set; }
        public double MinValue { get; set; }
        public double MaxValue { get; set; }
        public float ValueColumnWidth { get; set; }
        public Color BackgroundColor { get; set; }

        [SerializeField] private TextureDrawGUI _textureDrawGUI = new();
        
        private TextureDrawGUI.GraphPointInfo[] _graphPoints = new TextureDrawGUI.GraphPointInfo[1000];

        public void DrawValues(Rect rect)
        {
            var isEmptyVariable = Variable.Values.Count == 0 || Variable.Values.OriginalKeys.Count == 1 && Variable.Values.IsEmptyAt(0);
            
            if (isEmptyVariable )
            {
                return;
            }
            
            var graphRect = isEmptyVariable ? rect : rect.Extrude(ExtrudeFlags.Top | ExtrudeFlags.Bottom, -3);
            var valueColumnWidthInt = Mathf.RoundToInt(ValueColumnWidth);
            var graphPointsCount = 0;
            
            foreach (var index in IndicesToDisplay)
            {
                var noValue = Variable.Values.IsEmptyAt(index);
                
                
                var filColor = Colors.GraphFill;
                var topLineColor = Colors.GraphLine;
                var bottomLineColor = Colors.GraphFill;
                var pixelHeight = Mathf.RoundToInt(graphRect.height);


                if (!noValue)
                {
                    var value = Variable.GetValueNumber(index);
                    var normValue = MaxValue - MinValue < 0.000001 ? 1 : (value - MinValue) / (MaxValue - MinValue);
                    pixelHeight = Mathf.RoundToInt(graphRect.height * (float)normValue);
                }
                {
                    bottomLineColor = Colors.ExtraTextLineGraph;
                }
                
                for (var i = 0; i < valueColumnWidthInt; i++)
                {
                    var isDivider = ValueColumnWidth > 1 && index != IndicesToDisplay[0] && i == 0;

                    var point = new TextureDrawGUI.GraphPointInfo()
                    {
                        IsEmpty = noValue                         ,
                        WithLine = !noValue,
                        PixelHeight = pixelHeight,
                        TopLineColor = topLineColor,
                        BottomLineColor = bottomLineColor,
                        FillColor = isDivider ? filColor + new Color32(10, 10, 10, 10) : filColor
                    };

                    _graphPoints[graphPointsCount++] = point;

                    if (graphPointsCount == _graphPoints.Length)
                        Array.Resize(ref _graphPoints, _graphPoints.Length * 2);
                }
            }

            _textureDrawGUI.Prepare(graphRect);
            _textureDrawGUI.DrawTestGraph(
                BackgroundColor, 
                _graphPoints, 
                graphPointsCount);
            _textureDrawGUI.DrawResult();
        }
        
    }
}