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 brightness_full_file = try std.fs.openFileAbsolute("/sys/class/backlight/acpi_video0/max_brightness", .{});
    defer brightness_full_file.close();
    var brightness_now_file = try std.fs.openFileAbsolute("/sys/class/backlight/acpi_video0/actual_brightness", .{});
    defer brightness_now_file.close();

    const brightness_full_string = try brightness_full_file.reader().readAllAlloc(allocator, 32);
    const brightness_now_string = try brightness_now_file.reader().readAllAlloc(allocator, 32);
    const brightness_full = try std.fmt.parseInt(u32, brightness_full_string[0 .. brightness_full_string.len - 1], 10);
    const brightness_now = try std.fmt.parseInt(u32, brightness_now_string[0 .. brightness_now_string.len - 1], 10);

    const percent = @as(f32, @floatFromInt(brightness_now)) / @as(f32, @floatFromInt(brightness_full)) * 100;

    return .{
        .full_text = try std.fmt.allocPrint(allocator, "{d:.2}%", .{percent}),
    };
}
