//
//  OngoingContainerView.swift
//  PadelClub
//
//  Created by razmig on 07/11/2024.
//

import SwiftUI
import LeStorage

@Observable
class OngoingViewModel {
    static let shared = OngoingViewModel()

    var destination: OngoingDestination? = .running
    var hideUnconfirmedMatches: Bool = false
    var hideNotReadyMatches: Bool = false
    
    func areFiltersEnabled() -> Bool {
        hideUnconfirmedMatches || hideNotReadyMatches
    }

    let defaultSorting : [MySortDescriptor<Match>] = [.keyPath(\Match.startDate!), .keyPath(\Match.index), .keyPath(\Match.courtIndexForSorting)]

    var runningAndNextMatches: [Match] {
        DataStore.shared.runningAndNextMatches().sorted(using: defaultSorting, order: .ascending)
    }
    
    var filteredRunningAndNextMatches: [Match] {
        return runningAndNextMatches.filter({
            (hideUnconfirmedMatches == false || hideUnconfirmedMatches == true && $0.confirmed)
            && (hideNotReadyMatches == false || hideNotReadyMatches == true && $0.isReady() )
        })
    }
}

struct OngoingContainerView: View {
    @Environment(NavigationViewModel.self) private var navigation: NavigationViewModel
    @State private var showMatchPicker: Bool = false
    
    var body: some View {
        @Bindable var navigation = navigation
        @Bindable var ongoingViewModel = OngoingViewModel.shared
        NavigationStack(path: $navigation.ongoingPath) {
            VStack(spacing: 0) {
                GenericDestinationPickerView(selectedDestination: $ongoingViewModel.destination, destinations: OngoingDestination.allCases, nilDestinationIsValid: false)
                
                switch ongoingViewModel.destination! {
                case .running, .followUp, .over:
                    OngoingView()
                case .court, .free:
                    OngoingCourtView()
                }
            }
            .navigationBarTitleDisplayMode(.inline)
            .toolbarBackground(.visible, for: .navigationBar)
            .navigationTitle("Programmation")
            .toolbar {
                if ongoingViewModel.destination == .followUp {
                    ToolbarItem(placement: .topBarLeading) {
                        Menu {
                            Toggle(isOn: $ongoingViewModel.hideUnconfirmedMatches) {
                                Text("masquer non confirmés")
                            }
                            Toggle(isOn: $ongoingViewModel.hideNotReadyMatches) {
                                Text("masquer incomplets")
                            }
                        } label: {
                            Image(systemName: "line.3.horizontal.decrease.circle")
                                .resizable()
                                .scaledToFit()
                                .frame(minHeight: 32)
                        }
                        .symbolVariant(ongoingViewModel.areFiltersEnabled() ? .fill : .none)
                    }
                }
                ToolbarItem(placement: .topBarTrailing) {
                    Button {
                        showMatchPicker = true
                    } label: {
                        Image(systemName: "rectangle.stack.badge.plus")
                            .resizable()
                            .scaledToFit()
                            .frame(minHeight: 32)
                    }
                }
            }
        }
        .environment(ongoingViewModel)
        .sheet(isPresented: $showMatchPicker, content: {
            FollowUpMatchView(selectedCourt: nil, allMatches: ongoingViewModel.runningAndNextMatches, autoDismiss: false)
                .tint(.master)
        })
    }
}
