//
//  TeamWeightView.swift
//  PadelClub
//
//  Created by Razmig Sarkissian on 18/04/2024.
//

import SwiftUI

struct TeamWeightView: View {
    @EnvironmentObject var dataStore: DataStore
    let team: TeamRegistration
    var teamPosition: TeamPosition? = nil
    var teamIndex: Int?
    
    init(team: TeamRegistration, teamPosition: TeamPosition? = nil, teamIndex: Int? = nil) {
        self.team = team
        self.teamPosition = teamPosition
        self.teamIndex = teamIndex ?? team.tournamentObject()?.indexOf(team: team)
    }
    
    var displayWeight: Bool {
        team.shouldDisplayRankAndWeight() && team.tournamentObject()?.hideWeight() == false
    }

    var body: some View {
        VStack(alignment: .trailing, spacing: 0) {
            let displayWeight = self.displayWeight
            if (teamPosition == .one || teamPosition == nil) && displayWeight {
                Text(team.weight.formatted())
                    .monospacedDigit()
                    .font(.caption)
            }
            if let teamIndex {
                Text("#" + (teamIndex + 1).formatted(.number.precision(.integerLength(2...3))))
                    .monospacedDigit()
            }
            if teamPosition == .two && displayWeight {
                Text(team.weight.formatted())
                    .monospacedDigit()
                    .font(.caption)
                
            }
        }
    }
}

//#Preview {
//    TeamWeightView(team: TeamRegistration.mock(), teamPosition: .one)
//}
