const std = @import("std");
const protocol = @import("protocol");
const Session = @import("../Session.zig");
const Packet = @import("../Packet.zig");

const Allocator = std.mem.Allocator;
const CmdID = protocol.CmdID;

const B64Decoder = std.base64.standard.Decoder;

pub fn onPlayerHeartBeat(session: *Session, packet: *const Packet, allocator: Allocator) !void {
    const req = try packet.getProto(protocol.PlayerHeartBeatCsReq, allocator);
    const lua_code =
        \\local function setTextComponent(path, newText)
        \\    local obj = CS.UnityEngine.GameObject.Find(path)
        \\    if obj then
        \\        local textComponent = obj:GetComponentInChildren(typeof(CS.RPG.Client.LocalizedText))
        \\        if textComponent then
        \\            textComponent.text = newText
        \\        end
        \\    end
        \\end
        \\
        \\setTextComponent("UIRoot/AboveDialog/BetaHintDialog(Clone)", "<color=#ffc800>CipherSR is a free and open source software.</color>")
        \\setTextComponent("VersionText", "<color=#ffc800>Visit discord.gg/reversedrooms for more info!</color>")
    ;
    const data = protocol.ClientDownloadData{
        .version = 51,
        .time = @intCast(std.time.milliTimestamp()),
        .data = .{ .Owned = .{
            .allocator = allocator,
            .str = try allocator.dupe(u8, lua_code),
        } },
    };
    const rsp = protocol.PlayerHeartBeatScRsp{
        .retcode = 0,
        .client_time_ms = req.client_time_ms,
        .server_time_ms = @intCast(std.time.milliTimestamp()),
        .download_data = data,
    };
    try session.send(CmdID.CmdPlayerHeartBeatScRsp, rsp);
}
