//
//  OngoingView.swift
//  PadelClub
//
//  Created by Razmig Sarkissian on 24/04/2024.
//

import SwiftUI
import LeStorage

struct OngoingView: View {

    @Environment(NavigationViewModel.self) private var navigation: NavigationViewModel
    @EnvironmentObject var dataStore: DataStore

    @State private var sortByField: Bool = false
    
    let fieldSorting : [MySortDescriptor<Match>] = [.keyPath(\Match.courtIndexForSorting), .keyPath(\Match.startDate!)]
    let defaultSorting : [MySortDescriptor<Match>] = [.keyPath(\Match.startDate!), .keyPath(\Match.courtIndexForSorting)]

    var matches: [Match] {
        let sorting = self.sortByField ? fieldSorting : defaultSorting
        return self.dataStore.runningMatches().sorted(using: sorting, order: .ascending)
    }
    
    var body: some View {
        @Bindable var navigation = navigation
        NavigationStack(path: $navigation.ongoingPath) {
            List {
                ForEach(matches) { match in
                    
                    if let tournament = match.currentTournament() {
                        
                        Section {
                            MatchRowView(match: match, matchViewStyle: .standardStyle)
                        } header: {
                            HStack {
                                Text(tournament.tournamentTitle(.short))
                                Spacer()
                                if let club = tournament.club() {
                                    Text("@" + club.clubTitle(.short))
                                }
                            }
                        } footer: {
                            HStack {
                                Text(tournament.eventLabel())
#if DEBUG
                                Spacer()
                                FooterButtonView("copier l'id") {
                                    let pasteboard = UIPasteboard.general
                                    pasteboard.string = match.id
                                }
#endif
                            }
                        }
                        
                    }
                    
                }
            }
            .headerProminence(.increased)
            .overlay {
                if matches.isEmpty {
                    ContentUnavailableView("Aucun match en cours", systemImage: "figure.tennis", description: Text("Tous vos matchs en cours seront visibles ici, quelque soit le tournoi."))
                }
            }
            .navigationTitle("En cours")
            .toolbarBackground(.visible, for: .bottomBar)
            .toolbar(matches.isEmpty ? .hidden : .visible, for: .navigationBar)
            .toolbar {
                ToolbarItem(placement: .status) {
                    Picker(selection: $sortByField) {
                        Text("tri par date").tag(true)
                        Text("tri par terrain").tag(false)
                    } label: {
                        
                    }
                    .pickerStyle(.segmented)
                    .fixedSize()
                    .offset(y: -3)
                }
            }
        }
    }
}

//#Preview {
//    OngoingView()
//}
