import SwiftUI

struct AnimationSettingsView: View {
    @ObservedObject private var settingsController = AppSettingsController.shared

    var body: some View {
        Form {
            Section("Open Animation") {
                springControls(
                    response: settingsController.binding(\.animation.openSpringResponse),
                    damping: settingsController.binding(\.animation.openSpringDamping)
                )
            }

            Section("Close Animation") {
                springControls(
                    response: settingsController.binding(\.animation.closeSpringResponse),
                    damping: settingsController.binding(\.animation.closeSpringDamping)
                )
            }

            Section("Hover Animation") {
                springControls(
                    response: settingsController.binding(\.animation.hoverSpringResponse),
                    damping: settingsController.binding(\.animation.hoverSpringDamping)
                )
            }

            Section("Resize Animation") {
                durationControl(duration: settingsController.binding(\.animation.resizeAnimationDuration))
            }

            Section {
                Button("Reset to Defaults") {
                    settingsController.update {
                        $0.animation = AppSettings.default.animation
                    }
                }
            }
        }
        .formStyle(.grouped)
    }

    @ViewBuilder
    private func springControls(response: Binding<Double>, damping: Binding<Double>) -> some View {
        HStack {
            Text("Response")
            Slider(value: response, in: 0.1...1.5, step: 0.01)
            Text(String(format: "%.2f", response.wrappedValue))
                .monospacedDigit()
                .frame(width: 50)
        }
        HStack {
            Text("Damping")
            Slider(value: damping, in: 0.1...1.5, step: 0.01)
            Text(String(format: "%.2f", damping.wrappedValue))
                .monospacedDigit()
                .frame(width: 50)
        }
    }

    @ViewBuilder
    private func durationControl(duration: Binding<Double>) -> some View {
        HStack {
            Text("Duration")
            Slider(value: duration, in: 0.05...1.5, step: 0.01)
            Text(String(format: "%.2fs", duration.wrappedValue))
                .monospacedDigit()
                .frame(width: 56)
        }
    }
}
