//
//  ControlPanel.swift
//  HelloVisionOS - Niall George Horn - June 2026
//
//  Minimal control panel — wrist end-effector sliders.
//  Head is locked. No counter, no wave demo, no record/play.
//

import SwiftUI 
// Yuck I hate SwiftUI :/ 

struct ControlPanel: View {

    @Environment(AppModel.self) var appModel

    var body: some View {
        @Bindable var model = appModel

        VStack(alignment: .leading, spacing: 12) {

            // Title
            Text("CoreIK Demo")
                .font(.title3).fontWeight(.bold)
                .foregroundStyle(.white)

            // Solver status
            HStack(spacing: 6) {
                Circle()
                    .fill(appModel.bridge.isLoaded ? Color.green : Color.red)
                    .frame(width: 8, height: 8)
                Text(appModel.solverStatus)
                    .font(.caption)
                    .foregroundStyle(Color(white: 0.7))
            }

            Text("BodyFusion_PartialBody3PointRot  ·  \(appModel.totalJoints) joints")
                .font(.system(size: 8, design: .monospaced))
                .foregroundStyle(.cyan.opacity(0.7))

            Divider().overlay(Color.white.opacity(0.2))

            // Left wrist
            WristSection(label: "Left Wrist",
                         x: $model.leftX, y: $model.leftY, z: $model.leftZ) {
                appModel.needsBurst = true
            }

            Divider().overlay(Color.white.opacity(0.2))

            // Right wrist
            WristSection(label: "Right Wrist",
                         x: $model.rightX, y: $model.rightY, z: $model.rightZ) {
                appModel.needsBurst = true
            }

            Divider().overlay(Color.white.opacity(0.2))

            // Export report button
            Button {
                _ = CoreIKEventLog.shared.generateReport()
            } label: {
                Label("Export Report → /tmp/coreik_report.md", systemImage: "doc.text")
                    .font(.system(size: 11, design: .monospaced))
                    .frame(maxWidth: .infinity)
            }
            .buttonStyle(.bordered)
            .tint(.cyan)

        }
        .padding(16)
        .frame(width: 300)
        .background(.black.opacity(0.80), in: RoundedRectangle(cornerRadius: 18))
        .overlay(RoundedRectangle(cornerRadius: 18)
                    .stroke(Color.white.opacity(0.12), lineWidth: 1))
    }
}

// WristSection

private struct WristSection: View {
    let label: String
    @Binding var x: Float
    @Binding var y: Float
    @Binding var z: Float
    let onChange: () -> Void
  
    // Can extend this, it's not normalised. 
    private let range: ClosedRange<Float> = -1.5...1.5

    var body: some View {
        VStack(alignment: .leading, spacing: 6) {
            Text(label)
                .font(.subheadline).fontWeight(.semibold)
                .foregroundStyle(.white)

            SliderRow(label: "X", value: $x, range: range, onChange: onChange)
            SliderRow(label: "Y", value: $y, range: range, onChange: onChange)
            SliderRow(label: "Z", value: $z, range: range, onChange: onChange)
        }
    }
}

// SliderRow

private struct SliderRow: View {
    let label: String
    @Binding var value: Float
    let range: ClosedRange<Float>
    let onChange: () -> Void

    var body: some View {
        HStack {
            Text(label)
                .frame(width: 16)
                .foregroundStyle(Color(white: 0.6))
            Slider(value: $value, in: range)
                .tint(.cyan)
                .onChange(of: value) { _, _ in onChange() }
            Text(String(format: "%+.2f", value))
                .frame(width: 48, alignment: .trailing)
                .foregroundStyle(.white)
                .font(.system(.caption, design: .monospaced))
        }
        .font(.caption)
    }
}
