//
//  ShowAllPlayer.swift
//  DouShouQi_App
//
//  Created by Nathan Verdier on 14/06/2024.
//

import SwiftUI

struct ShowAllPlayer: View {
    @State private var isShowingSheet = false
    @Binding var currentShowingSheet: Bool
    
    @ObservedObject var currentPlayer:PlayerVM
    
    @ObservedObject var otherPlayer:PlayerVM
    
    @ObservedObject var playersVM: PlayersVM

    var body: some View {
        VStack(alignment: .leading) {
            Text("Choose a player :")
            List {
                ForEach($playersVM.players, id: \.self) { playerVM in
                    if otherPlayer.player.name != playerVM.player.wrappedValue.name {
                        Button(action: {
                            if currentPlayer.player.name == playerVM.player.wrappedValue.name {
                                currentPlayer.player = Player(name: "IA", photo: "")
                            } else {
                                currentPlayer.player = playerVM.player.wrappedValue
                            }
                            self.currentShowingSheet = false
                        }) {
                            HStack {
                                Image(systemName: "person.fill")
                                    .resizable()
                                    .frame(width: 30, height: 30)
                                Text(playerVM.player.name.wrappedValue)
                            }
                            .padding()
                            .background(Color.blue)
                            .foregroundColor(.white)
                            .cornerRadius(10)
                            .shadow(radius: 3)
                        }
                    }
                }
            }
            Button(action: {
                self.isShowingSheet = true
            }) {
                Text("Add a player")
                    .font(.headline)
                    .foregroundColor(.white)
                    .padding()
                    .frame(maxWidth: .infinity)
                    .background(Color.red)
                    .cornerRadius(10)
                    .padding(.horizontal)
            }
            .padding(.bottom)
            .sheet(isPresented: $isShowingSheet) {
                AddPlayerView(isPresented: $isShowingSheet, playersVM: playersVM)
            }
        }
    }
}

