//
//  ClubsView.swift
//  PadelClub
//
//  Created by Razmig Sarkissian on 20/03/2024.
//

import SwiftUI
import TipKit
import LeStorage

struct ClubsView: View {
    @EnvironmentObject var dataStore: DataStore
    @Environment(\.dismiss) private var dismiss
    @State private var presentClubSearchView: Bool = false
    @State private var newClub: Club?
    var selection: ((Club) -> ())? = nil
    
    var presentClubCreationView: Binding<Bool> { Binding(
        get: { newClub != nil },
        set: { isPresented in
            if isPresented == false {
                newClub = nil
            }
        }
    )}
    
    var body: some View {
        List {
            let clubs : [Club] = dataStore.user.clubsObjects(includeCreated: false)
            let onlyCreatedClubs : [Club] = dataStore.user.createdClubsObjectsNotFavorite()

            if clubs.isEmpty == false || onlyCreatedClubs.isEmpty == false {
                Section {
                    if clubs.isEmpty && onlyCreatedClubs.isEmpty == false {
                        _contentUnavailable()
                    }
                    ForEach(clubs) { club in
                        if let selection {
                            Button {
                                selection(club)
                                dismiss()
                            } label: {
                                ClubRowView(club: club, displayContext: .selection)
                                    .frame(maxWidth: .infinity)
                            }
                            .contentShape(Rectangle())
                            .buttonStyle(.plain)
                        } else {
                            NavigationLink {
                                ClubDetailView(club: club, displayContext: club.hasBeenCreated(by: Store.main.userId) ? .edition : .lockedForEditing)
                            } label: {
                                ClubRowView(club: club)
                            }
                        }
                    }
                } header: {
                    Text("Clubs favoris")
                }
            }

            if onlyCreatedClubs.isEmpty == false {
                Section {
                    ForEach(onlyCreatedClubs) { club in
                        if let selection {
                            Button {
                                selection(club)
                                dismiss()
                            } label: {
                                ClubRowView(club: club, displayContext: .selection)
                                    .frame(maxWidth: .infinity)
                            }
                            .contentShape(Rectangle())
                            .buttonStyle(.plain)
                        } else {
                            NavigationLink {
                                ClubDetailView(club: club, displayContext: club.hasBeenCreated(by: Store.main.userId) ? .edition : .lockedForEditing)
                            } label: {
                                ClubRowView(club: club)
                            }
                        }
                    }
                } header: {
                    Text("Clubs créés retirés des favoris")
                }
            }
        }
        .overlay {
            if dataStore.user.clubsObjects(includeCreated: true).isEmpty {
                _contentUnavailable()
            }
        }
        .navigationTitle(selection == nil ? "Clubs favoris" : "Choisir un club")
        .navigationBarTitleDisplayMode(.inline)
        .toolbarBackground(.visible, for: .navigationBar)
        .sheet(isPresented: presentClubCreationView) {
            if let newClub {
                CreateClubView(club: newClub) { club in
                    if let selection {
                        selection(club)
                        dismiss()
                    }
                }
                .tint(.master)
            }
        }
        .sheet(isPresented: $presentClubSearchView) {
            ClubImportView() { club in
                if let selection {
                    selection(club)
                    dismiss()
                }
            }
            .tint(.master)
        }
        .toolbar {
            ToolbarItemGroup(placement: .topBarTrailing) {
                Button {
                    presentClubSearchView = true
                } label: {
                    Image(systemName: "magnifyingglass.circle.fill")  
                        .resizable()
                        .scaledToFit()
                        .frame(minHeight: 28)
                }
                
                Button {
                    newClub = Club.newEmptyInstance()
                } label: {
                    Image(systemName: "plus.circle.fill")
                        .resizable()
                        .scaledToFit()
                        .frame(minHeight: 28)
                }
            }
        }
    }
    
    private func _contentUnavailable() -> some View {
        ContentUnavailableView {
            Label("Aucun club", systemImage: "house.and.flag.fill")
        } description: {
            Text("Avoir un club en favori permet d'accéder aux tournois enregistrés sur Tenup.")
        } actions: {
            RowButtonView("Créer un nouveau club", systemImage: "plus.circle.fill") {
                newClub = Club.newEmptyInstance()
            }
            RowButtonView("Chercher un club", systemImage: "magnifyingglass.circle.fill") {
                presentClubSearchView = true
            }
        }

    }
}

//#Preview {
//    NavigationStack {
//        ClubsView()
//            .environmentObject(DataStore.shared)
//    }
//}
