#ifndef MONOFORMAT_SCHEMA_HPP
#define MONOFORMAT_SCHEMA_HPP

#include "monoformat_bithelpers.hpp"

#include <cstddef>
#include <cstdint>
#include <span>
#include <tuple>

namespace monoformat {

enum class SectionType {
    AlwaysDrawn = 1,
    TimeBasedDrawn = 2,
    MultiTimeBasedDrawn = 3,
    ExpiryDate = 31,
    CustomFont = 32,
};

enum class ElementType {
    Image = 1,
    Animation = 2,
    HScrollImage = 3,
    VScrollImage = 4,
    Line = 5,
    Box = 6,
    ClippedText = 16,
    HScrollText = 17,
    //VScrollText = 18,
    CurrentTime = 32,
};

enum class LineStyle : std::uint8_t {
    Solid = 0,
};

struct ScrollFlags {
    bool endless{};
    bool invertDirection{};
    bool padBefore{};
    bool padAfter{};

    ScrollFlags() = default;

    explicit ScrollFlags(std::uint8_t flags) noexcept
        : endless{isBitSet(flags, 0)}
        , invertDirection{isBitSet(flags, 1)}
        , padBefore{isBitSet(flags, 2)}
        , padAfter{isBitSet(flags, 3)}
    {
    }

    explicit operator std::uint8_t() const noexcept {
        std::uint8_t result{};
        maybeSetBit(result, 0, endless);
        maybeSetBit(result, 1, invertDirection);
        maybeSetBit(result, 2, padBefore);
        maybeSetBit(result, 3, padAfter);
        return result;
    }
};

struct LineFlags {
    bool dark{};

    LineFlags() = default;

    explicit LineFlags(std::uint8_t flags) noexcept
        : dark{isBitSet(flags, 0)}
    {
    }

    explicit operator std::uint8_t() const noexcept {
        std::uint8_t result{};
        maybeSetBit(result, 0, dark);
        return result;
    }
};

enum class FillPattern : std::uint8_t {
    Solid = 0,
};

struct FillFlags {
    bool dark{};

    FillFlags() = default;

    explicit FillFlags(std::uint8_t flags) noexcept
        : dark{isBitSet(flags, 0)}
    {
    }

    explicit operator std::uint8_t() const noexcept {
        std::uint8_t result{};
        maybeSetBit(result, 0, dark);
        return result;
    }
};

struct TextFlags {
    bool dark{};
    bool autoWordWrap{};

    TextFlags() = default;

    explicit TextFlags(std::uint8_t flags) noexcept
        : dark{isBitSet(flags, 0)}
        , autoWordWrap{isBitSet(flags, 1)}
    {
    }

    explicit operator std::uint8_t() const noexcept {
        std::uint8_t result{};
        maybeSetBit(result, 0, dark);
        maybeSetBit(result, 1, autoWordWrap);
        return result;
    }
};

struct TimeDisplayFlags {
    bool use12h{};
    bool showHours{};
    bool showMinutes{};
    bool showSeconds{};

    TimeDisplayFlags() = default;

    explicit TimeDisplayFlags(std::uint16_t flags) noexcept
        : use12h{isBitSet(flags, 0)}
        , showHours{isBitSet(flags, 1)}
        , showMinutes{isBitSet(flags, 2)}
        , showSeconds{isBitSet(flags, 3)}
    {
    }

    explicit operator std::uint16_t() const noexcept {
        std::uint16_t result{};
        maybeSetBit(result, 0, use12h);
        maybeSetBit(result, 1, showHours);
        maybeSetBit(result, 2, showMinutes);
        maybeSetBit(result, 3, showSeconds);
        return result;
    }
};

struct OneBitBufferInterface {
    using Color = bool;

    virtual ~OneBitBufferInterface() = default;
    virtual bool isPixelSet(std::uint16_t x, std::uint16_t y) const = 0;
    virtual void setPixel(std::uint16_t x, std::uint16_t y, bool value) = 0;
};

struct MemoryOneBitBuffer : public OneBitBufferInterface {
    MemoryOneBitBuffer(std::span<std::byte> buffer, std::uint16_t width, std::uint16_t height);
    virtual ~MemoryOneBitBuffer() override;
    virtual bool isPixelSet(std::uint16_t x, std::uint16_t y) const override;
    virtual void setPixel(std::uint16_t x, std::uint16_t y, bool value) override;
private:
    std::uint16_t m_width{};
    std::uint16_t m_height{};
    std::span<std::byte> m_buffer;
};

struct ConstMemoryOneBitBuffer : public OneBitBufferInterface {
    ConstMemoryOneBitBuffer(std::span<std::byte const> buffer, std::uint16_t width, std::uint16_t height);
    virtual ~ConstMemoryOneBitBuffer() override;
    virtual bool isPixelSet(std::uint16_t x, std::uint16_t y) const override;
    virtual void setPixel(std::uint16_t x, std::uint16_t y, bool value) override;
private:
    std::uint16_t m_width{};
    std::uint16_t m_height{};
    std::span<std::byte const> m_buffer;
};

struct ClippedImage : public OneBitBufferInterface {
    ClippedImage(OneBitBufferInterface* underlying, std::uint16_t x, std::uint16_t y, std::uint16_t width, std::uint16_t height);
    virtual ~ClippedImage() override;
    virtual bool isPixelSet(std::uint16_t x, std::uint16_t y) const override;
    virtual void setPixel(std::uint16_t x, std::uint16_t y, bool value) override;
private:
    OneBitBufferInterface* m_underlying{};
    std::uint16_t m_x{};
    std::uint16_t m_y{};
    std::uint16_t m_width{};
    std::uint16_t m_height{};
};

} // namespace monoformat

#endif // MONOFORMAT_SCHEMA_HPP
