#ifndef _OPTIONS_H_
#define _OPTIONS_H_

#include <QSettings>
#include "ui_options.h"

#if QMC2_JOYSTICK == 1
#include <QWidget>
#include <QMap>
#include <QLabel>
#include <QProgressBar>
#include <QCheckBox>
#include <QSpinBox>
#include <QScrollArea>
#include "joystick.h"

class JoystickCalibrationWidget : public QWidget
{
  Q_OBJECT

  public:
    Joystick *myJoystick;
    QMap<int, QCheckBox *> axesEnablers;
    QMap<int, QProgressBar *> axesRanges;
    QMap<int, QSpinBox *> axesDeadzones;
    QMap<int, QSpinBox *> axesSensitivities;
    QMap<int, QToolButton *> axesButtons;
    QMap<int, QLabel *> sensitivityLabels;
    QMap<int, QLabel *> deadzoneLabels;
    QMap<int, int> axesMinimums;
    QMap<int, int> axesMaximums;
    QGridLayout *myLayout;

    JoystickCalibrationWidget(Joystick *joystick, QWidget *parent = 0);
    ~JoystickCalibrationWidget();

  public slots:
    void on_joystickAxisValueChanged(int, int);
    void on_joystickButtonValueChanged(int, bool);
    void on_joystickHatValueChanged(int, int);
    void on_joystickTrackballValueChanged(int, int, int);
    void on_resetAxisCalibration();
    void on_deadzoneValueChanged(int);
    void on_sensitivityValueChanged(int);
    void on_axisEnablerStateChanged(int);
};

class JoystickTestWidget : public QWidget
{
  Q_OBJECT

  public:
    Joystick *myJoystick;
    QMap<int, QProgressBar *> axesRanges;
    QMap<int, QLabel *> buttonLabels;
    QMap<int, QLabel *> hatValueLabels;
    QMap<int, QLabel *> trackballDeltaXLabels;
    QMap<int, QLabel *> trackballDeltaYLabels;
    QGridLayout *myLayout;

    JoystickTestWidget(Joystick *joystick, QWidget *parent = 0);
    ~JoystickTestWidget();

  public slots:
    void on_joystickAxisValueChanged(int, int);
    void on_joystickButtonValueChanged(int, bool);
    void on_joystickHatValueChanged(int, int);
    void on_joystickTrackballValueChanged(int, int, int);
    void cleanupPalette();
};
#endif

class Options : public QDialog, public Ui::Options
{
  Q_OBJECT

  public:
    QSettings *config;
    bool applied;
#if QMC2_JOYSTICK == 1
    Joystick *joystick;
    JoystickCalibrationWidget *joystickCalibrationWidget;
    JoystickTestWidget *joystickTestWidget;
    QScrollArea *scrollArea;
#endif

    Options(QWidget *parent = 0);
    ~Options();

    void apply();
    void restoreCurrentConfig(bool useDefaultSettings = FALSE);

  public slots:
    // callback functions
    void on_pushButtonOk_clicked();
    void on_pushButtonCancel_clicked();
    void on_pushButtonRestore_clicked();
    void on_pushButtonApply_clicked();
    void on_pushButtonDefault_clicked();
    void on_toolButtonBrowseTemporaryFile_clicked();
    void on_toolButtonBrowseFrontendLogFile_clicked();
    void on_toolButtonBrowseEmulatorLogFile_clicked();
    void on_toolButtonBrowseFavoritesFile_clicked();
    void on_toolButtonBrowseHistoryFile_clicked();
    void on_toolButtonBrowseGamelistCacheFile_clicked();
    void on_toolButtonBrowseROMStateCacheFile_clicked();
    void on_toolButtonBrowsePreviewDirectory_clicked();
    void on_toolButtonBrowsePreviewFile_clicked();
    void on_toolButtonBrowseFlyerDirectory_clicked();
    void on_toolButtonBrowseFlyerFile_clicked();
    void on_toolButtonBrowseIconDirectory_clicked();
    void on_toolButtonBrowseIconFile_clicked();
    void on_toolButtonBrowseExecutableFile_clicked();
    void on_toolButtonBrowseDataDirectory_clicked();
    void on_toolButtonBrowseGameInfoDB_clicked();
#if defined(QMC2_SDLMAME)
    void on_toolButtonBrowseEmuInfoDB_clicked();
#endif
    void on_toolButtonBrowseOptionsTemplateFile_clicked();
    void on_toolButtonBrowseListXMLCache_clicked();
    void on_toolButtonBrowseFont_clicked();
    void on_toolButtonBrowseZipTool_clicked();
    void on_toolButtonBrowseFileRemovalTool_clicked();
    void on_radioButtonPreviewSelect_clicked();
    void on_radioButtonFlyerSelect_clicked();
    void on_radioButtonIconSelect_clicked();
    void on_treeWidgetShortcuts_itemActivated(QTreeWidgetItem *item);
    void on_treeWidgetShortcuts_itemSelectionChanged();
    void on_pushButtonRedefineKeySequence_clicked();
    void on_pushButtonResetShortcut_clicked();

    // joystick setup callbacks
#if QMC2_JOYSTICK == 1
    void on_pushButtonRescanJoysticks_clicked();
    void on_toolButtonCalibrateAxes_clicked();
    void on_toolButtonTestJoystick_clicked();
    void on_toolButtonMapJoystick_clicked();
    void on_comboBoxSelectJoysticks_currentIndexChanged(int);
    void on_checkBoxEnableJoystickControl_toggled(bool);
    void on_checkBoxJoystickAutoRepeat_toggled(bool);
    void on_spinBoxJoystickAutoRepeatTimeout_valueChanged(int);
    void on_spinBoxJoystickEventTimeout_valueChanged(int);
    void on_treeWidgetJoystickMappings_itemActivated(QTreeWidgetItem *item);
    void on_treeWidgetJoystickMappings_itemSelectionChanged();
    void on_pushButtonRemapJoystickFunction_clicked();
    void on_pushButtonRemoveJoystickMapping_clicked();
    void checkJoystickMappings();
#endif

    // other slots
    void checkShortcuts();
    void setupShortcutActions();
    void applyDelayed();

  protected:
    void closeEvent(QCloseEvent *);
    void showEvent(QShowEvent *);
    void hideEvent(QHideEvent *);
    void moveEvent(QMoveEvent *);
    void resizeEvent(QResizeEvent *);
};

#endif
