#ifndef MYTHDB_H_
#define MYTHDB_H_

#include <QMap>
#include <QString>
#include <QVariant>
#include "mythbaseexp.h"
#include "mythdbcon.h"
#include "mythdbparams.h"

class MythDBPrivate;
class MDBManager;

class MBASE_PUBLIC MythDB
{
    friend class MSqlQuery;
  public:
    MDBManager *GetDBManager(void);

    static QString GetError(const QString &where, const MSqlQuery &query);
    static void DBError(const QString &where, const MSqlQuery &query);
    static QString DBErrorMessage(const QSqlError &err);

    DatabaseParams GetDatabaseParams(void) const;
    void SetDatabaseParams(const DatabaseParams &params);

    void SetLocalHostname(const QString &name);
    QString GetHostName(void) const;

    void IgnoreDatabase(bool bIgnore);
    bool IsDatabaseIgnored(void) const;

    void SetSuppressDBMessages(bool bUpgraded);
    bool SuppressDBMessages(void) const;

    void ClearSettingsCache(const QString &key = QString());
    void ActivateSettingsCache(bool activate = true);
    void OverrideSettingForSession(const QString &key, const QString &newValue);
    void ClearOverrideSettingForSession(const QString &key);

    void SaveSetting(const QString &key, int newValue);
    void SaveSetting(const QString &key, const QString &newValue);
    bool SaveSettingOnHost(const QString &key, const QString &newValue,
                           const QString &host);
    bool ClearSetting(const QString &key);
    bool ClearSettingOnHost(const QString &key, const QString &host);

    bool GetSettings(QMap<QString,QString> &_key_value_pairs);

    QString GetSetting(     const QString &key, const QString &defaultval);
    int     GetNumSetting(  const QString &key, int            defaultval);
    double  GetFloatSetting(const QString &key, double         defaultval);

    QString GetSetting(     const QString &key);
    int     GetNumSetting(  const QString &key);
    double  GetFloatSetting(const QString &key);

    QString GetSettingOnHost(
        const QString &key, const QString &host, const QString &defaultval);
    int     GetNumSettingOnHost(
        const QString &key, const QString &host, int            defaultval);
    double  GetFloatSettingOnHost(
        const QString &key, const QString &host, double         defaultval);

    QString GetSettingOnHost(     const QString &key, const QString &host);
    int     GetNumSettingOnHost(  const QString &key, const QString &host);
    double  GetFloatSettingOnHost(const QString &key, const QString &host);

    void GetResolutionSetting(const QString &type, int &width, int &height,
                              double& forced_aspect, double &refreshrate,
                              int index=-1);
    void GetResolutionSetting(const QString &type, int &width, int &height,
                              int index=-1);

    void WriteDelayedSettings(void);

    //JM ADDED
    void savesettings_togroup(QString src_hostname,
                              QString saved_settingsgroupname );
    void deletesettings(QString delete_hostname,
                        QString saved_settingsgroupname);

    QMap<QString,QStringList> list_settingsgroupname();

    QMap<QString,QStringList> diff_settingsgroupname(QString diff_hostname1,
                                                     QString diff_hostname2,
                                                     QStringList table_list);

    void clonesettings(QString src_hostname,
                       QString saved_settingsgroupname,
                       QStringList table_list,
                       bool is_full_clone = true,
                       QString dest_hostname="");

    void restoresettings (QString dest_hostname,
                          QString saved_settingsgroupname,
                          QStringList table_list);

    void change_hostname (QString old_hostname, QString new_hostname);
    bool settings_check (QString src_hostname, QString saved_settingsgroupname);
    void import_settings(QMap<QString,QString> vp_map, QString table);


    QMap<QString,QMap <QString,QString> > export_settings(
                         QString src_hostname,
                         QString saved_settingsgroupname,
                         QStringList table_list,
                         bool skip_host = false);

    void create_new_table (QString create_table_name,QString like_name);
    void drop_temp_table (QString table_name);
    void create_temp_table (QString create_table_name,QString like_name);
    bool does_table_exist(QString table_name);
    QString find_group_table_name(QString current_table,
                                  QString saved_settingsgroupname);
    //END JM ADDED



    void SetHaveDBConnection(bool connected);
    void SetHaveSchema(bool schema);
    bool HaveSchema(void) const;
    bool HaveValidDatabase(void) const;

    static MythDB *getMythDB();
    static void destroyMythDB();
    static QString toCommaList(const QMap<QString, QVariant> &bindings,
                               uint indent = 0, uint softMaxColumn = 80);

  protected:
    MythDB();
   ~MythDB();

  private:
    MythDBPrivate *d;
};

 MBASE_PUBLIC  MythDB *GetMythDB();
 MBASE_PUBLIC  void DestroyMythDB();

#endif
