/*******************************************************************************************************
 DkControlWidget.h
 Created on:	31.08.2015

 nomacs is a fast and small image viewer with the capability of synchronizing multiple instances

 Copyright (C) 2011-2013 Markus Diem <markus@nomacs.org>
 Copyright (C) 2011-2013 Stefan Fiel <stefan@nomacs.org>
 Copyright (C) 2011-2013 Florian Kleber <florian@nomacs.org>

 This file is part of nomacs.

 nomacs is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 nomacs is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.

 *******************************************************************************************************/

#pragma once

#include <QSharedPointer>

#include "DkBaseWidgets.h"
#include "DkImageContainer.h"

class QStackedLayout;
class QGridLayout;
class QLabel;

namespace nmc
{
class DkFilePreview;
class DkMetaDataHUD;
class DkCommentWidget;
class DkViewPort;
class DkCropWidget;
class DkZoomWidget;
class DkPlayer;
class DkFolderScrollBar;
class DkDelayedMessage;
class DkFileInfoLabel;
class DkHistogram;
class DkLabelBg;
class DkPluginViewPort;
class DkOverview;
class DkViewPortInterface;
class DkThumbLoader;

class DllCoreExport DkControlWidget : public DkWidget
{
    Q_OBJECT

public:
    enum VerPos {
        top_scroll = 0,
        top_thumbs,
        top_metadata,
        top_info,
        ver_center,
        bottom_info,
        bottom,
        bottom_metadata,
        bottom_thumbs,
        ver_pos_end
    };
    enum HorPos {
        left_thumbs = 0,
        left_metadata,
        left,
        hor_center,
        right,
        right_metadata,
        right_thumbs,
        hor_pos_end
    };

    enum InfoPos {
        bottom_left_label,
        bottom_right_label,
        top_left_label
    };

    enum Widgets {
        last_widget = -1,
        hud_widget,
        crop_widget,

        widget_end
    };

    explicit DkControlWidget(DkThumbLoader *thumbLoader,
                             DkViewPort *parent = nullptr,
                             Qt::WindowFlags flags = Qt::WindowFlags());
    ~DkControlWidget() override = default;

    void setFullScreen(bool fullscreen);

    DkFilePreview *getFilePreview() const;
    DkFolderScrollBar *getScroller() const;
    DkOverview *getOverview() const;
    DkZoomWidget *getZoomWidget() const;
    DkPlayer *getPlayer() const;
    DkFileInfoLabel *getFileInfoLabel() const;
    DkHistogram *getHistogram() const;
    DkCropWidget *getCropWidget() const;

    void showWidgetsSettings();
    void setWidgetsVisible(bool visible, bool saveSettings = false);

    void settingsChanged();

public slots:
    void toggleHUD(bool hide);
    void showPreview(bool visible);
    void showMetaData(bool visible);
    void showFileInfo(bool visible);
    void showPlayer(bool visible);
    void startSlideshow(bool start = true);
    void hideCrop(bool hide = true);
    void showCrop(bool visible);
    void showOverview(bool visible);
    void showHistogram(bool visible);
    void showCommentWidget(bool visible);
    void switchWidget(QWidget *widget = nullptr);
    void changeMetaDataPosition(int pos);
    void changeThumbNailPosition(int pos);
    void showScroller(bool visible);
    void setPluginWidget(DkViewPortInterface *pluginWidget, bool removeWidget);

    bool closePlugin(bool askForSaving, bool force = false);
    bool applyPluginChanges(bool askForSaving);

    void updateImage(QSharedPointer<DkImageContainerT> imgC, bool updateMetadataIfNull = true);

    void setInfo(const QString &msg, int time = 3000, int location = bottom_left_label);
    void updateRating(int rating);

    void imagePresenceChanged(bool imagePresent);

    void update();

private:
    void onImageContainerInternalUpdated();

protected slots:
    void setCommentSaved(const QString &comment);

    // signals from viewport plugins
    void pluginClosed(bool askForSaving);
    void pluginMessage(const QString &msg);
    void pluginLoadFile(const QString &path);
    void pluginLoadImage(const QImage &img);

protected:
    // events
    void mousePressEvent(QMouseEvent *event) override;
    void mouseReleaseEvent(QMouseEvent *event) override;
    void mouseMoveEvent(QMouseEvent *event) override;

    void keyPressEvent(QKeyEvent *event) override;
    void keyReleaseEvent(QKeyEvent *event) override;

    // functions
    void init();
    void connectWidgets();

    // layout (switching of HUD contexts)
    QVector<QWidget *> mWidgets;
    QStackedLayout *mLayout;
    QGridLayout *mHudLayout;

    DkViewPort *mViewport;
    DkCropWidget *mCropWidget;

    DkFilePreview *mFilePreview;
    DkMetaDataHUD *mMetaDataInfo;
    DkCommentWidget *mCommentWidget;
    DkZoomWidget *mZoomWidget;
    DkPlayer *mPlayer;
    DkHistogram *mHistogram;

    DkFolderScrollBar *mFolderScroll;
    DkFileInfoLabel *mFileInfoLabel;

    DkDelayedMessage *mDelayedInfo;

    DkLabelBg *mBottomLabel;
    DkLabelBg *mBottomLeftLabel;

    DkPluginViewPort *mPluginViewport = nullptr;

    QSharedPointer<DkImageContainerT> mImgC;

    QLabel *mWheelButton;

    QPointF mEnterPos;
};
}
