#ifndef MYTHSCREEN_TYPE_H_
#define MYTHSCREEN_TYPE_H_

#include <utility>

// Qt headers
#include <QEvent>
#include <QSemaphore>

// MythTV headers
#include "mythuicomposite.h"
#include "mythuiutils.h"

class ScreenLoadTask;
class MythScreenStack;
class MythUIBusyDialog;

/**
 * \class ScreenLoadCompletionEvent
 *
 * \brief Event that can be dispatched from a MythScreenType when it has
 *        completed loading.
 */
class MUI_PUBLIC ScreenLoadCompletionEvent : public QEvent
{
  public:
    explicit ScreenLoadCompletionEvent(QString id) :
        QEvent(kEventType), m_id(std::move(id)) { }

    QString GetId() { return m_id; }

    static Type kEventType;

  private:
    QString m_id;
};

/**
 * \class MythScreenType
 * \brief Screen in which all other widgets are contained and rendered.
 *
 * \ingroup MythUI_Widgets
 */
class MUI_PUBLIC MythScreenType : public MythUIComposite
{
    Q_OBJECT

    friend class ScreenLoadTask;

  public:
    MythScreenType(MythScreenStack *parent, const QString &name,
                   bool fullscreen = true);
    ~MythScreenType() override;

    virtual bool Create(void); // do the actual work of making the screen.
    bool keyPressEvent(QKeyEvent *event) override; // MythUIType
    bool gestureEvent(MythGestureEvent *event) override; // MythUIType
    virtual void ShowMenu(void);

    void doInit(void);
    void LoadInForeground(void);
    bool IsInitialized(void) const;

    // if the widget is full screen and obscures widgets below it
    bool IsFullscreen(void) const;
    void SetFullscreen(bool full);

    MythUIType *GetFocusWidget(void) const;
    bool SetFocusWidget(MythUIType *widget = nullptr);
    virtual bool NextPrevWidgetFocus(bool up_or_down);
    void BuildFocusList(void);

    MythScreenStack *GetScreenStack() const;

    virtual void aboutToHide(void);
    virtual void aboutToShow(void);

    bool IsDeleting(void) const;
    void SetDeleting(bool deleting);

    bool IsLoading(void) { return m_IsLoading; }
    bool IsLoaded(void) { return m_IsLoaded; }

    MythPainter *GetPainter(void) override; // MythUIType

  public slots:
    virtual void Close();

  signals:
    void Exiting();

  protected:
    // for the global store..
    MythScreenType(MythUIType *parent, const QString &name,
                   bool fullscreen = true);

    void CopyFrom(MythUIType *base) override; // MythUIType
    void CreateCopy(MythUIType *parent) override; // MythUIType
    bool ParseElement( const QString &filename,
                       QDomElement &element,
                       bool showWarnings) override; // MythUIType

    virtual void Load(void);   // ONLY to be used for loading data, NO UI WORK or it will segfault
    virtual void Init(void);   // UI work to draw data loaded

    void LoadInBackground(const QString& message = "");
    void ReloadInBackground(void);

    void OpenBusyPopup(const QString& message = "");
    void CloseBusyPopup(void);
    void SetBusyPopupMessage(const QString &message);
    void ResetBusyPopup(void);

    bool m_FullScreen                {false};
    bool m_IsDeleting                {false};

    QSemaphore m_LoadLock            {1};
    volatile bool m_IsLoading        {false};
    volatile bool m_IsLoaded         {false};
    bool m_IsInitialized             {false};

    MythUIType *m_CurrentFocusWidget {nullptr};
    //TODO We are currently dependant on the internal sorting of QMap for
    //     entries to be iterated in the correct order, this should probably
    //     be changed.
    QMap<int, MythUIType *> m_FocusWidgetList;

    MythScreenStack  *m_ScreenStack  {nullptr};
    MythUIBusyDialog *m_BusyPopup    {nullptr};

    QRegion m_SavedMask;

    friend class XMLParseBase;
};

#endif
