/*
    PIQt Utils - Qt utilites for PIP

    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 pivariant_edit_widgets_H
#define pivariant_edit_widgets_H

#include "pivariant_edit.h"

#include <QCheckBox>
#include <QComboBox>
#include <QDateEdit>
#include <QDoubleSpinBox>
#include <QMenu>
#include <QSpinBox>
#include <QTimeEdit>
#include <clineedit.h>
#include <colorbutton.h>
#include <ecombobox.h>
#include <piqt.h>

namespace PIVariantEditors {


class QAD_PIQT_UTILS_EXPORT NumberBase: public PIVariantEditorBase {
	Q_OBJECT

public:
	NumberBase();
	void setValueNumeric(double v);
	double valueNumeric() const;
	PIVariantMap attributes() const override;
	static PIVariantMap defaultAttributes();
	static PIVariantTypes::Enum createTypes();
	void retranslate() override;

protected:
	void applyAttributes(const PIVariantMap & a) override;
	enum Type {
		tInvalid,
		tSpinBox,
		tSlider,
		tSpinSlider,
		tEvalSpinBox,
		tScrollSpinBox,
	};
	Type type = tInvalid;
	PIString prefix, suffix;
	QWidget * widget = nullptr;
	bool is_int      = false;
};


class QAD_PIQT_UTILS_EXPORT Bool: public PIVariantEditorBase {
	Q_OBJECT

public:
	Bool() {
		widget = new QCheckBox();
		layout()->addWidget(widget);
	}
	void setValue(const PIVariant & v) override { widget->setChecked(v.toBool()); }
	PIVariant value() const override { return widget->isChecked(); }

private:
	void applyAttributes(const PIVariantMap & a) override;

	QCheckBox * widget;
};


class QAD_PIQT_UTILS_EXPORT Int: public NumberBase {
	Q_OBJECT

public:
	Int(): NumberBase() {
		is_int = true;
		applyAttributes(NumberBase::defaultAttributes());
	}
	void setValue(const PIVariant & v) override { setValueNumeric(v.toInt()); }
	PIVariant value() const override { return piRoundd(valueNumeric()); }
};


class QAD_PIQT_UTILS_EXPORT Double: public NumberBase {
	Q_OBJECT

public:
	Double(): NumberBase() {
		is_int = false;
		applyAttributes(NumberBase::defaultAttributes());
	}
	void setValue(const PIVariant & v) override { setValueNumeric(v.toDouble()); }
	PIVariant value() const override { return valueNumeric(); }
};


class QAD_PIQT_UTILS_EXPORT String: public PIVariantEditorBase {
	Q_OBJECT

public:
	String() {
		widget = new CLineEdit();
		layout()->addWidget(widget);
	}
	void setValue(const PIVariant & v) override { widget->setText(PI2QString(v.toString())); }
	PIVariant value() const override { return Q2PIString(widget->text()); }
	PIVariantMap attributes() const override;
	static PIVariantMap defaultAttributes();

private:
	void applyAttributes(const PIVariantMap & a) override;
	CLineEdit * widget;
};


class QAD_PIQT_UTILS_EXPORT StringList: public PIVariantEditorBase {
	Q_OBJECT

public:
	StringList();
	void setValue(const PIVariant & v) override;
	PIVariant value() const override;
	PIVariantMap attributes() const override;

private:
	void applyAttributes(const PIVariantMap & a) override;
	void retranslate() override;
	EComboBox * combo;
	QToolButton *butt_apply, *butt_add, *butt_del, *butt_clear;
};


class QAD_PIQT_UTILS_EXPORT Color: public PIVariantEditorBase {
	Q_OBJECT

public:
	Color() {
		widget = new ColorButton();
		layout()->addWidget(widget);
	}
	void setValue(const PIVariant & v) override { widget->setColor(PI2QColor(v.toColor())); }
	PIVariant value() const override { return Q2PIColor(widget->color()); }
	PIVariantMap attributes() const override;
	static PIVariantMap defaultAttributes();

private:
	void applyAttributes(const PIVariantMap & a) override;
	ColorButton * widget;
};


class QAD_PIQT_UTILS_EXPORT Time: public PIVariantEditorBase {
	Q_OBJECT

public:
	Time() {
		widget = new QTimeEdit();
		widget->setDisplayFormat("h:mm:ss");
		layout()->addWidget(widget);
	}
	void setValue(const PIVariant & v) override { widget->setTime(PI2QTime(v.toTime())); }
	PIVariant value() const override { return Q2PITime(widget->time()); }
	PIVariantMap attributes() const override { return {}; }

private:
	void applyAttributes(const PIVariantMap & a) override;
	QTimeEdit * widget;
};


class QAD_PIQT_UTILS_EXPORT Date: public PIVariantEditorBase {
	Q_OBJECT

public:
	Date() {
		widget = new QDateEdit();
		widget->setDisplayFormat("d.MM.yyyy");
		layout()->addWidget(widget);
	}
	void setValue(const PIVariant & v) override { widget->setDate(PI2QDate(v.toDate())); }
	PIVariant value() const override { return Q2PIDate(widget->date()); }
	PIVariantMap attributes() const override { return {}; }

private:
	void applyAttributes(const PIVariantMap & a) override;
	QDateEdit * widget;
};


class QAD_PIQT_UTILS_EXPORT DateTime: public PIVariantEditorBase {
	Q_OBJECT

public:
	DateTime() {
		widget = new QDateTimeEdit();
		widget->setDisplayFormat("d.MM.yyyy h:mm:ss");
		layout()->addWidget(widget);
	}
	void setValue(const PIVariant & v) override { widget->setDateTime(PI2QDateTime(v.toDateTime())); }
	PIVariant value() const override { return Q2PIDateTime(widget->dateTime()); }
	PIVariantMap attributes() const override { return {}; }

private:
	void applyAttributes(const PIVariantMap & a) override;
	QDateTimeEdit * widget;
};


class QAD_PIQT_UTILS_EXPORT Enum: public PIVariantEditorBase {
	Q_OBJECT

public:
	Enum();
	void setValue(const PIVariant & v) override;
	PIVariant value() const override;
	PIVariantMap attributes() const override { return {}; }

private:
	void applyAttributes(const PIVariantMap & a) override;
	void setFullEditMode(bool on) override;
	mutable PIVariantTypes::Enum src;
	QComboBox * widget;
	QWidget * edit_widget;
};


class QAD_PIQT_UTILS_EXPORT NetworkAddress: public PIVariantEditorBase {
	Q_OBJECT

public:
	NetworkAddress();
	void setValue(const PIVariant & v) override;
	PIVariant value() const override;
	PIVariantMap attributes() const override;
	static PIVariantMap defaultAttributes();

private:
	void applyAttributes(const PIVariantMap & a) override;
	QLineEdit * widget;
	bool has_port = true;
};


class QAD_PIQT_UTILS_EXPORT FileBase: public PIVariantEditorBase {
	Q_OBJECT

public:
	FileBase();
	PIVariantMap attributes() const override;
	static PIVariantMap defaultAttributes();

protected:
	void applyAttributes(const PIVariantMap & a) override;
	void setFullEditMode(bool on) override;
	void createMenu();
	CLineEdit * widget;
	QWidget * sel_widget;
	QWidget * edit_widget;
	QMenu edit_menu;
	QAction *act_abs = nullptr, *act_save = nullptr, *act_filter = nullptr;
	bool is_dir = false, is_abs = false, is_save = false;
	PIString filter;
};


class QAD_PIQT_UTILS_EXPORT File: public FileBase {
	Q_OBJECT

public:
	File(): FileBase() {
		is_dir = false;
		createMenu();
		setFullEditMode(false);
	}
	void setValue(const PIVariant & v) override;
	PIVariant value() const override;
};


class QAD_PIQT_UTILS_EXPORT Dir: public FileBase {
	Q_OBJECT

public:
	Dir(): FileBase() {
		is_dir = true;
		createMenu();
		setFullEditMode(false);
	}
	void setValue(const PIVariant & v) override;
	PIVariant value() const override;
};


}; // namespace PIVariantEditors


#endif
