//
//  HistoryView.swift
//  DouShouQiIOS
//
//  Created by Pierre FERREIRA on 21/05/2024.
//

import SwiftUI


struct HistoryView: View {
    var playerName: String
    var matches: [Match] // Modèle des parties du joueur

    var body: some View {
        
        VStack{
            Text("Historique du joueur").font(.largeTitle)
            
            HStack{
                Spacer()
                DoubleTextDisplay(textL: "Ratio", textR: "0,3")
                Spacer()
                DoubleTextDisplay(textL: "Total", textR: "3")
                Spacer()
            }
            

            List(matches) { match in
                HStack {
                    Image(match.gameEndScreen)
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                        .frame(width: 50, height: 50)
                        .cornerRadius(8)

                    VStack(alignment: .leading) {
                        Text(match.opponent)
                            .font(.headline)
                        Text("Score: \(match.selfScore) - \(match.opScore)")
                            .font(.subheadline)
                    }
                    .padding(.leading, 10)

                    Spacer()

                    Text(match.selfScore > match.opScore ? "Victoire" : (match.selfScore == match.opScore ? "Nul" : "Défaite"))
                        .foregroundColor(match.selfScore > match.opScore ? .green : .red)
                        .padding(.trailing, 10)
                }.listRowBackground(Color.yellow.opacity(0.1))
                .padding()
                .background(match.selfScore > match.opScore ? Color.green.opacity(0.3) : Color.red.opacity(0.3))
                .cornerRadius(10)
                
                
                
            }
            .padding()
            .navigationTitle("Historique de \(playerName)")
            
        }.background(Color.gray.opacity(0.1))
    }
}

struct Match: Identifiable {
    var id = UUID()
    var gameEndScreen: String
    var opponent: String
    var selfScore: Int
    var opScore: Int
    //var result: String
}

struct HistoryView_Previews: PreviewProvider {
    static var previews: some View {
        let sampleMatches = [
            Match(gameEndScreen: "DouShouQi", opponent: "Opponent 1", selfScore: 3, opScore: 0),//result: "Victoire""
            Match(gameEndScreen: "DouShouQi", opponent: "Opponent 2", selfScore: 0, opScore: 3),// result: "Défaite"),
            Match(gameEndScreen: "DouShouQi", opponent: "Opponent 3", selfScore: 2, opScore: 2),// result: "Nul")
        ]
        HistoryView(playerName: "bro", matches: sampleMatches)
    }
}
