use std::{error::Error, rc::Rc};

use common::net::tools::Packet;
use proto::{PlayerLogoutReason, ServerDisconnectClientNotify, SetPlayerBornDataRsp};

use crate::player::Player;

use super::get_proto;

pub fn on_set_player_born_data_req(
    player: &Rc<Player>,
    packet: Packet,
) -> Result<(), Box<dyn Error>> {
    let req = get_proto!(packet, SetPlayerBornDataReq);

    let retcode = player.set_player_born_data(req).err().unwrap_or_default();

    player.send_proto(SetPlayerBornDataRsp {
        retcode: retcode.into(),
    });

    Ok(())
}

pub fn on_player_logout_req(player: &Rc<Player>, packet: Packet) -> Result<(), Box<dyn Error>> {
    let req = get_proto!(packet, PlayerLogoutReq);

    tracing::info!("on_player_logout, uid: {}", player.uid);

    if player.block_stop_coroutine().is_err() {
        return Ok(());
    }

    player.save_to_db();
    if req.reason == PlayerLogoutReason::ClientReq as i32 {
        player.send_proto(ServerDisconnectClientNotify { data: 1 });
    }

    Ok(())
}
