use std::{
    sync::mpsc::{self, Receiver, Sender},
    thread,
};

use common::net::tools::Packet;

pub struct PlayerCoroExec {
    tx: Sender<Packet>,
}

impl PlayerCoroExec {
    pub fn start<F: 'static + Fn(Receiver<Packet>) -> () + Send>(coro_loop_func: F) -> Self {
        let (tx, rx) = mpsc::channel();
        thread::spawn(move || coro_loop_func(rx));

        Self { tx }
    }

    pub fn push_packet(&self, packet: Packet) {
        let _ = self.tx.send(packet).inspect_err(|err| {
            tracing::error!("PlayerCoroExec: tx.send() failed: {err}");
        });
    }
}
