 #ifndef WIDGET_H
#define WIDGET_H
#include "display.h"
	
class Widget {
protected:
	bool _initialized = false;
    bool _visible = true;
    bool _resumeRequired = false;
    bool _cleared = false;
	bool _forceDraw = false;
    bool _clearRequested = false;
	uint8_t _scr_color = 0, _text_color = 15;
	uint16_t _w, _h;
    int16_t _x, _y;
	int16_t _z = 0;
    MyDisplay* _dsp = nullptr;

public:
	virtual ~Widget() = default;
	
    void init(MyDisplay* dsp, int16_t x, int16_t y, uint16_t w, uint16_t h) {
        _dsp = dsp;
        _x = x;
        _y = y;
        _w = w;
        _h = h;
    }

    virtual void draw( void ) = 0;
    virtual void redraw( void ) = 0;

    virtual void loop( void ){
			
			if( !_initialized || !_dsp ) return;

			if( _clearRequested || !_visible ){
				
					if( _clearRequested && !_cleared ){
						_dsp->fillRect( _x, _y, _w, _h, _scr_color );
						_cleared = true;
					}

					if( !_resumeRequired ){
						_clearRequested = false;
					}
					
				return;
			}

        draw();
    }

    virtual void clear(bool resume_required = true) {
        _cleared = false;
        _clearRequested = true;
		_visible = false;
        _resumeRequired = resume_required;
    }

    virtual void resume(void) {
        _clearRequested = false;
        _resumeRequired = false;
        _visible = true;
    }
	
	virtual void setScreenColor( uint8_t c ){
		_scr_color = c;
	}

    virtual void setVisible(bool v) { _visible = v; }
    virtual bool visible() const { return !_clearRequested && _visible; }
	
    virtual int16_t x() const { return _x; }
    virtual int16_t y() const { return _y; }
    virtual uint16_t w() const { return _w; }
    virtual uint16_t h() const { return _h; }
	
    virtual void setZ(int16_t z) { _z = z; }
    virtual int16_t getZ() const { return _z; }
	
	virtual bool intersects(int16_t x, int16_t y, uint16_t w, uint16_t h) const {
		return !(
			_x + _w <= x ||
			x + w <= _x ||
			_y + _h <= y ||
			y + h <= _y
		);
	}
};
#endif