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

import SwiftUI
import LeStorage

extension Int: @retroactive Identifiable {
    public var id: Int {
        return self
    }
}



struct OngoingView: View {

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

    var filterMode: OngoingDestination {
        ongoingViewModel.destination!
    }

    var body: some View {
        let filteredMatches = filterMode.sortedMatches
        List {
            ForEach(filteredMatches) { match in
                let tournament = match.currentTournament()
                Section {
                    MatchRowView(match: match)
                        .matchViewStyle(.followUpStyle)
                } header: {
                    if let tournament {
                        HStack {
                            Text(tournament.tournamentTitle(.short))
                            Spacer()
                            if let club = tournament.club() {
                                Text("@" + club.clubTitle(.short))
                            }
                        }
                    }
                } footer: {
                    HStack {
                        if let tournament {
                            Text(tournament.eventLabel())
                        }
#if DEBUG
                        Spacer()
                        FooterButtonView("copier l'id") {
                            let pasteboard = UIPasteboard.general
                            pasteboard.string = match.id
                        }
#endif
                    }
                }
            }
        }
        .headerProminence(.increased)
        .overlay {
            if filteredMatches.isEmpty {
                filterMode.contentUnavailable()
            }
        }
    }
}

struct OngoingCourtView: View {

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

    var filterMode: OngoingDestination {
        ongoingViewModel.destination!
    }
    
    @State private var selectedCourtForFollowUp: Int?
        
    var body: some View {
        let sortedMatches = filterMode.sortedMatches
        let filteredMatches = sortedMatches.filter({ OngoingDestination.running.shouldDisplay($0) })
        
        List {
            ForEach(filterMode.sortedCourtIndex, id: \.self) { index in
                let courtFilteredMatches = filteredMatches.filter({ $0.courtIndex == index })
                let title : String = (index == nil ? "Aucun terrain défini" : "Terrain #\(index! + 1)")
                if (filterMode == .free && courtFilteredMatches.isEmpty) || (filterMode == .court && courtFilteredMatches.isEmpty == false) {
                    Section {
                        MatchListView(section: "En cours", matches: courtFilteredMatches, hideWhenEmpty: true, isExpanded: false)
                        MatchListView(section: "À venir", matches: sortedMatches.filter({ $0.courtIndex == index && $0.hasStarted() == false }), isExpanded: false)
                    } header: {
                        Text(title)
                    } footer: {
                        FooterButtonView("Ajouter un match à suivre") {
                            selectedCourtForFollowUp = index
                        }
                    }
                }
            }
        }
        .sheet(item: $selectedCourtForFollowUp, content: { selectedCourtForFollowUp in
            FollowUpMatchView(selectedCourt: selectedCourtForFollowUp, allMatches: filterMode.runningAndNextMatches)
                .tint(.master)
        })
        .headerProminence(.increased)
        .overlay {
            if (filteredMatches.isEmpty && filterMode != .free) || (filterMode == .free && filterMode.sortedCourtIndex.allSatisfy({ index in filteredMatches.filter({ $0.courtIndex == index }).isEmpty == false })) {
                filterMode.contentUnavailable()
            }
        }
    }
}
