/*
 * Rosalie's Mupen GUI - https://github.com/Rosalie241/RMG
 *  Copyright (C) 2020 Rosalie Wanders <rosalie@mailbox.org>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 3.
 *  You should have received a copy of the GNU General Public License
 *  along with this program. If not, see <https://www.gnu.org/licenses/>.
 */
#ifndef SETTINGSDIALOG_HPP
#define SETTINGSDIALOG_HPP

#include <string>
#include <vector>

#include <QDialog>
#include <QHBoxLayout>
#include <QStandardItemModel>
#include <QTreeWidget>
#include <QWidget>
#include <QMutex>
#include <QColor>

#include <RMG-Core/Core.hpp>

// needed for KeyBindButton in ui_SettingsDialog
#include "UserInterface/Widget/KeybindButton.hpp"

using namespace UserInterface::Widget;

#include "ui_SettingsDialog.h"

namespace UserInterface
{
namespace Dialog
{
class SettingsDialog : public QDialog, private Ui::SettingsDialog
{
    Q_OBJECT

  private:
    enum class SettingsDialogAction
    {
      ConnectSignals = 0,
      LoadSettings,
      LoadDefaultSettings,
      SaveSettings
    };

    bool romOpened = false;

    CoreRomSettings currentGameSettings;
    CoreRomSettings defaultGameSettings;
    std::string     gameSection;

    int             keybindButtonTimerId  = -1;
    int             keybindButtonTimeLeft = 5;
    KeybindButton*  currentKeybindButton  = nullptr;

    QColor currentBackgroundColor;
    QColor currentTextColor;

    std::vector<CorePlugin> pluginList;

    void showErrorMessage(QString, QString);

    int currentIndex(void);

    void restoreDefaults(int);
    void loadSettings(int);
    void saveSettings(int);

    void loadCoreSettings(void);
    void loadGameSettings(void);
    void loadGameCoreSettings(void);
    void loadGamePluginSettings(void);
    void loadPluginSettings(void);
    void loadDirectorySettings(void);
    void load64DDSettings(void);
    void loadHotkeySettings(void);
    void loadInterfaceGeneralSettings(void);
    void loadInterfaceEmulationSettings(void);
    void loadInterfaceRomBrowserSettings(void);
    void loadInterfaceLogSettings(void);
    void loadInterfaceOSDSettings(void);
    void loadInterfaceStyleSettings(void);

    void loadDefaultCoreSettings(void);
    void loadDefaultGameSettings(void);
    void loadDefaultGameCoreSettings(void);
    void loadDefaultGamePluginSettings(void);
    void loadDefaultPluginSettings(void);
    void loadDefaultDirectorySettings(void);
    void loadDefault64DDSettings(void);
    void loadDefaultHotkeySettings(void);
    void loadDefaultInterfaceGeneralSettings(void);
    void loadDefaultInterfaceEmulationSettings(void);
    void loadDefaultInterfaceRomBrowserSettings(void);
    void loadDefaultInterfaceLogSettings(void);
    void loadDefaultInterfaceOSDSettings(void);
    void loadDefaultInterfaceStyleSettings(void);

    void saveSettings(void);
    void saveCoreSettings(void);
    void saveGameSettings(void);
    void saveGameCoreSettings(void);
    void saveGamePluginSettings(void);
    void savePluginSettings(void);
    void saveDirectorySettings(void);
    void save64DDSettings(void);
    void saveHotkeySettings(void);
    void saveInterfaceGeneralSettings(void);
    void saveInterfaceEmulationSettings(void);
    void saveInterfaceRomBrowserSettings(void);
    void saveInterfaceLogSettings(void);
    void saveInterfaceOSDSettings(void);
    void saveInterfaceStyleSettings(void);

    void commonHotkeySettings(SettingsDialogAction);
    void commonPluginSettings(SettingsDialogAction);

    void setIconsForEmulationInfoText(void);
    void hideEmulationInfoText(void);

    void chooseDirectory(QLineEdit *);
    void chooseFile(QLineEdit *, QString filter = "", QString md5 = "");
    void chooseColor(QPushButton *, QColor *, bool skipChoice = false);

    bool applyPluginSettings(void);

  protected:
    void closeEvent(QCloseEvent* event) Q_DECL_OVERRIDE;
    void timerEvent(QTimerEvent* event) Q_DECL_OVERRIDE;

  private slots:
    void on_buttonBox_clicked(QAbstractButton *);

    void on_changeScreenShotDirButton_clicked(void);
    void on_changeSaveStateDirButton_clicked(void);
    void on_changeSaveSramDirButton_clicked(void);
    void on_changeUserDataDirButton_clicked(void);
    void on_changeUserCacheDirButton_clicked(void);

    void on_changeJapaneseIPLRomPathButton_clicked(void);
    void on_changeAmericanIPLRomPathButton_clicked(void);
    void on_changeDevelopmentIPLRomPathButton_clicked(void);

    void on_changeBackgroundColorButton_clicked(void);
    void on_changeTextColorButton_clicked(void);

    void on_KeybindButton_KeybindingChanged(KeybindButton* button);
    void on_KeybindButton_Clicked(KeybindButton* button);

    void on_coreCpuEmulatorComboBox_currentIndexChanged(int);

    void on_changeNTSCPifRomButton_clicked(void);
    void on_changePALPifRomButton_clicked(void);
  public:
    SettingsDialog(QWidget *parent);
    ~SettingsDialog(void);

    void ShowGameTab(void);
};
} // namespace Dialog
} // namespace UserInterface

#endif // SETTINGSDIALOG_HPP