//
//  PlayerBlockView.swift
//  Padel Tournament
//
//  Created by Razmig Sarkissian on 25/11/2023.
//

import SwiftUI

struct PlayerBlockView: View {
    var match: Match
    let teamPosition: TeamPosition
    let team: TeamRegistration?
    let color: Color
    let width: CGFloat
    let teamScore: TeamScore?
    
    init(match: Match, teamPosition: TeamPosition, color: Color, width: CGFloat) {
        self.match = match
        self.teamPosition = teamPosition
        let theTeam = match.team(teamPosition)
        self.team = theTeam
        self.color = color
        self.width = width
        self.teamScore = match.teamScore(ofTeam: theTeam)
    }
    
    var names: [String]? {
        match.teamNames(team)
    }
    
    var hasWon: Bool {
        match.teamWon(team)
    }
    
    var hideScore: Bool {
        match.hasWalkoutTeam()
    }
    
    var isWalkOut: Bool {
        match.teamWalkOut(team)
    }
    
    var scores: [String] {
        teamScore?.score?.components(separatedBy: ",") ?? []
    }

    private func _defaultLabel() -> String {
        teamPosition.localizedLabel()
    }
    
    var body: some View {
        HStack {
            VStack(alignment: .leading) {
                if let names {
                    if let teamScore, teamScore.luckyLoser != nil {
                        Text("Repêchée").italic().font(.caption)
                    }

                    if let name = team?.name {
                        Text(name).foregroundStyle(.secondary)
                    }
                    ForEach(names, id: \.self) { name in
                        Text(name).lineLimit(1)
                    }
                } else {
                    ZStack(alignment: .leading) {
                        VStack {
                            if let name = team?.name {
                                Text(name).foregroundStyle(.secondary)
                            }
                            Text("longLabelPlayerOne").lineLimit(1)
                            Text("longLabelPlayerTwo").lineLimit(1)
                        }
                        .opacity(0)
                        Text(_defaultLabel()).foregroundStyle(.secondary).lineLimit(1)
                    }
                }
            }
            .bold(hasWon)
            Spacer()
            if hasWon {
                Image(systemName: "trophy")
            } else if isWalkOut {
                Text("WO")
            }
            
            if hideScore == false && scores.isEmpty == false {
                ForEach(scores.indices, id: \.self) { index in
                    let string = scores[index]
                    if string.isEmpty == false {
                        if width == 1 {
                            Divider()
                        } else {
                            Divider().frame(width: width).overlay(color)
                        }
                        Text(string)
                            .font(.title3)
                            .frame(maxWidth: 20)
                            .scaledToFill()
                            .minimumScaleFactor(0.5)
                            .lineLimit(1)
                    }
                }
            } else if let team {                
                TeamWeightView(team: team, teamPosition: teamPosition)
            }
        }
    }
}
