#pragma once

#include <stdint.h>
#include <vector>

namespace mini
{
    namespace surf
    {
        #define SCREEN 0
        //#define MAX_SURFACES 100

        #define SURF_NOTHING   0
        #define SURF_EXTERNAL  1
        #define SURF_GENERATED 2

        struct surface_t {
            char*       name {nullptr};
            uint8_t*    p {nullptr};
            uint16_t    w, h;
            uint32_t    size;
            int         clip[4]     {0, 0, 0, 0};
            uint8_t     flags {SURF_NOTHING};
        };

        struct state_t {
            std::vector<surface_t>   surfaces;
            int  dest_surface = -1;
            int  source_surface = -1;
        };
        extern state_t state;

        void init();
        void quit();
        
        uint8_t create(int w, int h);
        uint8_t load(const char* filename, const bool external = false);
        uint8_t load(uint8_t* buffer, const char* name);
        void reloadsurfs();
        void save(uint8_t surface, const char* filename, uint8_t *pal, uint8_t colors=0);
        void destroy(uint8_t surface);
        int width(uint8_t surface);
        int height(uint8_t surface);
        void cls(uint8_t color=0);

        namespace target {
            void set(uint8_t surface);
            uint8_t get();
        }

        namespace source {
            void set(uint8_t surface);
            uint8_t get();
        }

        namespace clip {
            void set(int x, int y, int w, int h);
            void reset(int surface=-1);
            void recalculate();
        }
    }
}