use tokio::{io::AsyncReadExt, net::TcpStream};
use std::net::SocketAddr;
use serde::{Deserialize, Serialize};
use sha256::digest;

use crate::{card::Card, message_read::MessageReader};

#[derive(Serialize, Deserialize, Eq, PartialEq, Hash, Clone)]
pub struct Player {
    // addr will change because at this stage i really
    // don't know what i am doing
    // it will probably be something like id or playerid
    // or some other identifier idk
    pub addr: SocketAddr,
    // same goes for id because rn it's just the sha256 of
    // the ip
    pub id: String,
    pub name: String,
    pub hand: Vec<Card>,
}

impl Player {
    pub async fn new(addr: SocketAddr, name: &str) -> anyhow::Result<Option<Self>> {
        let hand_empty: Vec<Card> = Vec::new();

        let to_digest: String = addr.to_string();
        let id = digest(to_digest);

        Ok(Some(Player {
            addr,
            id,
            name: name.to_string(),
            hand: hand_empty,
        }))
    }

    pub fn get_addr(self) -> SocketAddr {
        self.addr
    }

    pub fn get_name(self) -> String {
        self.name
    }

    pub fn get_hand(self) -> Vec<Card> {
        self.hand
    }
}