const std = @import("std");
const Module = @import("../module.zig");
const Self = @This();

module: Module,

pub fn init(allocator: std.mem.Allocator) !Self {
    _ = allocator;
    return .{
        .module = .{ .getBlockFn = getBlock },
    };
}

pub fn getBlock(module: *Module, allocator: std.mem.Allocator) !Module.Block {
    _ = module;
    // const self: *const Self = @fieldParentPtr("module", module);

    var uptime_file = try std.fs.openFileAbsolute("/proc/uptime", .{});
    defer uptime_file.close();

    const uptime_string = try uptime_file.reader().readAllAlloc(allocator, 256);
    var uptime_split = std.mem.splitScalar(u8, uptime_string[0 .. uptime_string.len - 1], ' ');
    var uptime = try std.fmt.parseFloat(f32, uptime_split.first());

    uptime /= 60;
    const mins = @mod(uptime, 60);
    uptime /= 60;
    const hours = @mod(uptime, 24);
    uptime /= 24;

    return .{
        .full_text = try std.fmt.allocPrint(allocator, "{d:0>1.0}d {d:0>1.0}h {d:.0}m", .{ uptime, hours, mins }),
    };
}
