#include "qcd_core.h"
#include "cdutils_core.h"
#include "piqt.h"
#include <QWidget>
#include <QCheckBox>
#include <QGroupBox>
#include <QSpinBox>
#include <QSlider>
#include <QScrollBar>
#include <QDoubleSpinBox>
#include <QLineEdit>
#include <spinslider.h>
#include <clineedit.h>
#include <qcd_kview.h>

using namespace CDUtils;


int __QCore_Initializer__::count_(0);
QCDCore * __QCore_Initializer__::__instance__(0);


__QCore_Initializer__::__QCore_Initializer__() {
	count_++;
	if (count_ > 1) return;
	__instance__ = new QCDCore();
}


__QCore_Initializer__::~__QCore_Initializer__() {
	count_--;
	if (count_ > 0) return;
	if (__instance__ != 0) {
		delete __instance__;
		__instance__ = 0;
	}
}




QCDCore::QCDCore() {
	setObjectName("QCDCore");
	setName("QCDCore");
	CONNECTU(CDCore::instance(), K_ChangedGlobal, this, K_ChangedGlobal);
	updating = false;
}


QCDCore::~QCDCore() {
}


void QCDCore::K_ChangedGlobal() {
	QMetaObject::invokeMethod(this, "updateBindedWidgets", Qt::QueuedConnection);
}


void QCDCore::slotBool(bool v) {
	QWidget * w = (QWidget*)sender();
	if (!w || updating) return;
	QList<PIDeque<int> > pathes = binded_widgets.values(w);
	foreach (const PIDeque<int> & path, pathes)
		CDCore::instance()->k()[path].setValue(PIString::fromBool(v));
	emit updateViewRequest();
}


void QCDCore::slotInt(int v) {
	QWidget * w = (QWidget*)sender();
	if (!w || updating) return;
	QList<PIDeque<int> > pathes = binded_widgets.values(w);
	foreach (const PIDeque<int> & path, pathes)
		CDCore::instance()->k()[path].setValue(PIString::fromNumber(v));
	emit updateViewRequest();
}


void QCDCore::slotDouble(double v) {
	QWidget * w = (QWidget*)sender();
	if (!w || updating) return;
	QList<PIDeque<int> > pathes = binded_widgets.values(w);
	foreach (const PIDeque<int> & path, pathes)
		CDCore::instance()->k()[path].setValue(PIString::fromNumber(v));
	emit updateViewRequest();
}


void QCDCore::slotText(QString v) {
	QWidget * w = (QWidget*)sender();
	if (!w || updating) return;
	QList<PIDeque<int> > pathes = binded_widgets.values(w);
	foreach (const PIDeque<int> & path, pathes)
		CDCore::instance()->k()[path].setValue(Q2PIString(v));
	emit updateViewRequest();
}


int QCDCore::bindWindow(QWidget * wnd) {
	if (!wnd) return 0;
	CDCore::instance()->k().makePath();
	return bindWidgets(wnd->findChildren<QWidget * >());
}


int QCDCore::bindWidgets(QList<QWidget * > wl) {
	int ret = 0;
	foreach (QWidget * w, wl)
		if (bindWidget(w)) ++ret;
	return ret;
}


bool QCDCore::bindWidget(QWidget * w) {
	if (!w) return false;
	QString on = w->objectName();
	QString cn = w->metaObject()->className();
	if (cn == "CDKView") {
		bindView(w);
		return false;
	}
	PIVector<CDType * > ak = CDCore::instance()->k().children();
	piForeachC (CDType * k, ak) {
		if (!on.endsWith(PI2QString(k->name()))) continue;
		if (bindWidget(w, *k)) return true;
	}
	return false;
}


bool QCDCore::bindWidget(QWidget * w, const CDType k) {
	if (!w) return false;
	QString cn = w->metaObject()->className();
	bool ok = false;
	if (cn == "QCheckBox" || cn == "QGroupBox") {
		connect(w, SIGNAL(toggled(bool)), this, SLOT(slotBool(bool)), Qt::UniqueConnection);
		ok = true;
	}
	if (cn == "QSpinBox" || cn == "QSlider" || cn == "QScrollBar") {
		connect(w, SIGNAL(valueChanged(int)), this, SLOT(slotInt(int)), Qt::UniqueConnection);
		ok = true;
	}
	if (cn == "QDoubleSpinBox" || cn == "SpinSlider") {
		connect(w, SIGNAL(valueChanged(double)), this, SLOT(slotDouble(double)), Qt::UniqueConnection);
		ok = true;
	}
	if (cn == "QLineEdit" || cn == "CLineEdit") {
		connect(w, SIGNAL(textChanged(QString)), this, SLOT(slotText(QString)), Qt::UniqueConnection);
		ok = true;
	}
	if (cn == "CDKView") {
		bindView(w);
	}
	if (!ok) return false;
	piCout << k.name() << k.path();
	binded_widgets.insert(w, k.path());
	return true;
}


void QCDCore::updateBindedWidgets() {
	QMapIterator<QWidget * , PIDeque<int> > it(binded_widgets);
	updating = true;
	while (it.hasNext()) {
		QWidget * w = it.next().key();
		QString cn = w->metaObject()->className();
		const CDType k(CDCore::instance()->k()[it.value()]);
		if (cn == "QCheckBox") qobject_cast<QCheckBox*>(w)->setChecked(k.toBool());
		if (cn == "QGroupBox") qobject_cast<QGroupBox*>(w)->setChecked(k.toBool());
		if (cn == "QSpinBox") qobject_cast<QSpinBox*>(w)->setValue(k.toInt());
		if (cn == "QSlider") qobject_cast<QSlider*>(w)->setValue(k.toInt());
		if (cn == "QScrollBar") qobject_cast<QScrollBar*>(w)->setValue(k.toInt());
		if (cn == "QDoubleSpinBox") qobject_cast<QDoubleSpinBox*>(w)->setValue(k.toDouble());
		if (cn == "SpinSlider") qobject_cast<SpinSlider*>(w)->setValue(k.toDouble());
		if (cn == "QLineEdit") qobject_cast<QLineEdit*>(w)->setText(PI2QString(k.value()));
		if (cn == "CLineEdit") qobject_cast<CLineEdit*>(w)->setText(PI2QString(k.value()));
	}
	updating = false;
}


void QCDCore::bindView(QWidget * v) {
	CDKView * w = qobject_cast<CDKView * >(v);
	if (!w) return;
	connect(this, SIGNAL(updateViewRequest()), w->model(), SLOT(updateModel()), Qt::UniqueConnection);
}


int QCDCore::unbindWindow(QWidget * wnd) {
	if (!wnd) return 0;
	return unbindWidgets(wnd->findChildren<QWidget * >());
}


int QCDCore::unbindWidgets(QList<QWidget * > wl) {
	int ret = 0;
	foreach (QWidget * w, wl)
		if (unbindWidget(w)) ++ret;
	return ret;
}


bool QCDCore::unbindWidget(QWidget * w) {
	if (!w) return false;
	if (!binded_widgets.contains(w)) return false;
	QString cn = w->metaObject()->className();
	if (cn == "QCheckBox" || cn == "QGroupBox")
		disconnect(w, SIGNAL(toggled(bool)), this, SLOT(slotBool(bool)));
	if (cn == "QSpinBox" || cn == "QSlider" || cn == "QScrollBar")
		disconnect(w, SIGNAL(valueChanged(int)), this, SLOT(slotInt(int)));
	if (cn == "QDoubleSpinBox" || cn == "SpinSlider")
		disconnect(w, SIGNAL(valueChanged(double)), this, SLOT(slotDouble(double)));
	if (cn == "QLineEdit" || cn == "CLineEdit")
		disconnect(w, SIGNAL(textChanged(QString)), this, SLOT(slotText(QString)));
	binded_widgets.remove(w);
	return true;
}


void QCDCore::unbindAllWidgets() {
	QMapIterator<QWidget * , PIDeque<int> > it(binded_widgets);
	while (it.hasNext()) {
		QWidget * w = it.next().key();
		unbindWidget(w);
	}
	binded_widgets.clear();
}
