//
//  ImportedPlayerView.swift
//  PadelClub
//
//  Created by Razmig Sarkissian on 01/03/2024.
//

import SwiftUI

struct ImportedPlayerView: View {
    let player: PlayerHolder
    var index: Int? = nil
    var showFemaleInMaleAssimilation: Bool = false
    var showProgression: Bool = false
    var isAnimation: Bool {
        player.getComputedRank() == 0
    }

    var body: some View {
        VStack(alignment: .leading) {
            HStack {
                if player.isAnonymous() {
                    Text("Joueur Anonyme")
                } else {
                    Text(player.getLastName().capitalized)
                    Text(player.getFirstName().capitalized)
                }
                if index == nil {
                    Text(player.male ? "♂︎" : "♀︎")
                }
                Spacer()
                if let index {
                    HStack(alignment: .top, spacing: 0) {
                        Text(index.formatted())
                            .foregroundStyle(.secondary)
                            .font(.title3)
                        Text(index.ordinalFormattedSuffix())
                            .foregroundStyle(.secondary)
                            .font(.caption)
                    }
                }
            }
            .font(.title3)
            .lineLimit(1)
            if isAnimation == false {
                HStack {
                    HStack(alignment: .top, spacing: 0) {
                        Text(player.formattedRank()).italic(player.isAssimilated)
                            .font(.title3)                    
                            .background {
                                if player.isNotFromCurrentDate() {
                                    UnderlineView()
                                }
                            }
                        if let rank = player.getRank() {
                            Text(rank.ordinalFormattedSuffix()).italic(player.isAssimilated)
                                .font(.caption)
                        }
                    }
                    
                    if showProgression, player.getProgression() != 0 {
                        HStack(alignment: .top, spacing: 2) {
                            Text("(")
                            Text(player.getProgression().formatted(.number.sign(strategy: .always())))
                                .foregroundStyle(player.getProgressionColor(progression: player.getProgression()))
                            Text(")")
                        }.font(.title3)
                    }
                    
                    if let pts = player.getPoints(), pts > 0 {
                        HStack(alignment: .lastTextBaseline, spacing: 0) {
                            Text(pts.formatted()).font(.title3)
                            Text(" pts").font(.caption)
                        }
                    }
                    
                    if let tournamentPlayed = player.tournamentPlayed, tournamentPlayed > 0 {
                        HStack(alignment: .lastTextBaseline, spacing: 0) {
                            Text(tournamentPlayed.formatted()).font(.title3)
                            Text(" tournoi" + tournamentPlayed.pluralSuffix).font(.caption)
                        }
                    }
                }
                .lineLimit(1)
                .truncationMode(.tail)
                
                if showFemaleInMaleAssimilation, let assimilatedAsMaleRank = player.getAssimilatedAsMaleRank() {
                    HStack(alignment: .top, spacing: 2) {
                        Text("(")
                        Text(assimilatedAsMaleRank.formatted())
                        VStack(alignment: .leading, spacing: 0) {
                            Text("équivalence")
                            Text("messieurs")
                        }
                        .font(.caption)
                        Text(")").font(.title3)
                    }
                }
                
                HStack {
                    Text(player.formattedLicense())
                    if let computedAge = player.computedAge {
                        Text(computedAge.formatted() + " ans")
                    }
                }
                .font(.caption)
                if let clubName = player.clubName {
                    Text(clubName)
                        .font(.caption)
                }
                if let ligueName = player.ligueName {
                    Text(ligueName)
                        .font(.caption)
                }
            }
        }
        .contextMenu {
            CopyPasteButtonView(title: "Licence", pasteValue: player.formattedLicense())
        }
    }
}
