/***************************** LICENSE START ***********************************

 Copyright 2012 ECMWF and INPE. This software is distributed under the terms
 of the Apache License version 2.0. In applying this license, ECMWF does not
 waive the privileges and immunities granted to it by virtue of its status as
 an Intergovernmental Organization or submit itself to any jurisdiction.

 ***************************** LICENSE END *************************************/

#ifndef MvQAbstractMessageExaminer_H
#define MvQAbstractMessageExaminer_H

#include <QModelIndex>
#include <QSettings>

#include "MvMessageMetaData.h"

#include "MvQMainWindow.h"

class QAction;
class QCheckBox;
class QComboBox;
class QHBoxLayout;
class QLabel;
class QListView;
class QSortFilterProxyModel;
class QSplitter;
class QStackedLayout;
class QToolButton;
class QVBoxLayout;

class MessageControlPanel;
class MvKeyProfile;

class MvQArrowSpinWidget;
class MvQFileInfoLabel;
class MvQFileList;
class MvQFileListWidget;
class MvQKeyManager;
class MvQKeyProfileModel;
class MvQKeyProfileSortFilterModel;
class MvQKeyProfileTree;
class MvQLogPanel;
class MvQTreeViewSearchLine;
class MvQTreeView;

class CodesPanel;

class MvQMessagePanel : public QWidget, public MvMessageMetaDataObserver
{
   Q_OBJECT
public:
    explicit MvQMessagePanel(QWidget* parent=0);

    void init(MvMessageMetaData *);
    void loadKeyProfile(MvKeyProfile *prof);
    MvKeyProfile* keyProfile() const;
    void clearProfile();

public slots:
    void selectMessage(int);
    void adjustProfile(bool,int);
    void slotMessageDataInvalid(int msg);

protected slots:
    void selectMessage(QModelIndex);

signals:
    void messageSelected(int);
    void profileLoaded();
    void messageScanProgess(int);
    void keyProfileChanged();

protected:
     void messageScanStepChanged(int);

private:
    void loadKeyProfileCore(MvKeyProfile *prof);

    MvQKeyProfileTree*   tree_;
    MvQKeyProfileModel* model_;
    MvQKeyProfileSortFilterModel* sortModel_;
    MvMessageMetaData *data_;
};


class MvQAbstractMessageExaminer : public MvQMainWindow
{
    Q_OBJECT

public:
    enum Type {GribType,BufrType};

    MvQAbstractMessageExaminer(QString,Type,QWidget *parent = 0);
    ~MvQAbstractMessageExaminer();

    void init(MvMessageMetaData*,MvQKeyManager*,MvQFileList*);
	QString currentKeyProfileName();
	void setCurrentKeyProfileName(QString);

protected slots:
	void slotManageKeyProfiles();
	void slotLoadKeyProfile(int);
    void slotKeyProfileChanged();
	void slotCreateNewProfile();	
	void slotSaveAsCurrentProfile();
	virtual void slotShowAboutBox()=0;  
    void slotAddFile(bool);
    virtual void slotLoadFile(QString)=0;
    void slotClearFilePanel(bool);
    void slotCloseFilePanel(bool);
    void slotSocketMessage(QString msg);

signals:
    void keyProfileChanged();

protected:
    void closeEvent(QCloseEvent *);

    virtual void setupFileMenu();
    virtual void setupEditMenu();
	virtual void setupViewMenu();
	virtual void setupSettingsMenu();
	virtual void setupProfilesMenu();
	virtual void setupHelpMenu();
    void setupMessagePanel();  

    virtual void loadKeyProfile(MvKeyProfile *)=0;
	void loadKeyProfileCore(MvKeyProfile *);
    virtual MvKeyProfile* loadedKeyProfile() const=0;
    void saveKeyProfiles(bool);

    virtual void initMain(MvMessageMetaData*)=0;
    virtual void initDumps()=0;   
	virtual void initAllKeys()=0;

    void updateWinTitle(QString);

	virtual void updateFileInfoLabel()=0;
    void clearLog();

    void readSettings(QSettings& settings);
    void writeSettings(QSettings& settings);

    QString winTitleBase_;
	QString messageType_;
    QString appName_;
    QString settingsName_;

    Type type_;

    MvMessageMetaData *data_;
 	int currentMessageNo_;

    QString sourceFileName_;
    int sourceMessageNum_;

	MvQMainWindow::MenuItemMap menuItems_;

	QComboBox*   keyCombo_;
    MvQKeyManager *keyManager_;
	QString savedKeyProfileName_;

    //Main layout components
    QSplitter*   centralSplitter_;
    QSplitter*   mainSplitter_;
    MvQFileInfoLabel*  fileInfoLabel_;
    QLabel *statusMessageLabel_;

    //File panel
    MvQFileListWidget* filePanel_;
    QAction* actionFilePanel_;
    QAction *actionAddFile_;
    MvQFileList* fileList_;

	QList<MvKeyProfile*> allKeys_;
    qint64 fileSize_;

    QAction* actionFileInfo_;
    MvQLogPanel* logPanel_;

private:
    QAction* actionLog_;
};

#endif
