//
//  ExpandableControlsPanel.swift
//  CameraTester
//
//  Created by Matt Bruce on 1/2/26.
//

import SwiftUI
import Bedrock
import MijickCamera

// MARK: - Expandable Controls Panel

struct ExpandableControlsPanel: View {
    @Binding var isExpanded: Bool

    // Collapsed state info
    let hasActiveSettings: Bool
    let activeSettingsIcons: [String]

    // Control properties
    let flashMode: CameraFlashMode
    let flashIcon: String
    let onFlashTap: () -> Void
    let isFlashSyncedWithRingLight: Bool
    let onFlashSyncTap: () -> Void

    let hdrMode: CameraHDRMode
    let hdrIcon: String
    let onHDRTap: () -> Void

    let isGridVisible: Bool
    let gridIcon: String
    let onGridTap: () -> Void

    let photoQuality: PhotoQuality
    let onQualityTap: () -> Void

    let isCenterStageAvailable: Bool
    let isCenterStageEnabled: Bool
    let onCenterStageTap: () -> Void

    let isFrontCamera: Bool
    let onFlipCameraTap: () -> Void

    let isRingLightEnabled: Bool
    let onRingLightTap: () -> Void

    let ringLightColor: Color
    let onRingLightColorTap: () -> Void

    let ringLightSize: CGFloat
    let onRingLightSizeTap: () -> Void

    let ringLightOpacity: Double
    let onRingLightOpacityTap: () -> Void

    // Layout constants
    private let collapsedHeight: CGFloat = 36
    private let iconSize: CGFloat = 16
    private let maxVisibleIcons: Int = 3

    // Grid layout
    private let columns = [
        GridItem(.flexible()),
        GridItem(.flexible()),
        GridItem(.flexible())
    ]

    var body: some View {
        VStack(spacing: 0) {
            // Header row (always visible) - acts as the collapsed pill
            Button(action: { isExpanded.toggle() }) {
                HStack(spacing: Design.Spacing.small) {
                    // Chevron that rotates
                    Image(systemName: "chevron.down")
                        .font(.system(size: iconSize, weight: .semibold))
                        .foregroundStyle(Color.white)
                        .rotationEffect(.degrees(isExpanded ? 180 : 0))

                    // Show active setting icons when collapsed
                    if !isExpanded && hasActiveSettings {
                        HStack(spacing: Design.Spacing.xSmall) {
                            ForEach(activeSettingsIcons.prefix(maxVisibleIcons), id: \.self) { icon in
                                Image(systemName: icon)
                                    .font(.system(size: iconSize, weight: .medium))
                                    .foregroundStyle(Color.yellow)
                            }
                        }
                    }

                    if isExpanded {
                        Spacer()
                    }
                }
                .frame(height: collapsedHeight)
                .frame(maxWidth: isExpanded ? .infinity : nil)
                .padding(.horizontal, Design.Spacing.medium)
            }
            .accessibilityLabel("Camera controls")
            .accessibilityHint(isExpanded ? "Tap to collapse settings" : "Tap to expand camera settings")

            // Expandable content
            if isExpanded {
                ScrollView {
                    VStack(spacing: Design.Spacing.large) {
                        // Camera Controls Section
                        VStack(spacing: Design.Spacing.medium) {
                            // Section header
                            Text("Camera Controls")
                                .font(.system(size: 14, weight: .semibold))
                                .foregroundStyle(Color.white.opacity(0.8))
                                .frame(maxWidth: .infinity, alignment: .leading)

                            // Controls grid
                            LazyVGrid(columns: columns, spacing: Design.Spacing.medium) {
                                // Flash
                                ExpandedControlItem(
                                    icon: flashIcon,
                                    label: flashLabel,
                                    isActive: flashMode != .off,
                                    action: onFlashTap
                                )

                                // Flash Sync
                                ExpandedControlItem(
                                    icon: isFlashSyncedWithRingLight ? "link" : "link.slash",
                                    label: "SYNC",
                                    isActive: isFlashSyncedWithRingLight,
                                    action: onFlashSyncTap
                                )

                                // HDR
                                ExpandedControlItem(
                                    icon: hdrIcon,
                                    label: hdrLabel,
                                    isActive: hdrMode != .off,
                                    action: onHDRTap
                                )

                                // Grid
                                ExpandedControlItem(
                                    icon: gridIcon,
                                    label: "GRID",
                                    isActive: isGridVisible,
                                    action: onGridTap
                                )

                                // Quality
                                ExpandedControlItem(
                                    icon: photoQuality.icon,
                                    label: photoQuality.rawValue.uppercased(),
                                    isActive: false,
                                    action: onQualityTap
                                )

                                // Center Stage (if available)
                                if isCenterStageAvailable {
                                    ExpandedControlItem(
                                        icon: isCenterStageEnabled ? "person.crop.rectangle.fill" : "person.crop.rectangle",
                                        label: "STAGE",
                                        isActive: isCenterStageEnabled,
                                        action: onCenterStageTap
                                    )
                                }

                                // Flip Camera
                                ExpandedControlItem(
                                    icon: "arrow.triangle.2.circlepath.camera",
                                    label: isFrontCamera ? "FRONT" : "BACK",
                                    isActive: false,
                                    action: onFlipCameraTap
                                )
                            }
                        }

                        // Ring Light Section
                        VStack(spacing: Design.Spacing.medium) {
                            // Section header
                            Text("Ring Light")
                                .font(.system(size: 14, weight: .semibold))
                                .foregroundStyle(Color.white.opacity(0.8))
                                .frame(maxWidth: .infinity, alignment: .leading)

                            // Ring light controls grid
                            LazyVGrid(columns: columns, spacing: Design.Spacing.medium) {
                                // Ring Light Enable/Disable
                                ExpandedControlItem(
                                    icon: isRingLightEnabled ? "circle.fill" : "circle",
                                    label: "ENABLE",
                                    isActive: isRingLightEnabled,
                                    action: onRingLightTap
                                )

                                // Ring Color
                                ExpandedControlItem(
                                    icon: "circle.fill",
                                    label: "COLOR",
                                    isActive: false,
                                    action: onRingLightColorTap
                                )
                                .foregroundStyle(ringLightColor)

                                // Ring Size
                                ExpandedControlItem(
                                    icon: "circle",
                                    label: "SIZE",
                                    isActive: false,
                                    action: onRingLightSizeTap
                                )

                                // Ring Brightness
                                ExpandedControlItem(
                                    icon: "circle.fill",
                                    label: "BRIGHT",
                                    isActive: false,
                                    action: onRingLightOpacityTap
                                )
                                .foregroundStyle(Color.white.opacity(ringLightOpacity))
                            }
                        }
                    }
                    .padding(.top, Design.Spacing.small)
                    .padding(.bottom, Design.Spacing.medium)
                    .padding(.horizontal, Design.Spacing.small)
                }
                .frame(maxHeight: 400) // Limit max height to prevent excessive scrolling
                .scrollIndicators(.hidden) // Hide scroll indicators for cleaner look
            }
        }
        .padding(.horizontal, Design.Spacing.small)
        .background(
            RoundedRectangle(cornerRadius: isExpanded ? Design.CornerRadius.large : collapsedHeight / 2)
                .fill(Color.black.opacity(isExpanded ? Design.Opacity.strong : Design.Opacity.medium))
        )
        .animation(.easeInOut(duration: 0.25), value: isExpanded)
    }

    private var flashLabel: String {
        switch flashMode {
        case .off: return "FLASH"
        case .auto: return "AUTO"
        case .on: return "ON"
        }
    }

    private var hdrLabel: String {
        switch hdrMode {
        case .off: return "HDR"
        case .auto: return "AUTO"
        case .on: return "ON"
        }
    }
}
