/**
 * @file Canvas.h
 * @author apio (cloudapio.eu)
 * @brief Drawable surfaces.
 *
 * @copyright Copyright (c) 2023, the Luna authors.
 *
 */

#pragma once
#include <luna/Result.h>
#include <luna/Types.h>
#include <ui/Color.h>
#include <ui/Point.h>
#include <ui/Rect.h>

namespace ui
{
    /**
     * @brief A drawable surface.
     */
    struct Canvas
    {
        int width;
        int height;
        int stride;
        u8* ptr;

        /**
         * @brief Create a new Canvas object.
         *
         * @param ptr The memory to use for the canvas. It must be of at least width * height * 4 bytes of length.
         * @param width The width of the canvas.
         * @param height The height of the canvas.
         * @return Canvas The new Canvas object.
         */
        static Canvas create(u8* ptr, int width, int height);

        /**
         * @brief Return a new Canvas that represents a subsection of the current one.
         *
         * @param rect The dimensions of the new canvas. If these exceed the bounds of the current canvas, they will be
         * clamped.
         * @return Canvas The new Canvas object.
         */
        Canvas subcanvas(Rect rect);

        /**
         * @brief Return the dimensions of the current canvas.
         *
         * @return Rect This canvas's dimensions, as a Rect object.
         */
        Rect rect()
        {
            return Rect { .pos = { 0, 0 }, .width = width, .height = height };
        }

        /**
         * @brief Fill the entire canvas with one color.
         *
         * @param color The color to use.
         */
        void fill(Color color);

        /**
         * @brief Fill the canvas with pixels.
         *
         * @param pixels The array of pixels (must be at least width*height).
         * @param stride The number of pixels to skip to go to the next line.
         */
        void fill(u32* pixels, int stride);
    };
};