//
//  PlayersVM.swift
//  DouShouQi_App
//
//  Created by étudiant on 12/06/2024.
//

import Foundation
import DouShouQiModel
import CoreData
import Combine

public class PlayersVM: ObservableObject{
    
    @Published var players: [PlayerVM]
    
    public func SavePlayer(player: PlayerVM){
        players.append(player)
        CoreDataManager.shared.savePlayer(playerVM: player)
    }
    
    public func contains(where predicate: (PlayerVM) -> Bool) -> Bool {
        return players.contains(where: predicate)
    }
    
    
    public func getAllPlayer() -> [PlayerVM]{
        let coreDataPlayers = CoreDataManager.shared.fetchPlayers()
        self.players = coreDataPlayers.map { PlayerVM(player: $0.toModel()) }
        return players
    }
    
    init() {
        players = []
        
        players = getAllPlayer()
        
       /* let player = PlayerVM(player: Player(name: "Rayhan", photo: ""))
        players.append(player)
        let player2 = PlayerVM(player: Player(name: "Nathan", photo: ""))
        players.append(player2)
        let player3 = PlayerVM(player: Player(name: "ayoub", photo: ""))
        players.append(player3) */
    }
    
}
