//
//  OngoingDestination.swift
//  PadelClub
//
//  Created by razmig on 07/11/2024.
//
import SwiftUI

enum OngoingDestination: Int, CaseIterable, Identifiable, Selectable, Equatable {
    var id: Int { self.rawValue }
    
    static func == (lhs: OngoingDestination, rhs: OngoingDestination) -> Bool {
        return lhs.id == rhs.id
    }
    
    case running
    case followUp
    case court
    case free
    case over
    
    var runningAndNextMatches: [Match] {
        switch self {
        case .running, .court, .free:
            return OngoingViewModel.shared.runningAndNextMatches
        case .followUp:
            return OngoingViewModel.shared.filteredRunningAndNextMatches
        case .over:
            return DataStore.shared.endMatches()
        }
    }
    
    var sortedMatches: [Match] {
        return runningAndNextMatches.filter({ self.shouldDisplay($0) })
    }
    
    var filteredMatches: [Match] {
        sortedMatches.filter({ OngoingDestination.running.shouldDisplay($0) })
    }

    var sortedCourtIndex: [Int?] {
        let courtUsed = sortedMatches.grouped(by: { $0.courtIndex }).keys
        let sortedNumbers = courtUsed.sorted { (a, b) -> Bool in
            switch (a, b) {
            case (nil, _): return false
            case (_, nil): return true
            case let (a?, b?): return a < b
            }
        }
        return sortedNumbers
    }
    
    func contentUnavailable() -> some View {
        switch self {
        case .running:
            ContentUnavailableView("Aucun match en cours", systemImage: "figure.tennis", description: Text("Tous vos matchs en cours seront visibles ici, quelque soit le tournoi."))
        case .followUp:
            ContentUnavailableView("Aucun match à suivre", systemImage: "figure.tennis", description: Text("Tous vos matchs planifiés et confirmés, seront visibles ici, quelque soit le tournoi."))
        case .court:
            ContentUnavailableView("Aucun match en cours", systemImage: "sportscourt", description: Text("Tous vos terrains correspondant aux matchs en cours seront visibles ici, quelque soit le tournoi."))
        case .free:
            ContentUnavailableView("Aucun terrain libre", systemImage: "sportscourt", description: Text("Les terrains libres seront visibles ici, quelque soit le tournoi."))
        case .over:
            ContentUnavailableView("Aucun match terminé", systemImage: "clock.badge.xmark", description: Text("Les matchs terminés seront visibles ici, quelque soit le tournoi."))
        }
    }
    
    func localizedFilterModeLabel() -> String {
        switch self {
        case .running:
            return "En cours"
        case .followUp:
            return "À suivre"
        case .court:
            return "Terrains"
        case .free:
            return "Libres"
        case .over:
            return "Finis"
        }
    }
    
    func shouldDisplay(_ match: Match) -> Bool {
        switch self {
        case .running:
            return match.isRunning()
        case .court, .free:
            return true
        case .followUp:
            return match.isRunning() == false
        case .over:
            return match.hasEnded()
        }
    }
    
    func selectionLabel(index: Int) -> String {
        localizedFilterModeLabel()
    }
    
    func systemImage() -> String? {
        switch self {
        default:
            return nil
        }
    }
    
    func badgeValue() -> Int? {
        switch self {
        case .running, .followUp, .over:
            sortedMatches.count
        case .court:
            sortedCourtIndex.filter({ index in
                filteredMatches.filter({ $0.courtIndex == index }).isEmpty == false
            }).count
        case .free:
            sortedCourtIndex.filter({ index in
                filteredMatches.filter({ $0.courtIndex == index }).isEmpty
            }).count
        }
    }
    
    func badgeValueColor() -> Color? {
        nil
    }
    
    func badgeImage() -> Badge? {
        nil
    }
}
