//
//  FlyerFormView.swift
//  AlloVoisinsSwiftUI
//
//  Created by Victor on 25/11/2024.
//

import SwiftUI
import _PhotosUI_SwiftUI

struct FlyerFormView: View {
    @State var title: String = ""
    @State var subtitle: String = ""
    @State var job: String = ""
    @State var showRating: Bool = true
    @State var phoneNumber: String = ""
    @State var address: String = ""
    @State var selectedPicture: PhotosPickerItem?
    @State private var image: Image?
    @State private var showImagePicker = false
    @State private var showPhotoPicker = false
    @State private var showActionSheet = false
    @State private var useCamera = false
    @State private var authentConfirm: Bool = false
    @State private var confirmIsInError: Bool = false

    var body: some View {
        VStack {
            ScrollView {
                VStack {
                    VStack(alignment: .leading, spacing: 16) {
                        VStack(alignment: .leading) {
                            SQText("Informations")
                                .sqSize(24)
                                .sqFont(.bold)
                            SQText("Les modifications apportées sur votre prospectus ne seront pas reportées sur votre profil.")
                        }
                        VStack(alignment: .leading) {
                            SQText("Image")
                                .sqFont(.semiBold)

                            ZStack(alignment: .bottomTrailing) {
                                Button(action: { showActionSheet = true }) {
                                    if let image = image {
                                        image
                                            .resizable()
                                            .scaledToFill()
                                            .frame(width: 160, height: 160)
                                            .clipShape(Circle())
                                    } else {
                                        SQIcon(.camera, customSize: 40)
                                            .frame(width: 160, height: 160)
                                            .background(Color.sqNeutral(10))
                                            .clipShape(Circle())
                                    }
                                }

                                if image != nil {
                                    SQIcon(.pen_to_square, size: .m)
                                        .padding(8)
                                        .background(Circle().fill(Color.sqNeutral(15)))
                                        .shadow(color: .sqNeutral(100).opacity(0.1), radius: 4, x: 0, y: 2)
                                        .offset(x: -8, y: -8)
                                }
                            }
                            .frame(maxWidth: .infinity)
                        }
                        SQTextField("Titre", text: $title)
                            .sqPlaceholder("Ex : Pro Solutions")
                        SQTextField("Sous-titre", text: $subtitle)
                            .sqPlaceholder("Ex : Martin Dupont")
                            .sqOptional()
                        SQTextField("Métier", text: $job)
                            .sqPlaceholder("Ex : Dépannage électroménager")
                            .sqOptional()
                        VStack(alignment: .leading, spacing: 0) {
                            Toggle(isOn: $showRating) {
                                SQText("Afficher ma note AlloVoisins")
                                    .sqFont(.semiBold)
                            }
                            .tint(Color.sqNeutral(100))
                            HStack(spacing: 4) {
                                SQIcon(.star, type: .solid, color: .sqGold(50))
                                SQText("4,7/5 sur")
                                    .sqSize(12)
                                    .sqFont(.semiBold)
                                SQImage("logo", height: 14)
                            }
                        }
                        .padding(.horizontal, 2)
                    }
                    .padding()

                    Rectangle()
                        .frame(height: 16)
                        .foregroundColor(Color.sqNeutral(10))

                    VStack(alignment: .leading, spacing: 16) {
                        SQTextEditor("Zone de text", placeholder: "Ex : Pro Solutions propose une gamme de services complète pour tout type de dépannage électroménager. Bénéficiez de nos 10 ans d’expérience ! Devis gratuit", text: .constant(""))

                        SQTextField("Prestation 1", text: $job)
                            .sqPlaceholder("Ex : Réparation lave-vaisselle")
                            .sqOptional()
                        SQTextField("Prestation 2", text: $job)
                            .sqPlaceholder("Ex : Réparation machine à laver")
                            .sqOptional()
                        SQTextField("Prestation 3", text: $job)
                            .sqPlaceholder("Ex : Réparation four")
                            .sqOptional()
                        SQTextField("Prestation 4", text: $job)
                            .sqPlaceholder("Ex : Réparation outillage")
                            .sqOptional()
                        SQTextField("Prestation 5", text: $job)
                            .sqPlaceholder("Ex : Dépannage électroménager")
                            .sqOptional()
                    }
                    .padding()

                    Rectangle()
                        .frame(height: 16)
                        .foregroundColor(Color.sqNeutral(10))

                    VStack(alignment: .leading, spacing: 16) {
                        SQTextField("Numéro de téléphone", text: $phoneNumber)
                            .sqPlaceholder("Ex : 06 12 34 56 78")
                        SQTextField("Adresse complète", text: $address)
                            .sqPlaceholder("Ex : 1 rue de la gare, 67000 Strasbourg")
                    }
                    .padding()

                    Rectangle()
                        .frame(height: 16)
                        .foregroundColor(Color.sqNeutral(10))

                    VStack(alignment: .leading, spacing: 16) {
                        SQText("Mentions légales obligatoires")
                            .sqSize(18)
                            .sqFont(.bold)

                        SQTextField("Dénomination sociale", text: $job)
                            .sqPlaceholder("Ex : Pro solutions")
                            .sqOptional()
                        SQTextField("Adresse du siège social", text: $job)
                            .sqPlaceholder("Ex : 16 rue de la Redoute, 67500 Haguenau")
                            .sqOptional()
                        VStack(spacing: 0) {
                            SQTextField("Numéro SIRET", text: $job)
                                .sqPlaceholder("Ex : 12345678901234")
                                .sqOptional()
                            SQText("Le numéro SIRET se compose de 14 chiffres : les 9 chiffres du SIREN + 5 chiffres propres à chaque établissement (NIC).")
                                .sqSize(12)
                                .sqColor(.sqNeutral(50))
                        }
                        SQTextField("Numéro de RCS", text: $job)
                            .sqPlaceholder("Ex : RCS STRASBOURG B 123456789")
                            .sqOptional()
                        SQTextField("Statut juridique", text: $job)
                            .sqPlaceholder("Ex : SARL")
                            .sqOptional()
                        SQTextField("Montant du capital social (€)", text: $job)
                            .sqPlaceholder("Ex : 1 000,00")
                            .sqOptional()
                        SQTextField("Autre champ relatif à votre activité", text: $job)
                            .sqPlaceholder("Ex : Pour votre santé, mangez 5 fruits et légumes par jour")
                            .sqOptional()
                        SQCheckbox("Je comprends que j’engage ma responsabilité sur l’exhaustivité et l’authenticité des informations renseignées ci-dessus.", isChecked: $authentConfirm, error: .constant(.none))
                    }
                    .padding()
                }
            }
            SQFooter {
                SQButton("Aperçu") {}
                    .sqIcon(SQIcon(.eye, color: .sqNeutral(100)))
                    .sqButtonType(.line)
                SQButton("Imprimer") {
                    if authentConfirm == false {
                        self.confirmIsInError = true
                    }
                }
                .sqIcon(SQIcon(.print, color: .white))
            }
        }
        .confirmationDialog("Choisir une image", isPresented: $showActionSheet, actions: {
            Button("Appareil photo") {
                useCamera = true
                showImagePicker = true
            }

            Button("Galerie") {
                useCamera = false
                showPhotoPicker = true
            }

            Button("Annuler", role: .cancel) {}
        })
        .photosPicker(isPresented: $showPhotoPicker, selection: $selectedPicture)
        .sheet(isPresented: $showImagePicker) {
            if useCamera {
                ImagePicker(image: $image, sourceType: .camera)
            }
        }
        .task(id: selectedPicture) {
            if let data = try? await selectedPicture?.loadTransferable(type: Image.self) {
                image = data
            }
        }
        .sqNavigationBar(title: "Mon prospectus")
    }
}

#Preview {
    FlyerFormView()
}
