//
//  SQColorPicker.swift
//  AlloVoisinsSwiftUI
//
//  Created by Victor on 13/11/2024.
//

import SwiftUI

struct SQColorPicker: View {
    @Binding var selectedColor: Color
    @State private var showColorPicker = false
    @State private var isCustomColor = false

    let colors: [Color] = [
        .sqNeutral(),
        .sqGreen(50),
        .sqPink(),
        .sqBlue(50),
        .sqOrange(50),
        .sqGrape(80),
        .sqForest(80)
    ]
    var title: String
    var subtitle: String?

    init(_ title: String, selectedColor: Binding<Color>, subtitle: String? = nil) {
        self.title = title
        self.subtitle = subtitle
        self._selectedColor = selectedColor
        let initialColor = selectedColor.wrappedValue
        _isCustomColor = State(initialValue: !colors.contains(initialColor))
    }

    var body: some View {
        VStack(alignment: .leading) {
            VStack(alignment: .leading) {
                SQText(title, size: 18, font: .bold)
                SQText(subtitle ?? "")
            }
            HStack {
                ForEach(colors, id: \.self) { color in
                    colorDot(color)
                    Spacer()
                }

                SQImage("multicolor", height: 32)
                    .overlay {
                        if isCustomColor {
                            ZStack {
                                Circle()
                                    .foregroundColor(selectedColor)
                                    .frame(width: 20, height: 20)
                                Circle()
                                    .inset(by: -2)
                                    .strokeBorder(Color.white, lineWidth: 3)
                                    .frame(width: 20, height: 20)
                            }
                        }
                    }
                    .onTapGesture {
                        isCustomColor = true
                        showColorPicker.toggle()
                    }
            }
        }
        .sheet(isPresented: $showColorPicker) {
            UIColorPickerViewController_SwiftUI(selectedColor: $selectedColor)
                .background(Color.white)
                .sqNavigationBar(title: "Choisir une couleur personnalisée")
            SQButton("Valider", color: .sqNeutral(100), textColor: .white) {
                showColorPicker.toggle()
            }
        }
    }

    private func colorDot(_ color: Color) -> some View {
        HStack(alignment: .center, spacing: 8) {}
            .padding(4)
            .frame(width: 32, height: 32, alignment: .center)
            .background(color)
            .cornerRadius(32)
            .overlay(
                ZStack {
                    if selectedColor == color && !isCustomColor {
                        Circle()
                            .foregroundColor(color)
                            .frame(width: 20, height: 20)
                        Circle()
                            .inset(by: -2)
                            .strokeBorder(Color.white, lineWidth: 3)
                            .frame(width: 20, height: 20)
                    }
                }
            )
            .onTapGesture {
                selectedColor = color
                isCustomColor = false
            }
    }
}

struct UIColorPickerViewController_SwiftUI: UIViewControllerRepresentable {
    @Binding var selectedColor: Color

    func makeUIViewController(context: Context) -> UIColorPickerViewController {
        let picker = UIColorPickerViewController()
        picker.delegate = context.coordinator
        picker.selectedColor = UIColor(selectedColor)
        return picker
    }

    func updateUIViewController(_ uiViewController: UIColorPickerViewController, context: Context) {
        uiViewController.selectedColor = UIColor(selectedColor)
    }

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

    class Coordinator: NSObject, UIColorPickerViewControllerDelegate {
        var parent: UIColorPickerViewController_SwiftUI

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

        func colorPickerViewControllerDidSelectColor(_ viewController: UIColorPickerViewController) {
            parent.selectedColor = Color(viewController.selectedColor)
        }

        func colorPickerViewControllerDidFinish(_ viewController: UIColorPickerViewController) {
            parent.selectedColor = Color(viewController.selectedColor)
        }
    }
}

// Preview
struct ColorPickerPreview: View {
    @State private var selectedColor: Color = .sqNeutral()

    var body: some View {
        SQColorPicker("Couleur principale", selectedColor: $selectedColor, subtitle: "Choisissez la couleur qui sera appliquée sur vos cartes de visite.")
            .padding()
    }
}

#Preview {
    ColorPickerPreview()
}
