//
//  PlayerView.swift
//  DouShouQiIOS
//
//  Created by Pierre FERREIRA on 13/05/2024.
//

import SwiftUI
import DouShouQiModel


struct PlayerView: View {
    //var Player : Player;
    //var name : String
    var age: Int
    var winLossRatio: Double
    var gamesPlayed: Int
    var wins: Int
    var losses: Int
    var histoMatches : [ Match ]
    
    @State var isEdited : Bool = false
    @ObservedObject var playerVm : PlayerVM //TODO
    //@Published var data : Data
    
    var body: some View {
        //ZStack{
        //Rectangle().fill(Color.bgColor).ignoresSafeArea()
        VStack{
            VStack{
                Image("Blob")
                    .resizable()
                    .aspectRatio(contentMode: .fit)
                    .clipShape(Circle())
                    .overlay(Circle().strokeBorder(Color.orange, lineWidth: 2))
                    .scaleEffect(1)
                    .transformEffect(.identity)
                
                Spacer()
                
                VStack(alignment: .center){
                    Text("\(playerVm.player.name) Sname")
                        .font(.largeTitle)
                        .foregroundColor(Color.white)
                    
                    
                    Text("1")
                        .font(.title2)
                        .foregroundColor(Color.white)
                        .frame(width: 35, height: 35, alignment: .center)
                        .padding(5)
                        .overlay(
                            Circle()
                                .fill(Color.orange.opacity(0.3))
                        )
                    
                }.frame(width: 200, height: 150)
                
                
                
            }.frame(maxWidth: .infinity)
                .padding(20)
                .background(Color.primaryColor)
                .cornerRadius(50)
                .shadow(color: .gray, radius: 3, x: 2, y: 2)
                .padding(20)
            
            
            Spacer()
            
            VStack{
                Text("Statistiques")
                    .fontWeight(.heavy)
                    .font(.largeTitle)
                    .foregroundColor(Color.primaryColor)
                
                Spacer()
                
                DoubleTextDisplay(textL: "Nombre de victoires", textR: "9")
                
                DoubleTextDisplay(textL: "Nombre de défaites", textR: "5")
                
                DoubleTextDisplay(textL: "Nombre de parties jouées", textR: "14")
                
                Spacer()
                
            }
            .foregroundStyle(.white)
            .overlay(
                RoundedRectangle(cornerRadius: 16)
                    .stroke(Color.primaryColor, lineWidth: 5)
            )
            .padding(20)
            
            Spacer()
            
            NavButton("Historique", destinationView: {HistoryView(playerName: playerVm.player.name, matches: histoMatches)})
                .padding(.top, 20)
        }
        .toolbar {
                //ToolbarItemGroup(placement: .bottomBar) {
                Button(action: {isEdited.toggle()}, label: {Text("Edit")})
            //}
        }
        .sheet(isPresented: $isEdited, content: {
            EditModalView(playerVM: playerVm, isEdited: $isEdited)
        })
    //}//Zstack
    }
}

struct PlayerView_Previews: PreviewProvider {
    
    static var previews: some View {
        /*var player1 = new Player(Player(withName: "Meruemu", andSName: "Roi", andId: 2, andAge: 1)*/
        let stubedMatches = [
            Match(gameEndScreen: "opponent1", opponent: "Opponent 1", selfScore: 3, opScore: 2),
            Match(gameEndScreen: "opponent2", opponent: "Opponent 2", selfScore: 1, opScore: 3),
            Match(gameEndScreen: "opponent3", opponent: "Opponent 3", selfScore: 2, opScore: 2)
        ]
        PlayerView(age: 18, winLossRatio: 1.0, gamesPlayed: 10, wins: 5, losses: 5, histoMatches : stubedMatches, playerVm: PlayerVM(with: Player(withName: "toto", andId: .player2)!))
    }
}


