/*
 * Copyright 2023 Hartmut Seichter
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */
#ifndef PARADISO_BITMAP_HPP
#define PARADISO_BITMAP_HPP

#include <paradiso/geometry.hpp>
#include <paradiso/globals.hpp>
#include <paradiso/rgba.hpp>

namespace paradiso {
/**
 * @brief aggregate to handle bitmap data
 */
struct Bitmap final {

    /**
     * @brief creates an empty bitmap
     * @param[in] size extent of bitmap
     * @return an empty bitmap
     */
    constexpr static Bitmap empty(Size size) noexcept {
        return {.size = size, .data = std::vector<RGBA>{size.area()}};
    }

    /**
     * @brief creates a bitmap from manually adding data
     * @param[in] size extent of bitmap
     * @param[in] values values to set the bitmap from
     * @return bitmap with data from this call
     */
    template <typename... Arguments>
    static constexpr Bitmap from_data(Size size, Arguments... values) noexcept {
        assert(sizeof...(Arguments) == size.height * size.width);
        return {.size = size, .data = {values...}};
    }

    /**
     * @brief fills a bitmap uniformly
     * @param[in] color RGBA value of a color
     * @return reference to itself
     */
    constexpr auto fill(const RGBA& color) noexcept {
        change_count++;
        std::fill(data.begin(), data.end(), color);
        return *this;
    }

    /**
     * @brief access a pixel by position
     * @param[in] x x-component of position
     * @param[in] y y-component of position
     * @return reference to pixel data at position
     */
    constexpr RGBA& pixel(std::integral auto x, std::integral auto y) {
        change_count++;
        return data[y * size.width + x];
    }

    /**
     * @brief access a pixel by position
     * @param[in] x x-component of position
     * @param[in] y y-component of position
     * @return const reference to pixel data at position
     */
    constexpr const RGBA& pixel(std::integral auto x,
                                std::integral auto y) const {
        return data[y * size.width + x];
    }

    constexpr void force_change() noexcept
    {
        change_count++;
    } 

    Size size{.width = 0, .height = 0}; //!< extent of bitmap
    std::vector<RGBA> data{};           //!< data storage
    std::uint64_t change_count{};
};
} // namespace paradiso

#endif