//
//  SQPicker.swift
//  AlloVoisinsSwiftUI
//
//  Created by Victor on 18/10/2024.
//

import SwiftUI

struct PickerOption: Identifiable, Hashable {
    let id = UUID()
    let text: String
}

struct SQPicker: View {
    @Binding var selection: PickerOption
    let options: [PickerOption]
    @State private var isPresenting = false

    var body: some View {
        GeometryReader { geometry in
            ZStack {
                // Contenu visuel
                HStack {
                    SQText(selection.text)
                    Spacer()
                    SQIcon(.chevron_down)
                }
                .padding()
                .frame(width: geometry.size.width)
                .overlay(
                    RoundedRectangle(cornerRadius: 8)
                        .inset(by: 0.5)
                        .stroke(Color.sqNeutral(30), lineWidth: 1)
                )

                // Menu invisible mais cliquable
                MenuPicker(selection: $selection, options: options, isPresenting: $isPresenting)
                    .frame(width: geometry.size.width)
            }
        }
        .frame(maxWidth: .infinity)
        .fixedSize(horizontal: false, vertical: true)
        .tint(.sqNeutral(100))
    }

    func triggerPickerMenu() {
        isPresenting = true
    }
}

struct MenuPicker: UIViewRepresentable {
    @Binding var selection: PickerOption
    let options: [PickerOption]
    @Binding var isPresenting: Bool

    func makeUIView(context: Context) -> UIButton {
        let button = UIButton(type: .custom)
        button.setTitle("", for: .normal)
        updateMenu(button)
        return button
    }

    func updateUIView(_ uiView: UIButton, context: Context) {
        updateMenu(uiView)
        if isPresenting {
            uiView.sendActions(for: .menuActionTriggered)
            isPresenting = false
        }
    }

    private func updateMenu(_ button: UIButton) {
        let menu = UIMenu(title: "", children: options.map { option in
            UIAction(title: option.text, state: option == selection ? .on : .off) { _ in
                self.selection = option
            }
        }.reversed())

        button.showsMenuAsPrimaryAction = true
        button.menu = menu
    }

    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }

    class Coordinator: NSObject {
        var parent: MenuPicker

        init(_ parent: MenuPicker) {
            self.parent = parent
        }
    }
}

struct SQPickerPreview: View {
    @State private var selectedDuration: PickerOption
    let durationOptions: [PickerOption]

    init() {
        let options = [
            PickerOption(text: "1 mois"),
            PickerOption(text: "2 mois"),
            PickerOption(text: "3 mois")
        ]
        _selectedDuration = State(initialValue: options[0])
        durationOptions = options
    }

    var body: some View {
        SQPicker(selection: $selectedDuration, options: durationOptions)
            .padding()
    }
}

#Preview {
    SQPickerPreview()
}
