
//  Copyright (C) 2009 by Arnaud Dupuis
//  a.dupuis@infinityperl.org
//  http://www.infinityperl.org
// 
//  This program 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.
// 
//  This program 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, write to the
//  Free Software Foundation, Inc.,
//  59 Temple Place - Suite 330, Boston, MA  02111-1307, USA

#ifndef ASSUMAMAINWINDOW_H
#define ASSUMAMAINWINDOW_H

#include <QMainWindow>
#include "ui_assuma_mainwindow.h"
#include "assuma_passivepopup.h"
#include "federationhandler.h"
#include "assuma_configwidget.h"
#include "assuma_configmanager.h"
#include "customfieldsxmlhandler.h"

class QMenu;
class QString;
class QTranslator;
class AssumaMailDialog;
class QSplashScreen;
class AssumaAddonEngine;
class AssumaTreeWidgetItemEditorDelegate;

static const QString ASSUMA_VERSION("3.2.0");
static const QString ASSUMA_DB_VERSION("3.15");

//! \warning This class is a Singleton one, you cannot instanciate it, use the getInstance() function to have the unique instance of AssumaMainWindow.

#define AssumaMW AssumaMainWindow::instance()

class AssumaMainWindow : public QMainWindow, public Ui::AssumaMainWindow
{
	Q_OBJECT
	public:
		enum LicenseFeeRoles {
			IdRole = Qt::UserRole + 4242,
			NameRole = IdRole + 1,
			FeeRole = NameRole + 1
		};
		QTranslator *m_translator;
		
		static AssumaMainWindow *instance();
		
		int elementColumn(const QString&);
		int elementColumn(const QTreeWidgetItem *, const QString& );
		QString subscribersListToXml();
		QString subscribersListToString(const QString&, bool);
		QString subscribersListToHtml();
		QString subscribersListToPrintableHtml();
		QString citiesToXml();
		bool writeDatabase(const QString &);
		int priceToInt(QString &);
// 		int calculateDue(const QListWidgetItem *);
		double calculateDue(const QTreeWidgetItem *);
		bool areFeesEquivalent( QString , QString );
		QString processTemplate( const QString & );
		QString getDirectory( const QString & );
		QString getImageFromTheme(const QString &);
		QString getImageFromTheme(const QString &, const QString &);
		QString loadTextFile( const QString & );
		QString assumaVersion();
		CustomFieldItem * getCustomFieldItemByName(const QString &);
		QString buildHtmlPreview(QTreeWidgetItem *);
	public slots:
		void readSettingsInConfigurationManager();
		void setSplashMessage( const QString & );
		void checkUpdatesOnStart();
		void loadPostalCodes(const QString&);
		void writePostalCodes( const QString &);
		void loadDatabase(const QString&);
		void loadFederation(const QString&);
		void loadLanguageList(const QString&);
		void loadAddons();
		void addSubscriber( QTreeWidgetItem * );
		void addAvatar( const QString& );
		void showPassiveMessage( const QString & );
		void showPassiveMessage( const QString &, const QPixmap & );
		void showPassiveMessage( const QString &, AssumaPassivePopup::Icon );
		void showPassiveMessage( const QString & , AssumaConfigManager::Severity );
		void changeLanguage(const QString &);
		void setActionBarToolTips();
		void loadTheme( const QString & themeName = "default" );
		void processPostInitTasks();
		void loadCustomFields();
	protected:
		void closeEvent(QCloseEvent *event);
	private:
		AssumaMainWindow(QWidget *parent=0);
		static AssumaMainWindow *m_singleInstance;
		static bool m_canInstanciate;
		static bool m_isInitializing; // used to tell the rest of the class to not do something useless while the class is not fully initialized.
		QString m_currentDatabase;
		QString m_previousLanguage;
		QString m_currentFederation;
		AssumaMailDialog* createMailDialog();
		void readSettings();
		void writeSettings();
		void setItemTooltip(QTreeWidgetItem *);
		double licenseFeeByLabel(const QString &);
		double licenseFeeById(const QString &);
		QString licenseIdByName(const QString & );
		QString licenseNameById(const QString & );
		bool maybeSave();
		bool m_shouldCloseEditor;
		bool m_shouldSavePostalCodes;
		bool m_dontUpdateSubscribersList;
		QMap<QString, int> m_columnMap;
		QMenu *m_contextMenu;
		QMap<QString,QString> m_pc_map;
		QString m_postal_codes_filename;
// 		FederationHandler m_federationHandler;
		QList<FederationLicenseFee *> m_licences_fees;
		QMap<QString,QString> m_translation_equiv;
		QSplashScreen *m_splash;
		AssumaAddonEngine *m_addonEngine;
		AssumaTreeWidgetItemEditorDelegate *m_treeWidgetDelegate;
		QList<AssumaCustomWidgetBase *> m_customWidgets;
                QList<CustomFieldItem *> m_customFields;
	private slots:
		void updateAllItemsTooltips();
		void updateSplitterSize();
		void on_treeWidget_itemSelectionChanged();
		void finishUpdate(bool);
		void subscribersImported(QList<QTreeWidgetItem*>*);
		void on_treeWidget_customContextMenuRequested ( const QPoint & );
		void on_editSubscriberButton_toggled(bool);
		void on_actionAssociation_settings_toggled(bool);
		void on_action_Find_toggled(bool visible);
		void aboutQt();
		void exploitData();
		void exploitItemData( QTreeWidgetItem * );
		void on_addressEdit_textChanged ( const QString & );
		void on_cityEdit_textChanged ( const QString & );
// 		void on_cityEdit_currentIndexChanged ( int );
// 		void on_cityEdit_editTextChanged ( const QString & );
		void on_postalCodeEdit_textChanged ( const QString & );
// 		void on_postalCodeEdit_currentIndexChanged ( int );
// 		void on_postalCodeEdit_editTextChanged ( const QString & );
		void on_duesStatusEdit_currentIndexChanged ( int );
		void on_medicalStatuEdit_currentIndexChanged ( int );
		void on_licenseLabelComboBox_currentIndexChanged ( int );
		void on_action_Copy_activated();
		void on_addImagesButton_clicked();
		void on_actionExport_to_CSV_activated();
		void on_actionExport_to_Excel_activated();
		void on_actionExport_to_HTML_activated();
		void on_actionImport_subscribers_from_another_database_activated();
		void on_actionPaste_activated();
// 		void on_treeWidget_itemDoubleClicked( QTreeWidgetItem *, int);
// 		void on_treeWidget_itemClicked ( QTreeWidgetItem *, int );
		void on_treeWidget_itemChanged( QTreeWidgetItem * , int );
		void on_firstNameEdit_textChanged( const QString &);
		void on_maidenNameEdit_textChanged( const QString &);
		void on_lastNameEdit_textChanged( const QString &);
		void on_emailEdit_textChanged( const QString &);
		void on_phoneNumberEdit_textChanged( const QString &);
		void on_licenseNumberEdit_textChanged( const QString &);
		void on_commentEdit_textChanged( const QString &);
		void on_subscriptionDateEdit_dateChanged ( const QDate & );
		void on_birthDateEdit_dateChanged ( const QDate & );
		void on_lessonsNumberEdit_valueChanged(const QString &);
		void on_duesEdit_valueChanged(double);
		void on_actionSend_a_newsletter_activated();
		void on_actionSend_fee_reminder_activated();
		void on_actionAbout_Association_Subscribers_Manager_activated();
		void on_actionAssociation_Subscribers_Manager_manual_activated();
		void on_activityComboBox_currentIndexChanged( int );
		void on_actionEdit_subscriber_toggled(bool);
// 		void on_searchQuery_textChanged(const QString &);
// 		void on_caseSensitiveBox_stateChanged ( int );
// 		void on_nameSearchBox_stateChanged ( int );
// 		void on_extraSearchBox_stateChanged ( int );
// 		void on_clearSearchButton_clicked(bool);
// 		void on_doSearchButton_clicked(bool);
// 		void performSearch(const QString & query);
		void on_pictureEdit_toggled(bool isChecked);
		void on_imagesListWidget_itemDoubleClicked ( QListWidgetItem *);
		void on_actionAdd_a_subscriber_activated();
		void on_deleteSubscriberButton_clicked();
		void on_action_New_activated();
		void on_action_Open_activated();
		void on_action_Save_activated();
		void on_actionSave_as_activated();
// 		void on_liveSearchBox_stateChanged( int );
		void on_actionQuit_activated();
		void setColumnVisibility( bool );
		void on_actionShow_FirstName_column_toggled (bool);
		void on_actionShow_MaidenName_column_toggled (bool);
		void on_actionShow_LastName_column_toggled (bool);
		void on_actionShow_BirthDate_column_toggled (bool);
		void on_actionShow_Address_column_toggled (bool);
		void on_actionShow_City_column_toggled (bool);
		void on_actionShow_PostalCode_column_toggled (bool);
		void on_actionShow_PhoneNumber_column_toggled (bool);
		void on_actionShow_MobilePhone_column_toggled (bool);
		void on_actionShow_Email_column_toggled (bool);
		void on_actionShow_Activity_column_toggled (bool);
		void on_actionShow_LessonsNumber_column_toggled (bool);
		void on_actionShow_Dues_column_toggled (bool);
		void on_actionShow_DuesStatus_column_toggled (bool);
		void on_actionShow_SubscriptionDate_column_toggled (bool);
		void on_actionShow_MedicalCertificateStatus_column_toggled (bool);
		void on_actionShow_LicenseNumber_column_toggled (bool);
		void on_actionShow_Comment_column_toggled (bool);
		void on_actionShow_Picture_column_toggled (bool);
		void on_actionShow_LicenseLabel_column_toggled (bool);
		void on_treeWidget_currentItemChanged ( QTreeWidgetItem *, QTreeWidgetItem *);
		void on_actionSettings_toggled(bool);
		void on_actionUpdate_activated();
		void on_sendSummaryButton_clicked();
		void on_actionSend_database_by_email_activated();
		void on_action_Print_activated();
		void on_actionGo_to_YouTube_video_tutorials_channel_activated();
                void onCustomWidgetValueChanged( const QString & text );
		void reloadCustomFields();
		void updateColumnVisibility();
};

#endif
