import SwiftUI

struct ShaderPlayground: View {
    @State private var time: Float = 0.0
    @State private var animationTimer: Timer?

    // Adjustable parameters
    @State private var speed: Float = 0.016
    @State private var scale: Float = 2.0
    @State private var noiseStrength: Float = 0.6
    @State private var hotspotSize: Float = 0.3
    @State private var blendIntensity: Float = 0.6

    // Color parameters (RGB values 0-1)
    @State private var coldColorR: Float = 0.147
    @State private var coldColorG: Float = 0.0
    @State private var coldColorB: Float = 0.180

    @State private var warmColorR: Float = 0.333
    @State private var warmColorG: Float = 0.108
    @State private var warmColorB: Float = 0.247

    @State private var hotColorR: Float = 0.065
    @State private var hotColorG: Float = 0.0
    @State private var hotColorB: Float = 0.180

    var body: some View {
        ZStack {
            // Background
            Constants.Colors.Background.primary
                .ignoresSafeArea()

            VStack(spacing: 0) {
                // Shader preview area
                Rectangle()
                    .fill(Constants.Colors.Background.primary)
                    .visualEffect { content, proxy in
                        content
                            .colorEffect(
                                ShaderLibrary.default.chatThermalPlayground(
                                    .float(time),
                                    .float2(proxy.size),
                                    .float(scale),
                                    .float(noiseStrength),
                                    .float(hotspotSize),
                                    .float(blendIntensity),
                                    .float3(coldColorR, coldColorG, coldColorB),
                                    .float3(warmColorR, warmColorG, warmColorB),
                                    .float3(hotColorR, hotColorG, hotColorB)
                                )
                            )
                    }
                    .frame(maxHeight: .infinity)

                // Controls panel
                ScrollView {
                    VStack(alignment: .leading, spacing: 20) {
                        Text("Shader Playground")
                            .font(Constants.Typography.largeTitle)
                            .foregroundColor(Constants.Colors.Foreground.primary)
                            .padding(.bottom, 8)

                        // Animation controls
                        VStack(alignment: .leading, spacing: 12) {
                            Text("Animation")
                                .font(Constants.Typography.mediumTitle)
                                .foregroundColor(Constants.Colors.Foreground.secondary)

                            SliderControl(
                                label: "Speed",
                                value: $speed,
                                range: 0.001...0.05,
                                format: "%.3f"
                            )
                        }

                        Divider()
                            .background(Constants.Colors.Border.primary)

                        // Pattern controls
                        VStack(alignment: .leading, spacing: 12) {
                            Text("Pattern")
                                .font(Constants.Typography.mediumTitle)
                                .foregroundColor(Constants.Colors.Foreground.secondary)

                            SliderControl(
                                label: "Scale",
                                value: $scale,
                                range: 0.5...10.0,
                                format: "%.1f"
                            )

                            SliderControl(
                                label: "Noise Strength",
                                value: $noiseStrength,
                                range: 0.0...2.0,
                                format: "%.2f"
                            )

                            SliderControl(
                                label: "Hotspot Size",
                                value: $hotspotSize,
                                range: 0.1...0.9,
                                format: "%.2f"
                            )

                            SliderControl(
                                label: "Blend Intensity",
                                value: $blendIntensity,
                                range: 0.0...1.0,
                                format: "%.2f"
                            )
                        }

                        Divider()
                            .background(Constants.Colors.Border.primary)

                        // Color controls
                        VStack(alignment: .leading, spacing: 12) {
                            Text("Cold Color (Dark Purple)")
                                .font(Constants.Typography.mediumTitle)
                                .foregroundColor(Constants.Colors.Foreground.secondary)

                            ColorSliders(r: $coldColorR, g: $coldColorG, b: $coldColorB)
                        }

                        VStack(alignment: .leading, spacing: 12) {
                            Text("Warm Color (Purple-Pink)")
                                .font(Constants.Typography.mediumTitle)
                                .foregroundColor(Constants.Colors.Foreground.secondary)

                            ColorSliders(r: $warmColorR, g: $warmColorG, b: $warmColorB)
                        }

                        VStack(alignment: .leading, spacing: 12) {
                            Text("Hot Color (Deep Purple)")
                                .font(Constants.Typography.mediumTitle)
                                .foregroundColor(Constants.Colors.Foreground.secondary)

                            ColorSliders(r: $hotColorR, g: $hotColorG, b: $hotColorB)
                        }

                        // Reset button
                        Button {
                            resetToDefaults()
                        } label: {
                            Text("Reset to Defaults")
                                .font(Constants.Typography.mediumTitle)
                                .foregroundColor(Constants.Colors.Foreground.primary)
                                .frame(maxWidth: .infinity)
                                .frame(height: 44)
                                .background(
                                    RoundedRectangle(cornerRadius: 12)
                                        .fill(Color.white.opacity(0.1))
                                )
                        }
                        .padding(.top, 8)

                        // Copy values button
                        Button {
                            copyValues()
                        } label: {
                            Text("Copy Values")
                                .font(Constants.Typography.mediumTitle)
                                .foregroundColor(Constants.Colors.Foreground.primary)
                                .frame(maxWidth: .infinity)
                                .frame(height: 44)
                                .background(
                                    RoundedRectangle(cornerRadius: 12)
                                        .fill(Color.white.opacity(0.1))
                                )
                        }
                    }
                    .padding(20)
                }
                .frame(height: 500)
                .background(
                    RoundedRectangle(cornerRadius: 28)
                        .fill(Constants.Colors.Background.secondary)
                )
            }
        }
        .onAppear {
            startAnimation()
        }
        .onDisappear {
            stopAnimation()
        }
    }

    private func startAnimation() {
        stopAnimation()
        animationTimer = Timer.scheduledTimer(withTimeInterval: 1/60.0, repeats: true) { _ in
            DispatchQueue.main.async {
                time += speed
            }
        }
    }

    private func stopAnimation() {
        animationTimer?.invalidate()
        animationTimer = nil
    }

    private func resetToDefaults() {
        speed = 0.016
        scale = 2.0
        noiseStrength = 0.6
        hotspotSize = 0.3
        blendIntensity = 0.6

        coldColorR = 0.147
        coldColorG = 0.0
        coldColorB = 0.180

        warmColorR = 0.333
        warmColorG = 0.108
        warmColorB = 0.247

        hotColorR = 0.065
        hotColorG = 0.0
        hotColorB = 0.180
    }

    private func copyValues() {
        let values = """
        Speed: \(String(format: "%.3f", speed))
        Scale: \(String(format: "%.1f", scale))
        Noise Strength: \(String(format: "%.2f", noiseStrength))
        Hotspot Size: \(String(format: "%.2f", hotspotSize))
        Blend Intensity: \(String(format: "%.2f", blendIntensity))

        Cold Color: (\(String(format: "%.3f", coldColorR)), \(String(format: "%.3f", coldColorG)), \(String(format: "%.3f", coldColorB)))
        Warm Color: (\(String(format: "%.3f", warmColorR)), \(String(format: "%.3f", warmColorG)), \(String(format: "%.3f", warmColorB)))
        Hot Color: (\(String(format: "%.3f", hotColorR)), \(String(format: "%.3f", hotColorG)), \(String(format: "%.3f", hotColorB)))
        """

        UIPasteboard.general.string = values
        print("Values copied to clipboard:")
        print(values)
    }
}

// Reusable slider control
struct SliderControl: View {
    let label: String
    @Binding var value: Float
    let range: ClosedRange<Float>
    let format: String

    var body: some View {
        VStack(alignment: .leading, spacing: 4) {
            HStack {
                Text(label)
                    .font(Constants.Typography.small)
                    .foregroundColor(Constants.Colors.Foreground.tertiary)
                Spacer()
                Text(String(format: format, value))
                    .font(Constants.Typography.small)
                    .foregroundColor(Constants.Colors.Foreground.secondary)
                    .monospacedDigit()
            }

            Slider(value: $value, in: range)
                .tint(Constants.Colors.Foreground.primary)
        }
    }
}

// Color slider group
struct ColorSliders: View {
    @Binding var r: Float
    @Binding var g: Float
    @Binding var b: Float

    var body: some View {
        VStack(spacing: 8) {
            HStack(spacing: 12) {
                // Color preview
                RoundedRectangle(cornerRadius: 8)
                    .fill(Color(red: Double(r), green: Double(g), blue: Double(b)))
                    .frame(width: 40, height: 40)
                    .overlay(
                        RoundedRectangle(cornerRadius: 8)
                            .stroke(Constants.Colors.Border.primary, lineWidth: 1)
                    )

                VStack(spacing: 4) {
                    SliderControl(label: "R", value: $r, range: 0.0...1.0, format: "%.3f")
                    SliderControl(label: "G", value: $g, range: 0.0...1.0, format: "%.3f")
                    SliderControl(label: "B", value: $b, range: 0.0...1.0, format: "%.3f")
                }
            }
        }
    }
}

#Preview {
    ShaderPlayground()
}
