#ifndef DOUBLE_CHARGE_SLIDER_H
#define DOUBLE_CHARGE_SLIDER_H

#include <QtGui>
#include "ui_doubleChargeSlider.h"
#include "weaponItem.h"
#include <cassert>
#include <sstream>
#include <iostream>
#include <string>

class DoubleChargeSlider : public QWidget {
Q_OBJECT
private:
	Ui::DoubleChargeSlider ui_;
	bool isInside_;
	bool canEmit_;

	int min_;
	int max_;
	int fireCharge_;

	QWidget * parent_;
	WeaponItem * weapon_;

	std::wstring energyUnit_;
private:
	void updateFireCharge() {
		ui_.dominantSlider->setName(L"Nabíjet na: " + int2WString(fireCharge_) + energyUnit_);
	}
signals:
	void valueChanged(int newValue);
public slots:
	void sliderValueChangedSlot(int newValue) {
		fireCharge_ = newValue;
		updateFireCharge();
		emit valueChanged(newValue);
	}
public:
	DoubleChargeSlider(QWidget * parent) : QWidget(parent) {
		ui_.setupUi(this);

		this->setMouseTracking(true);

		this->parent_ = parent;

		isInside_ = false;
		canEmit_ = false;

		min_ = 0;
		max_ = 0;
		fireCharge_ = 0;

		QObject::connect(
			ui_.dominantSlider, SIGNAL(valueChanged(int)),
			this, SLOT(sliderValueChangedSlot(int)));

		ui_.slaveSlider->setFontColor(QColor(0, 0, 0));

		ui_.slaveSlider->showCurrent(false);
		ui_.dominantSlider->showCurrent(false);
		ui_.dominantSlider->showMaximum(false);
		ui_.dominantSlider->setFontColor(QColor::fromRgb(40, 40, 40));
	}

	DoubleChargeSlider(QWidget * parent, WeaponItem * weapon) : QWidget(parent) {
		ui_.setupUi(this);

		this->setMouseTracking(true);

		this->parent_ = parent;
		this->weapon_ = weapon;

		isInside_ = false;
		canEmit_ = false;

		min_ = 0;
		max_ = 0;
		fireCharge_ = 0;

		QObject::connect(
			ui_.dominantSlider, SIGNAL(valueChanged(int)),
			this, SLOT(sliderValueChangedSlot(int)));
	}

	virtual void setValue(int newValue) {
		ui_.dominantSlider->setValue(newValue);		
		updateFireCharge();
	}

	int value() {
		return ui_.dominantSlider->value();
	}

	virtual void setLimits(int min, int max) {
		this->min_ = min;
		this->max_ = max;

		QValidator * inputRange = new QIntValidator(min, max, this);
		
		ui_.dominantSlider->setMinimum(min);
		ui_.dominantSlider->setMaximum(max);

		ui_.slaveSlider->setMinimum(min);
		ui_.slaveSlider->setMaximum(max);
	}

	std::wstring int2WString(int value) {
		std::wstringstream streamVal;
		streamVal << value;
		return streamVal.str();
	}

	void setCurrent(int value) {
		ui_.slaveSlider->setValue(value);
		ui_.slaveSlider->setName(int2WString(value) + energyUnit_);		
	}

	void updatePosition() {
		ui_.dominantSlider->setSliderPosition(weapon_->getChargeToFire());
	}

	void setEnergyUnit(const std::wstring & unit) {
		energyUnit_ = L" " + unit;
		ui_.slaveSlider->setUnit(unit);
	}

	void setAsBig() {
		resize(188, 38);
		ui_.dominantSlider->setGeometry(0, 0, 188, 18);
		ui_.slaveSlider->setGeometry(0, 19, 188, 18);
		ui_.dominantSlider->setFontSize(8, QFont::Bold);
		ui_.slaveSlider->setFontSize(8, QFont::Bold);
	}

	void setAsSmall() {
		resize(109, 29);
		ui_.dominantSlider->setGeometry(0, 0, 109, 14);
		ui_.slaveSlider->setGeometry(0, 15, 109, 14);
		ui_.dominantSlider->setFontSize(7, QFont::Normal);
		ui_.slaveSlider->setFontSize(7, QFont::Normal);
	}
protected:
	void mouseMoveEvent(QMouseEvent * e) {
	}
};

#endif