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

const Grid = @import("grid.zig");
const Piece = @import("piece.zig");
const Bag = @import("bag.zig");

const renderer = @import("renderer.zig");
const movement = @import("movement.zig");

const Self = @This();

grid: Grid,
grid_cell_size: i32,
grid_pos_x: i32,
grid_pos_y: i32,

bag: Bag,

held: ?Piece.Type,
piece: Piece,
shadow: Piece,

renderer: *SDL.SDL_Renderer,

right_action: bool = false,
left_action: bool = false,
down_action: bool = false,
hdrop_action: bool = false,
swap_action: bool = false,
rotate_right_action: bool = false,
rotate_left_action: bool = false,

pub fn init(_renderer: *SDL.SDL_Renderer) Self {
    var ret = Self{
        .grid = Grid.init(),
        .grid_cell_size = 16,
        .grid_pos_x = 200,
        .grid_pos_y = 0,

        .bag = Bag.init(),
        .piece = undefined,
        .shadow = undefined,
        .held = null,
        .renderer = _renderer,
    };
    ret.piece = ret.bag.pop();
    // ret.shadow = movement.shadow(ret.grid, ret.piece);
    // ret.shadow.color.a /= 2;
    return ret;
}
// fn render(self: Self) void {
//     self.grid.render(self.renderer, self.grid_cell_size, self.grid_pos_x, self.grid_pos_y);
//     self.piece.render(self.renderer, self.grid_cell_size, self.grid_pos_x, self.grid_pos_y);
//     renderer.renderBag(self.renderer, self.bag, self.grid_cell_size, self.grid_pos_x + self.grid_cell_size * (Grid.ncolumns + 1), self.grid_pos_y);
//     if (self.held) |held_piece| {
//         renderer.renderHeld(self.renderer, held_piece, self.grid_cell_size, self.grid_pos_x - (self.grid_cell_size * 5), self.grid_pos_y);
//     }
// }
pub fn tick(self: *Self) void {

    var key_state = SDL.SDL_GetKeyboardState(null);

    // MOVEMENT

    // GO RIGHT
    if (key_state[SDL.SDL_SCANCODE_D] == 1 and !self.right_action) {
        self.right_action = true;
        self.piece = movement.moveRight(self.grid, self.piece);
    }
    if (key_state[SDL.SDL_SCANCODE_D] == 0) {
        self.right_action = false;
    }
    // GO LEFT
    if (key_state[SDL.SDL_SCANCODE_A] == 1 and !self.left_action) {
        self.left_action = true;
        self.piece = movement.moveLeft(self.grid, self.piece);
    }
    if (key_state[SDL.SDL_SCANCODE_A] == 0) {
        self.left_action = false;
    }
    // GO DOWN
    if (key_state[SDL.SDL_SCANCODE_S] == 1 and !self.down_action) {
        self.down_action = true;
        self.piece = movement.moveDown(self.grid, self.piece);
    }
    if (key_state[SDL.SDL_SCANCODE_S] == 0) {
        self.down_action = false;
    }

    // HARD DROP
    if (key_state[SDL.SDL_SCANCODE_W] == 1 and !self.hdrop_action) {
        self.hdrop_action = true;
        self.piece = movement.hardDrop(self.grid, self.piece);
    }
    if (key_state[SDL.SDL_SCANCODE_W] == 0) {
        self.hdrop_action = false;
    }

    // SWAP PIECES
    if (key_state[SDL.SDL_SCANCODE_SPACE] == 1 and !self.swap_action) {
        self.swap_action = true;
        self.swapHeld();
    }
    if (key_state[SDL.SDL_SCANCODE_SPACE] == 0) {
        self.swap_action = false;
    }

    // ROTATE
    if (key_state[SDL.SDL_SCANCODE_RIGHT] == 1 and !self.rotate_right_action) {
        self.rotate_right_action = true;
        self.piece = movement.rotateRight(self.grid, self.piece);
    }
    if (key_state[SDL.SDL_SCANCODE_RIGHT] == 0) {
        self.rotate_right_action = false;
    }

    if (key_state[SDL.SDL_SCANCODE_LEFT] == 1 and !self.rotate_left_action) {
        self.rotate_left_action = true;
        self.piece = movement.rotateLeft(self.grid, self.piece);
    }
    if (key_state[SDL.SDL_SCANCODE_LEFT] == 0) {
        self.rotate_left_action = false;
    }

    // DROP

    if (self.piece.dropped) {
        self.grid = movement.drop(self.grid, self.piece);
        self.piece = self.bag.pop();
    }

    // Update Shadow
    self.shadow = movement.shadow(self.grid, self.piece);
    self.shadow.color.a /= 4;

    self.grid.clearLines();

    renderer.render(self.*);
}
fn swapHeld(self: *Self) void {
    if (self.held) |held_piece| {
        if (!self.piece.swapped) {
            self.held = self.piece.piece_type;
            self.piece = Piece.init(held_piece);
            self.piece.swapped = true;
        }
    } else {
        self.held = self.piece.piece_type;
        self.piece = self.bag.pop();
        self.piece.swapped = true;
    }
}
