const std = @import("std");
const SDL = @import("sdl2");

const Cell = @import("cell.zig");
const utils = @import("utils.zig");

const range = utils.range;

const Self = @This();

pub const ncolumns = 10;
pub const nrows = 20;

cells: [nrows][ncolumns]Cell,

pub fn init() Self {
    var self = Self{
        .cells = undefined,
    };

    for (self.cells) |_, i| {
        for (self.cells[i]) |_, j| {
            self.cells[i][j] = Cell.init();
        }
    }

    return self;
}

pub fn render(self: Self, renderer: *SDL.SDL_Renderer, cell_size: i32, pos_x: i32, pos_y: i32) void {
    var rect = SDL.SDL_Rect{
        .x = pos_x,
        .y = pos_y,
        .w = cell_size,
        .h = cell_size,
    };

    for (self.cells) |_, y| {
        for (self.cells[y]) |_, x| {
            var r = self.cells[y][x].color.r;
            var g = self.cells[y][x].color.g;
            var b = self.cells[y][x].color.b;
            var a = self.cells[y][x].color.a;

            rect.x = pos_x + @intCast(i32, x) * cell_size;
            rect.y = pos_y + @intCast(i32, y) * cell_size;

            _ = SDL.SDL_SetRenderDrawColor(renderer, r, g, b, a);
            _ = SDL.SDL_RenderFillRect(renderer, &rect);
            _ = SDL.SDL_SetRenderDrawColor(renderer, 0x33, 0x33, 0x33, 0xFF);
            _ = SDL.SDL_RenderDrawRect(renderer, &rect);
        }
    }
}
