//
//  SQRadio.swift
//  Sequoia
//
//  Created by Victor on 10/10/2024.
//

import SwiftUI

enum SQRadioOrientation {
    case horizontal
    case vertical
}

struct SQRadio: View {
    let title: String?
    var orientation: SQRadioOrientation = .vertical
    let options: [String]
    @Binding var selectedIndex: Int?

    init(title: String? = nil, orientation: SQRadioOrientation = .vertical, options: [String], selectedIndex: Binding<Int?>) {
        self.title = title
        self.orientation = orientation
        self.options = options
        self._selectedIndex = selectedIndex
    }

    var body: some View {
        VStack(spacing: 16) {
            if let title = title {
                SQText(title, size: 24, font: .bold)
                    .multilineTextAlignment(.center)
            }
            Group {
                if orientation == .horizontal {
                    HorizontalRadioButtons(options: options, selectedIndex: $selectedIndex)
                } else {
                    VStack(alignment: .leading, spacing: 16) {
                        radioButtons
                    }
                }
            }
        }
    }

    private var radioButtons: some View {
        ForEach(Array(options.enumerated()), id: \.offset) { index, option in
            RadioButton(
                title: option,
                isSelected: Binding(
                    get: { selectedIndex == index },
                    set: { _ in selectedIndex = index }
                ),
                orientation: orientation
            )
        }
    }
}

private struct HorizontalRadioButtons: View {
    let options: [String]
    @Binding var selectedIndex: Int?

    var body: some View {
        GeometryReader { geometry in
            HStack(alignment: .top, spacing: 16) {
                ForEach(Array(options.enumerated()), id: \.offset) { index, option in
                    RadioButton(
                        title: option,
                        isSelected: Binding(
                            get: { selectedIndex == index },
                            set: { _ in selectedIndex = index }
                        ),
                        orientation: .horizontal
                    )
                    .frame(width: (geometry.size.width - CGFloat(options.count - 1) * 16) / CGFloat(options.count))
                }
            }
        }
    }
}

private struct RadioButton: View {
    let title: String
    @Binding var isSelected: Bool
    let orientation: SQRadioOrientation

    var body: some View {
        Group {
            if orientation == .horizontal {
                VStack(spacing: 8) {
                    radioCircle
                    SQText(title)
                        .multilineTextAlignment(.center)
                }
            } else {
                HStack(spacing: 8) {
                    radioCircle
                    SQText(title)
                }
            }
        }
        .contentShape(Rectangle())  // Make the entire area tappable
        .onTapGesture {
            isSelected.toggle()
        }
    }

    private var radioCircle: some View {
        ZStack {
            Circle()
                .stroke(isSelected ? Color.sqNeutral(100) : Color.sqNeutral(30), lineWidth: 1)
                .frame(width: 20, height: 20)

            if isSelected {
                Circle()
                    .inset(by: 3)
                    .stroke(Color.sqNeutral(100), lineWidth: 6)
                    .frame(width: 20, height: 20)
            }
        }
    }
}

#Preview {
    VStack(spacing: 32) {
        SQRadio(title: "Vertical", options: ["Option 1", "Option 2", "Option 3"], selectedIndex: .constant(0))
        SQRadio(title: "Horizontal", orientation: .horizontal, options: ["Option 1", "Option 2", "Option 3"], selectedIndex: .constant(2))
    }
}
