#pragma once
#include "img.hpp"
#include "vertex.hpp"
#include "common.hpp"
#ifdef USE_X
#include <X11/Xutil.h> // -lX11
#include <X11/Xos.h>


class XImg {
    private:
        Img::rgb_t* const buf;
        Img* const img;
        GC gctx;
        XImage* xim;

    public:
        XImg(Display*, Visual*, unsigned w, unsigned h);
        ~XImg();

        Img* get();
        bool draw(Display*, Window);
};


class XWin {
    private:
        Display* display;
        Visual *visual;
        Window window;
        XImg* ximg;

    public:
        XWin(unsigned w, unsigned h);
        ~XWin();

        Img* get();
        bool draw();

        typedef enum { KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT, KEY_FORWARD, KEY_BACK, KEY_PAN_UP, KEY_PAN_DOWN, KEY_PAN_LEFT, KEY_PAN_RIGHT, KEY_QUIT } key_t;
        key_t get_key();
};


#endif


class OutImg {
    private:
        unsigned supersample;
        Img* out;
        Img* out_sampled;
#ifdef USE_X
        XWin* xwin;
#endif

    public:
        OutImg(unsigned ww, unsigned hh, unsigned samp);
        ~OutImg();

        Img* get() { return out; }
        bool put(const char* outdir);

        bool has_x() const;
        bool get_input(coord_t, coord_t, vertex_t&, vertex_t&);
};