/*
    QAD - Qt ADvanced

    Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser 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 Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef SQL_RECORD_WIDGET_H
#define SQL_RECORD_WIDGET_H

#include "qad_sql_table_export.h"

#include <QMap>
#include <QSet>
#include <QSqlField>
#include <QSqlQuery>
#include <QVariant>
#include <QWidget>


class QAD_SQL_TABLE_EXPORT SQLRecordWidget: public QWidget {
	Q_OBJECT

public:
	SQLRecordWidget(QWidget * parent = 0);
	~SQLRecordWidget();

	void setRecord(const QSqlRecord & q, bool full_update = false);
	void setReadOnly(bool yes) {
		ro = yes;
		updateWidgets();
	}
	void setFixedValue(const QString & name, const QVariant & value);
	void setTypeForName(const QString & name, const QString & type) { ftypes[name] = type; }
	void clearValues();
	void removeWidgets() {
		qDeleteAll(cws);
		cws.clear();
	}

	void hideColumn(const QString & col) { hidden << col; }
	void showColumn(const QString & col) { hidden.remove(col); }
	void showColumns() { hidden.clear(); }
	void addRelation(const QString & this_column, const QString & other_table, const QString & other_key, const QString & other_column) {
		relations_src << RelationSrc(this_column, other_table, other_key, other_column);
		updateRelations();
	}
	void removeRelations() {
		relations_src.clear();
		updateRelations();
	}
	void addTranslation(const QString & file);
	void setConnectionName(const QString & conn_name) { connection_name = conn_name; }
	QString connectionName() const { return connection_name; }

	QString getValuesQuery() const;
	QString getUpdateQuery() const;
	bool isEmpty() const;
	QWidget * valueWidget(const QString & name) const {
		foreach(QWidget * w, cws)
			if (w->objectName() == name) return w;
		return 0;
	}

	void updateRelations();

protected:
	QPair<QString, QString> trColumn(const QString & n) {
		QPair<QString, QString> trn = translates.value(n);
		if (trn.first.isEmpty()) return QPair<QString, QString>(n, "");
		return trn;
	}
	void createWidgets(const QSqlRecord & q);
	void updateWidgets();
	QVariant::Type fieldType(const QSqlField & f);

	struct QAD_SQL_TABLE_EXPORT RelationSrc {
		RelationSrc(const QString & v0 = QString(),
		            const QString & v1 = QString(),
		            const QString & v2 = QString(),
		            const QString & v3 = QString())
			: tcol(v0)
			, table(v1)
			, key(v2)
			, ocol(v3) {}
		QString tcol;
		QString table;
		QString key;
		QString ocol;
	};

	QMap<QString, QPair<QString, QString>> translates;
	QVector<RelationSrc> relations_src;
	QMap<QString, QList<QPair<int, QString>>> relations;
	QMap<QString, QString> ftypes;
	QSet<QString> hidden;
	QWidgetList cws;
	bool ro;
	QString connection_name;

signals:
	void updateRequest();
	void widgetsCreated();
};

#endif // SQL_RECORD_WIDGET_H
