using SFML.Graphics;
using SFML.System;
using FluidSim.Core;
using FluidSim.Components;
using System;
using System.Collections.Generic;

namespace FluidSim.Tests
{
    public abstract class Scenario
    {
        protected const float AmbientPressure = 101325f;
        protected const float AmbientTemperature = 300f;
        public float Throttle { get; set; }
        public float Load { get; set; }
        public float Clutch { get; set; }   // 0 = engaged, 1 = fully disengaged (manual lever)
        public Font? Font { get; set; }

        public abstract void Initialize(int sampleRate);
        public abstract float Process();
        public abstract void Draw(RenderWindow target);

        public virtual void ShiftUp() { }
        public virtual void ShiftDown() { }     

        // ---- Dyno curve graph ----
        private const float RpmBinSize = 50f;
        private readonly List<(float powerKw, float torqueNm)> _dynoBins = new();
        private int _lastDynoBin = -1;

        public void ResetDynoCurve()
        {
            _dynoBins.Clear();
            _lastDynoBin = -1;
        }

        protected void UpdateDynoCurve(float rpm, float powerKw, float torqueNm)
        {
            if (rpm <= 0) return;
            int bin = (int)(rpm / RpmBinSize);

            while (_dynoBins.Count <= bin)
                _dynoBins.Add((0f, 0f));

            if (_lastDynoBin >= 0 && bin > _lastDynoBin + 1)
            {
                float lastPower = _dynoBins[_lastDynoBin].powerKw > 0 ? _dynoBins[_lastDynoBin].powerKw : 0f;
                float lastTorque = _dynoBins[_lastDynoBin].torqueNm > 0 ? _dynoBins[_lastDynoBin].torqueNm : 0f;
                for (int b = _lastDynoBin + 1; b < bin; b++)
                {
                    float t = (b - _lastDynoBin) / (float)(bin - _lastDynoBin);
                    float interpPower = lastPower + (powerKw - lastPower) * t;
                    float interpTorque = lastTorque + (torqueNm - lastTorque) * t;
                    if (interpPower > _dynoBins[b].powerKw || _dynoBins[b].powerKw <= 0)
                        _dynoBins[b] = (interpPower, _dynoBins[b].torqueNm);
                    if (interpTorque > _dynoBins[b].torqueNm || _dynoBins[b].torqueNm <= 0)
                        _dynoBins[b] = (_dynoBins[b].powerKw, interpTorque);
                }
            }

            var current = _dynoBins[bin];
            if (powerKw > current.powerKw || current.powerKw <= 0)
                current.powerKw = powerKw;
            if (torqueNm > current.torqueNm || current.torqueNm <= 0)
                current.torqueNm = torqueNm;
            _dynoBins[bin] = current;

            _lastDynoBin = bin;
        }

        protected void DrawDynoCurve(RenderWindow target,
                                     float graphX, float graphY, float graphWidth, float graphHeight,
                                     float currentRpm, float currentPowerKw)
        {
            if (_dynoBins.Count == 0) return;

            float maxPowerKw = 0.01f, maxTorqueNm = 0.01f, maxRpm = 1000f;
            for (int b = 0; b < _dynoBins.Count; b++)
            {
                var bin = _dynoBins[b];
                if (bin.powerKw > 0 || bin.torqueNm > 0)
                {
                    float rpmBin = b * RpmBinSize + RpmBinSize / 2f;
                    if (bin.powerKw > maxPowerKw) maxPowerKw = bin.powerKw;
                    if (bin.torqueNm > maxTorqueNm) maxTorqueNm = bin.torqueNm;
                    if (rpmBin > maxRpm) maxRpm = rpmBin;
                }
            }
            maxPowerKw *= 1.1f;
            maxTorqueNm *= 1.1f;
            maxRpm = MathF.Max(maxRpm * 1.05f, 1000f);

            var bg = new RectangleShape(new Vector2f(graphWidth, graphHeight))
            {
                FillColor = new Color(20, 20, 20, 200),
                Position = new Vector2f(graphX, graphY)
            };
            target.Draw(bg);

            const float leftMargin = 50f, rightMargin = 50f, topMargin = 20f, bottomMargin = 35f;
            float plotX = graphX + leftMargin;
            float plotY = graphY + topMargin;
            float plotW = graphWidth - leftMargin - rightMargin;
            float plotH = graphHeight - topMargin - bottomMargin;

            float xMin = 0f, xMax = maxRpm;
            float yLeftMin = 0f, yLeftMax = maxPowerKw;
            float yRightMin = 0f, yRightMax = maxTorqueNm;

            var powerColor = new Color(0xFF, 0x1B, 0x1B);
            var torqueColor = new Color(0x09, 0x09, 0xFF);
            var gridColor = new Color(50, 50, 50);

            for (int i = 0; i <= 9; i++)
            {
                float t = i / 9f;
                float x = plotX + t * plotW;
                var vLine = new VertexArray(PrimitiveType.Lines, 2);
                vLine[0] = new Vertex(new Vector2f(x, plotY), gridColor);
                vLine[1] = new Vertex(new Vector2f(x, plotY + plotH), gridColor);
                target.Draw(vLine);
            }
            for (int i = 0; i <= 5; i++)
            {
                float t = i / 5f;
                float y = plotY + (1 - t) * plotH;
                var hLine = new VertexArray(PrimitiveType.Lines, 2);
                hLine[0] = new Vertex(new Vector2f(plotX, y), gridColor);
                hLine[1] = new Vertex(new Vector2f(plotX + plotW, y), gridColor);
                target.Draw(hLine);
            }

            DrawLabel(target, "RPM", new Vector2f(graphX + graphWidth / 2 - 12, graphY + graphHeight - 15), Color.White, 12);
            DrawLabel(target, "kW", new Vector2f(graphX + 5, graphY + 2), Color.White, 11);
            DrawLabel(target, "Nm", new Vector2f(graphX + graphWidth - 25, graphY + 2), Color.White, 11);

            for (int i = 0; i <= 5; i++)
            {
                float leftValue = yLeftMin + (yLeftMax - yLeftMin) * i / 5f;
                float rightValue = yRightMin + (yRightMax - yRightMin) * i / 5f;
                float y = plotY + (1 - i / 5f) * plotH;
                DrawLabel(target, $"{leftValue:F1}", new Vector2f(graphX + 2, y - 6), Color.White, 9);
                DrawLabel(target, $"{rightValue:F1}", new Vector2f(graphX + graphWidth - 40, y - 6), Color.White, 9);
            }

            for (int i = 0; i <= 9; i++)
            {
                float value = xMin + (xMax - xMin) * i / 9f;
                float x = plotX + i / 9f * plotW;
                DrawLabel(target, $"{value / 1000f:F1}k", new Vector2f(x - 15, graphY + graphHeight - bottomMargin + 5), Color.White, 9);
            }

            var powerLine = new VertexArray(PrimitiveType.LineStrip);
            bool firstPower = true;
            for (int b = 0; b < _dynoBins.Count; b++)
            {
                float rpmBin = b * RpmBinSize + RpmBinSize / 2f;
                if (rpmBin > xMax) break;
                var bin = _dynoBins[b];
                if (bin.powerKw > 0)
                {
                    float sx = plotX + (rpmBin - xMin) / (xMax - xMin) * plotW;
                    float sy = plotY + (1 - (bin.powerKw - yLeftMin) / (yLeftMax - yLeftMin)) * plotH;
                    if (firstPower) { powerLine.Clear(); firstPower = false; }
                    powerLine.Append(new Vertex(new Vector2f(sx, sy), powerColor));
                }
                else if (!firstPower)
                {
                    target.Draw(powerLine);
                    powerLine.Clear();
                    firstPower = true;
                }
            }
            if (!firstPower) target.Draw(powerLine);

            var torqueLine = new VertexArray(PrimitiveType.LineStrip);
            bool firstTorque = true;
            for (int b = 0; b < _dynoBins.Count; b++)
            {
                float rpmBin = b * RpmBinSize + RpmBinSize / 2f;
                if (rpmBin > xMax) break;
                var bin = _dynoBins[b];
                if (bin.torqueNm > 0)
                {
                    float sx = plotX + (rpmBin - xMin) / (xMax - xMin) * plotW;
                    float sy = plotY + (1 - (bin.torqueNm - yRightMin) / (yRightMax - yRightMin)) * plotH;
                    if (firstTorque) { torqueLine.Clear(); firstTorque = false; }
                    torqueLine.Append(new Vertex(new Vector2f(sx, sy), torqueColor));
                }
                else if (!firstTorque)
                {
                    target.Draw(torqueLine);
                    torqueLine.Clear();
                    firstTorque = true;
                }
            }
            if (!firstTorque) target.Draw(torqueLine);

            if (currentRpm > 0 && currentRpm <= xMax && currentPowerKw > 0)
            {
                float sx = plotX + (currentRpm - xMin) / (xMax - xMin) * plotW;
                float sy = plotY + (1 - (currentPowerKw - yLeftMin) / (yLeftMax - yLeftMin)) * plotH;
                var dot = new CircleShape(2.5f)
                {
                    FillColor = Color.White,
                    Position = new Vector2f(sx - 2.5f, sy - 2.5f)
                };
                target.Draw(dot);
            }
        }

        // ---- Drawing helpers ----
        protected Color PressureColor(float pressurePa)
        {
            float bar = pressurePa / 1e5f;
            byte r, g, b;
            if (bar < 1f)
            {
                float f = Math.Clamp(bar, 0f, 1f);
                r = 0; g = (byte)(255 * f); b = (byte)(255 * (1 - f));
            }
            else
            {
                float f = Math.Min((bar - 1f) / 9f, 1f);
                r = (byte)(255 * f); g = (byte)(255 * (1 - f)); b = 0;
            }
            return new Color(r, g, b);
        }

        protected Color TemperatureColor(float t)
        {
            t = Math.Clamp(t, 0f, 2000f);
            byte r, g, b;
            if (t < AmbientTemperature)
            {
                float f = t / AmbientTemperature;
                r = 0; g = (byte)(255 * f); b = (byte)(255 * (1 - f));
            }
            else
            {
                float f = (t - AmbientTemperature) / (2000f - AmbientTemperature);
                r = (byte)(255 * f); g = (byte)(255 * (1 - f)); b = 0;
            }
            return new Color(r, g, b);
        }

        protected void DrawVolume(RenderWindow target, Volume0D volume,
                                  float centerX, float topY, float width, float height)
        {
            var rect = new RectangleShape(new Vector2f(width, height))
            {
                FillColor = PressureColor(volume.Pressure),
                Position = new Vector2f(centerX - width / 2f, topY)
            };
            target.Draw(rect);
            var border = new RectangleShape(new Vector2f(width, height))
            {
                FillColor = Color.Transparent,
                OutlineColor = Color.White,
                OutlineThickness = 1f,
                Position = rect.Position
            };
            target.Draw(border);
        }

        protected void DrawCylinder(RenderWindow target, EngineCylinder cylinder,
                                    float centerX, float topY, float width, float maxHeight)
        {
            float fraction = cylinder.PistonFraction;
            float currentHeight = maxHeight * fraction;
            var wall = new RectangleShape(new Vector2f(width, maxHeight))
            {
                FillColor = new Color(60, 60, 60),
                Position = new Vector2f(centerX - width / 2f, topY)
            };
            target.Draw(wall);
            var gas = new RectangleShape(new Vector2f(width, currentHeight))
            {
                FillColor = PressureColor(cylinder.Pressure),
                Position = new Vector2f(centerX - width / 2f, topY)
            };
            target.Draw(gas);
            var piston = new RectangleShape(new Vector2f(width, 4f))
            {
                FillColor = Color.White,
                Position = new Vector2f(centerX - width / 2f, topY + currentHeight)
            };
            target.Draw(piston);
            float valveW = 6f, valveH = 10f, valveY = topY + 4f;
            var iv = new RectangleShape(new Vector2f(valveW, valveH))
            {
                FillColor = cylinder.IntakeValveArea > 0f ? Color.Green : Color.Red,
                Position = new Vector2f(centerX - width / 2f - valveW - 2f, valveY)
            };
            target.Draw(iv);
            var ev = new RectangleShape(new Vector2f(valveW, valveH))
            {
                FillColor = cylinder.ExhaustValveArea > 0f ? Color.Green : Color.Red,
                Position = new Vector2f(centerX + width / 2f + 2f, valveY)
            };
            target.Draw(ev);
        }

        protected void DrawPipe(RenderWindow target, PipeSystem pipeSystem, int pipeIndex,
                        float pipeCenterY, float pipeStartX, float pipeEndX,
                        float areaScale = 0f)
        {
            int start = pipeSystem.GetPipeStart(pipeIndex);
            int end = pipeSystem.GetPipeEnd(pipeIndex);
            int n = end - start;
            if (n < 2) return;

            float pipeLen = pipeEndX - pipeStartX;
            float dx = pipeLen / (n - 1);

            var centers = new float[n];
            var radii = new float[n];
            var temps = new float[n];

            for (int i = 0; i < n; i++)
            {
                int cell = start + i;
                float p = pipeSystem.GetCellPressure(cell);
                float rho = pipeSystem.GetCellDensity(cell);
                temps[i] = p / MathF.Max(rho * 287f, 1e-12f);

                if (areaScale > 0f)
                {
                    // Use actual cell area to determine visual radius
                    float area = pipeSystem.GetCellArea(cell);
                    radii[i] = MathF.Sqrt(area / MathF.PI) * areaScale;
                    if (radii[i] < 1f) radii[i] = 1f;
                }
                else
                {
                    // Original pressure‑based radius
                    float dev = MathF.Tanh((p - AmbientPressure) / AmbientPressure * 0.5f);
                    float baseRadius = 25f;   // default visual radius for constant‑area pipes
                    radii[i] = baseRadius * (1f + dev * 2f);
                    if (radii[i] < 2f) radii[i] = 2f;
                }

                centers[i] = pipeStartX + i * dx;
            }

            int segments = 8;
            var va = new VertexArray(PrimitiveType.TriangleStrip);
            for (int i = 0; i < n; i++)
            {
                float x = centers[i], r = radii[i];
                Color col = TemperatureColor(temps[i]);
                va.Append(new Vertex(new Vector2f(x, pipeCenterY - r), col));
                va.Append(new Vertex(new Vector2f(x, pipeCenterY + r), col));
                if (i < n - 1)
                {
                    for (int s = 1; s <= segments; s++)
                    {
                        float t = s / (float)segments;
                        float xi = centers[i] + (centers[i + 1] - centers[i]) * t;
                        float ri = radii[i] + (radii[i + 1] - radii[i]) * t;
                        float Ti = temps[i] + (temps[i + 1] - temps[i]) * t;
                        Color colS = TemperatureColor(Ti);
                        va.Append(new Vertex(new Vector2f(xi, pipeCenterY - ri), colS));
                        va.Append(new Vertex(new Vector2f(xi, pipeCenterY + ri), colS));
                    }
                }
            }
            target.Draw(va);
        }

        protected void DrawLabel(RenderWindow target, string text, Vector2f position, Color fillColor, uint characterSize = 14)
        {
            if (Font == null) return;
            var txt = new Text(Font)
            {
                DisplayedString = text,
                Position = position,
                FillColor = fillColor,
                CharacterSize = characterSize
            };
            target.Draw(txt);
        }
    }
}