//
//  TeamRowView.swift
//  PadelClub
//
//  Created by Razmig Sarkissian on 24/03/2024.
//

import SwiftUI

struct TeamRowView: View {
    @EnvironmentObject var dataStore: DataStore

    var team: TeamRegistration
    var teamPosition: TeamPosition? = nil
    var displayCallDate: Bool = false
    var displayRestingTime: Bool = false
    var teamIndex: Int?
    
    var body: some View {
        LabeledContent {
            TeamWeightView(team: team, teamPosition: teamPosition, teamIndex: teamIndex)
        } label: {
            VStack(alignment: .leading) {
                TeamHeadlineView(team: team)
                TeamView(team: team)
            }
            if displayCallDate {
                TeamCallDateView(team: team)
            }
            if displayRestingTime {
                TeamRestingView(team: team)
            }
        }
    }
    
    struct TeamRestingView: View {
        let team: TeamRegistration

        @ViewBuilder
        var body: some View {
            if let restingTime = team.restingTime()?.timeIntervalSinceNow, let value = Date.hourMinuteFormatter.string(from: restingTime * -1) {
                if restingTime > -300 {
                    Text("vient de finir")
                        .font(.footnote)
                        .foregroundStyle(.secondary)
                } else {
                    Text("en repos depuis " + value)
                        .font(.footnote)
                        .foregroundStyle(.secondary)
                }
            }
        }
    }
    
    struct TeamView: View {
        let team: TeamRegistration

        var body: some View {
            if let name = team.name, name.isEmpty == false {
                Text(name).foregroundStyle(.secondary).font(.footnote)
                if team.players().isEmpty {
                    Text("Aucun joueur")
                } else {
                    CompactTeamView(team: team)
                }
            } else {
                if team.players().isEmpty == false {
                    CompactTeamView(team: team)
                } else {
                    Text("Place réservée")
                    Text("Place réservée")
                }
            }
        }
    }
    
    struct TeamHeadlineView: View {
        @Environment(\.isEditingTournamentSeed) private var isEditingTournamentSeed
        let team: TeamRegistration
        
        var body: some View {
            VStack(alignment: .leading) {
                if let wildcardLabel = team.wildcardLabel() {
                    Text(wildcardLabel).italic().foregroundStyle(.logoRed).font(.caption)
                }

                if let qualifiedStatus = team.qualifiedStatus(hideBracketStatus: isEditingTournamentSeed.wrappedValue == true) {
                    Text(qualifiedStatus)
                        .font(.caption)
                        .foregroundStyle(.secondary)
                }
            }
        }
    }

    struct TeamCallDateView: View {
        let team: TeamRegistration

        var body: some View {
            if let callDate = team.callDate {
                Text("Déjà convoquée \(callDate.localizedDate())")
                    .foregroundStyle(.logoRed)
                    .italic()
                    .font(.caption)
            } else {
                Text("Pas encore convoquée")
                    .foregroundStyle(.logoRed)
                    .italic()
                    .font(.caption)
            }
        }
    }

    struct CompactTeamView: View {
        let team: TeamRegistration

        var body: some View {
            ForEach(team.players()) { player in
                HStack(spacing: 4) {
                    if player.registeredOnline {
                        Image(systemName: "circle.fill").foregroundStyle(.green)
                            .font(.system(size: 8))
                    }
                    Text(player.playerLabel()).lineLimit(1).truncationMode(.tail)
                }
            }
        }
    }
}
