import SwiftUI
import Charts

struct LSVView: View {
    @Bindable var state: AppState

    var body: some View {
        VStack(spacing: 0) {
            controlsRow
            Divider()
            GeometryReader { geo in
                if geo.size.width > 700 {
                    HSplitLayout(ratio: 0.55) {
                        voltammogramPlot
                    } trailing: {
                        lsvTable
                    }
                } else {
                    ScrollView {
                        VStack(spacing: 12) {
                            voltammogramPlot.frame(height: 350)
                            lsvTable.frame(height: 300)
                        }
                        .padding()
                    }
                }
            }
        }
    }

    // MARK: - Controls

    private var controlsRow: some View {
        ScrollView(.horizontal, showsIndicators: false) {
            HStack(spacing: 10) {
                LabeledField("Start mV", text: $state.lsvStartV, width: 80)
                LabeledField("Stop mV", text: $state.lsvStopV, width: 80)
                LabeledField("Scan mV/s", text: $state.lsvScanRate, width: 80)

                LabeledPicker("RTIA", selection: $state.lsvRtia, items: LpRtia.allCases) { $0.label }
                    .frame(width: 120)

                Button("Start LSV") { state.startLSV() }
                    .buttonStyle(ActionButtonStyle(color: .green))
            }
            .padding(.horizontal)
            .padding(.vertical, 8)
        }
    }

    // MARK: - Plot

    private var voltammogramPlot: some View {
        Group {
            if state.lsvPoints.isEmpty {
                Text("No data")
                    .foregroundStyle(Color(white: 0.4))
                    .frame(maxWidth: .infinity, maxHeight: .infinity)
                    .background(Color.black.opacity(0.3))
            } else {
                PlotContainer(title: "") {
                    Chart {
                        if let ref = state.lsvRef {
                            ForEach(Array(ref.enumerated()), id: \.offset) { _, pt in
                                LineMark(
                                    x: .value("V", Double(pt.vMv)),
                                    y: .value("I", Double(pt.iUa))
                                )
                                .foregroundStyle(Color.gray.opacity(0.5))
                                .lineStyle(StrokeStyle(lineWidth: 1.5))
                            }
                        }
                        ForEach(Array(state.lsvPoints.enumerated()), id: \.offset) { _, pt in
                            LineMark(
                                x: .value("V", Double(pt.vMv)),
                                y: .value("I", Double(pt.iUa))
                            )
                            .foregroundStyle(Color.yellow)
                            .lineStyle(StrokeStyle(lineWidth: 2))
                        }
                        ForEach(Array(state.lsvPoints.enumerated()), id: \.offset) { _, pt in
                            PointMark(
                                x: .value("V", Double(pt.vMv)),
                                y: .value("I", Double(pt.iUa))
                            )
                            .foregroundStyle(Color.yellow)
                            .symbolSize(16)
                        }
                    }
                    .chartXAxisLabel("V (mV)")
                    .chartYAxisLabel("I (uA)", position: .leading)
                    .chartXAxis {
                        AxisMarks { _ in
                            AxisGridLine(stroke: StrokeStyle(lineWidth: 0.5))
                                .foregroundStyle(Color.gray.opacity(0.3))
                            AxisValueLabel()
                                .font(.caption2)
                                .foregroundStyle(.secondary)
                        }
                    }
                    .chartYAxis {
                        AxisMarks(position: .leading) { _ in
                            AxisGridLine(stroke: StrokeStyle(lineWidth: 0.5))
                                .foregroundStyle(Color.gray.opacity(0.3))
                            AxisValueLabel()
                                .font(.caption2)
                                .foregroundStyle(Color.yellow)
                        }
                    }
                    .padding(8)
                }
            }
        }
        .background(Color.black.opacity(0.3))
        .clipShape(RoundedRectangle(cornerRadius: 8))
    }

    // MARK: - Table

    private var lsvTable: some View {
        MeasurementTable(
            columns: [
                MeasurementColumn(header: "V (mV)", width: 80, alignment: .trailing),
                MeasurementColumn(header: "I (uA)", width: 90, alignment: .trailing),
            ],
            rows: state.lsvPoints,
            cellText: { pt, col in
                switch col {
                case 0: String(format: "%.1f", pt.vMv)
                case 1: String(format: "%.3f", pt.iUa)
                default: ""
                }
            }
        )
    }
}
