//
//  SQNavigationBar.swift
//  AlloVoisinsSwiftUI
//
//  Created by Victor on 14/10/2024.
//

import SwiftUI

struct SQNavigationBar: ViewModifier {
    @Environment(\.presentationMode) var presentationMode
    let title: String

    func body(content: Content) -> some View {
        content
            .navigationBarBackButtonHidden(true)
            .navigationBarTitle(title, displayMode: .inline)
            .navigationBarItems(leading: backButton)
            .onAppear {
                configureNavigationBarAppearance()
            }
    }

    private var backButton: some View {
        Button(action: {
            self.presentationMode.wrappedValue.dismiss()
        }) {
            Image(systemName: "chevron.left")
                .foregroundColor(.black)
                .imageScale(.large)
        }
    }

    private func configureNavigationBarAppearance() {
        let appearance = UINavigationBarAppearance()
        appearance.configureWithOpaqueBackground()
        appearance.backgroundColor = .white
        appearance.shadowColor = .gray.withAlphaComponent(0.3)
        appearance.titleTextAttributes = [.foregroundColor: UIColor.black]

        UINavigationBar.appearance().standardAppearance = appearance
        UINavigationBar.appearance().compactAppearance = appearance
        UINavigationBar.appearance().scrollEdgeAppearance = appearance
    }
}

extension View {
    func sqNavigationBar(title: String) -> some View {
        self.modifier(SQNavigationBar(title: title))
    }
}
