//
//  NewProjectForm.swift
//  OnCue
//
//  Created by Pradyun Setti on 22/06/2024.
//

import SwiftUI

struct NewProjectForm: View {
    
    @State private var newProjectName = ""
    @State private var chosenColor: Color = .blue
    @Environment(\.dismiss) var dismiss
    
    var colors: [Color] = [.red, .orange, .yellow, .green, .blue, .indigo]
    
    let completionHandler: (String, String) -> Void
    
    var body: some View {
        NavigationView {
            Form {
                TextField("Project Name", text: $newProjectName)
                Section(header: Text("Project Colour")) {
                    HStack {
                        ForEach(colors, id: \.self) { clr in
                            ColorChooserCircle(color: clr)
                                .padding(.vertical)
                                .onTapGesture {
                                    self.chosenColor = clr
                                    print(clr.toHex()!)
                                }
                        }
                    }
                }
            }
            .navigationTitle("Add New Project").foregroundStyle(chosenColor)
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .topBarLeading) {
                    Button("Cancel", role: .cancel) { dismiss() }
                        .foregroundStyle(.red)
                }
                ToolbarItem(placement: .topBarTrailing) {
                    Button("Done") {
                        completionHandler(newProjectName, chosenColor.toHex() ?? "FF0000")
                        dismiss()
                    }
                }
            }
        }
    }
}

struct ColorChooserCircle: View {
    var color: Color
    
    var body: some View {
        Circle()
            .strokeBorder(Color.gray,lineWidth: 2)
            .background(Circle().foregroundColor(color))
            .frame(minHeight: 25)
    }
}

#Preview {
    NewProjectForm { name, color in }
}
