package body Grids is


    function Create
           (X, Y, W, H : in Integer;
            Text       : in String)
        return Grid is
    begin
        return This : Grid :=
            (FLTK.Widgets.Widget'(FLTK.Widgets.Forge.Create (X, Y, W, H, Text)) with
                Cells => Square_Vector_Vectors.Empty_Vector,
                Rows => 0, Cols => 0);
    end Create;


    function Create
           (X, Y : in Integer)
        return Grid is
    begin
        return Create (X, Y, 0, 0, "Sokoban Grid");
    end Create;




    procedure Set_Cols
           (This : in out Grid;
            To   : in     Natural) is
    begin
        This.Cells.Set_Length (Ada.Containers.Count_Type (To));
        for X in Integer range This.Cols + 1 .. To loop
            This.Cells.Replace_Element (X, Square_Vectors.Empty_Vector);
            This.Cells.Reference (X).Set_Length (Ada.Containers.Count_Type (This.Rows));
            for Y in Integer range 1 .. This.Rows loop
                This.Cells.Reference (X).Replace_Element (Y, Squares.Void);
            end loop;
        end loop;
        This.Cols := To;
        This.Resize (Step * This.Cols, This.Get_H);
    end Set_Cols;


    procedure Set_Rows
           (This : in out Grid;
            To   : in     Natural) is
    begin
        for X in Integer range 1 .. This.Cols loop
            This.Cells.Reference (X).Set_Length (Ada.Containers.Count_Type (To));
            for Y in Integer range This.Rows + 1 .. To loop
                This.Cells.Reference (X).Replace_Element (Y, Squares.Void);
            end loop;
        end loop;
        This.Rows := To;
        This.Resize (This.Get_W, Step * This.Rows);
    end Set_Rows;




    function In_Bounds
           (This : in Grid;
            X, Y : in Integer)
        return Boolean is
    begin
        return X in 1 .. This.Cols and Y in 1 .. This.Rows;
    end In_Bounds;


    function Get_Square
           (This : in Grid;
            X, Y : in Integer)
        return Squares.Square is
    begin
        if not This.In_Bounds (X, Y) then
            return Squares.Void;
        else
            return This.Cells.Element (X).Element (Y);
        end if;
    end Get_Square;


    procedure Set_Square
           (This : in out Grid;
            X, Y : in     Integer;
            Item : in     Squares.Square) is
    begin
        This.Cells.Reference (X).Reference (Y) := Item;
    end Set_Square;




    procedure Pixel_To_Colrow
           (This : in     Grid;
            X, Y : in     Integer;
            C, R :    out Integer) is
    begin
        C := X / Step + 1;
        R := Y / Step + 1;
    end Pixel_To_Colrow;


    procedure Colrow_To_Pixel
           (This : in     Grid;
            C, R : in     Integer;
            X, Y :    out Integer) is
    begin
        X := (C - 1) * Step;
        Y := (R - 1) * Step;
    end Colrow_To_Pixel;




    procedure Draw
           (This : in out Grid) is
    begin
        for Y in Integer range 1 .. This.Rows loop
            for X in Integer range 1 .. This.Cols loop
                This.Cells.Reference (X).Reference (Y).Draw
                   (This.Get_X + (X - 1) * Step,
                    This.Get_Y + (Y - 1) * Step);
            end loop;
        end loop;
    end Draw;


end Grids;