//
//  PlayersView.swift
//  DouShouQi_App
//
//  Created by étudiant on 26/05/2024.
//

import Foundation
import SwiftUI

// Modèle de données pour un joueur
struct Player: Identifiable {
    var id = UUID()
    var name: String
    var wins: Int
    var losses: Int
    var photo: String
}

// Exemple de vue pour l'interface utilisateur des joueurs
struct PlayersView: View {
    // Liste de joueurs pour l'exemple
    @State private var players = [
        Player(name: "Rayhan", wins: 7, losses: 6, photo: "shrek"),
        Player(name: "Remi", wins: 7, losses: 2, photo: "shrek"),
        Player(name: "Nathan", wins: 14, losses: 5, photo: "shrek")
    ]
    
    @State private var searchText = ""
    @State private var showingPopup = false
    @State private var newPlayerName = ""
    
    var filteredPlayers: [Player] {
        if searchText.isEmpty {
            return players
        } else {
            return players.filter { $0.name.lowercased().contains(searchText.lowercased()) }
        }
    }
    
    var body: some View {
        NavigationView {
            VStack {
                
                TitlePageFrame(Text: "PLAYERS", ImageWidth: 200, ImageHeight: 200)
                
                
                SearchBar(text: $searchText)
                    .padding(.horizontal)
                
                List {
                    ForEach(groupedPlayers.keys.sorted(), id: \.self) { key in
                        Section(header: Text(key)) {
                            ForEach(groupedPlayers[key]!) { player in
                                PlayerRow(player: player, players: $players)
                            }
                        }
                    }
                }
                
                Button(action: {
                    showingPopup = true
                }) {
                    Text("Add a player")
                        .font(.headline)
                        .foregroundColor(.white)
                        .padding()
                        .frame(maxWidth: .infinity)
                        .background(Color.red)
                        .cornerRadius(10)
                        .padding(.horizontal)
                }
                .padding(.bottom)
                .sheet(isPresented: $showingPopup) {
                    AddPlayerView(isPresented: $showingPopup, players: $players)
                }
            }
        }
    }
    
    var groupedPlayers: [String: [Player]] {
        Dictionary(grouping: filteredPlayers) { player in
            String(player.name.prefix(1)).uppercased()
        }
    }
}

// Vue pour la barre de recherche
struct SearchBar: UIViewRepresentable {
    @Binding var text: String
    
    class Coordinator: NSObject, UISearchBarDelegate {
        @Binding var text: String
        
        init(text: Binding<String>) {
            _text = text
        }
        
        func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
            text = searchText
        }
    }
    
    func makeCoordinator() -> Coordinator {
        return Coordinator(text: $text)
    }
    
    func makeUIView(context: Context) -> UISearchBar {
        let searchBar = UISearchBar(frame: .zero)
        searchBar.delegate = context.coordinator
        return searchBar
    }
    
    func updateUIView(_ uiView: UISearchBar, context: Context) {
        uiView.text = text
    }
}


struct PlayersPageView_Previews: PreviewProvider {
    static var previews: some View {
        PlayersView()
    }
}
