#include <QtGui>
#include <cassert>

#include "ScienceNode.h"
#include "ScienceTree.h"
#include "ui_ScienceTreeWidget.h"
#include "ScienceTreeWidget.h"
#include "protocol.h"
#include "gameParams.h"
#include "softProj\clientServer\structs.h"

ScienceTreeWidget::ScienceTreeWidget(QWidget * parent) : QWidget(parent)
{
	mainWidget_ = new Ui_treeWidget();
	mainWidget_->setupUi(this);

	mainWidget_->scienceSection->setName(L"Strom vědy");

	tree_ = new ScienceTree(mainWidget_, 5);
	
	bool ok = true;

	ok = ok && QObject::connect(
		mainWidget_->tokensCountSlider, SIGNAL(valueChanged(int)),
		tree_, SLOT(tokensCountChanged(int))
		);

	ok = ok && QObject::connect(
		mainWidget_->tokensCountSpinBox, SIGNAL(valueChanged(int)),
		tree_, SLOT(tokensCountChanged(int))
		);
	

	connectNodes();
	buildTree();

	ok = ok && QObject::connect(
		mainWidget_->scienceSection->submitButton,
		SIGNAL(clicked(bool)),
		this,
		SLOT(confirmationButtonClicked(bool))
		);

	ok = ok && QObject::connect(
		mainWidget_->scienceSection->cancelButton,
		SIGNAL(clicked(bool)),
		this,
		SLOT(cancelButtonClicked(bool))
		);

	ok = ok && connect(tree_, SIGNAL(assignedPointsChanged(int)), this, SLOT(settingsChangedSlot()));
	ok = ok && connect(tree_, SIGNAL(freePercentsChangedSignal(int)), this, SLOT(settingsChangedSlot()));

	assert(ok && "Signals not connected");

	lockForUpdate(false);
}

void ScienceTreeWidget::settingsChangedSlot() {
	lockForUpdate(true);
}

void ScienceTreeWidget::confirmationButtonClicked(bool)
{
	lockForUpdate(false);
	emit changeScienceTreeStateSignal(getState());
}

void ScienceTreeWidget::cancelButtonClicked(bool)
{
	lockForUpdate(false);
	emit updateScienceTreeStateSignal();
}

void ScienceTreeWidget::connectNodes()
{
	connectNode(tree_, mainWidget_->node_1_1);

	connectNode(tree_, mainWidget_->node_2_1);

	connectNode(tree_, mainWidget_->node_2_2);

	connectNode(tree_, mainWidget_->node_2_3);

	connectNode(tree_, mainWidget_->node_2_4);

	connectNode(tree_, mainWidget_->node_3_1);
	connectNode(tree_, mainWidget_->node_3_2);

	connectNode(tree_, mainWidget_->node_3_3);
	connectNode(tree_, mainWidget_->node_3_4);

	connectNode(tree_, mainWidget_->node_3_5);
	connectNode(tree_, mainWidget_->node_3_6);

	connectNode(tree_, mainWidget_->node_3_7);
	connectNode(tree_, mainWidget_->node_3_8);

	connectNode(tree_, mainWidget_->node_4_1);
	connectNode(tree_, mainWidget_->node_4_2);
	connectNode(tree_, mainWidget_->node_4_3);
	connectNode(tree_, mainWidget_->node_4_4);

	connectNode(tree_, mainWidget_->node_4_5);
	connectNode(tree_, mainWidget_->node_4_6);
	connectNode(tree_, mainWidget_->node_4_7);
	connectNode(tree_, mainWidget_->node_4_8);

	connectNode(tree_, mainWidget_->node_4_9);
	connectNode(tree_, mainWidget_->node_4_10);
	connectNode(tree_, mainWidget_->node_4_11);
	connectNode(tree_, mainWidget_->node_4_12);

	connectNode(tree_, mainWidget_->node_4_13);
	connectNode(tree_, mainWidget_->node_4_14);
	connectNode(tree_, mainWidget_->node_4_15);
	connectNode(tree_, mainWidget_->node_4_16);

	connectNode(tree_, mainWidget_->node_4_17);
	connectNode(tree_, mainWidget_->node_4_18);
	connectNode(tree_, mainWidget_->node_4_19);
	connectNode(tree_, mainWidget_->node_4_20);

	connectNode(tree_, mainWidget_->node_4_21);
	connectNode(tree_, mainWidget_->node_4_22);
	connectNode(tree_, mainWidget_->node_4_23);
	connectNode(tree_, mainWidget_->node_4_24);

	connectNode(tree_, mainWidget_->node_4_25);
	connectNode(tree_, mainWidget_->node_4_26);
	connectNode(tree_, mainWidget_->node_4_27);
	connectNode(tree_, mainWidget_->node_4_28);

	connectNode(tree_, mainWidget_->node_4_29);
	connectNode(tree_, mainWidget_->node_4_30);
	connectNode(tree_, mainWidget_->node_4_31);
	connectNode(tree_, mainWidget_->node_4_32);
}

void ScienceTreeWidget::buildTree()
{
	// TOKENS_SCIENCE_SUPREME_ENTITY_SCIENCE
	tree_->setRoot(TOKENS_SCIENCE_SUPREME_ENTITY_SCIENCE, mainWidget_->label_1_1, mainWidget_->node_1_1);

	// TOKENS_SCIENCE_SUPREME_ENTITY_SCIENCE
	tree_->appendChild(TOKENS_SCIENCE_SUPREME_ENTITY_SCIENCE, TOKENS_SCIENCE_SUPREME_ENTITY_DRIVE,
		mainWidget_->label_2_1, mainWidget_->node_2_1);

	tree_->appendChild(TOKENS_SCIENCE_SUPREME_ENTITY_SCIENCE, TOKENS_SCIENCE_SUPREME_ENTITY_PLATFORM,
		mainWidget_->label_2_2, mainWidget_->node_2_2);

	tree_->appendChild(TOKENS_SCIENCE_SUPREME_ENTITY_SCIENCE, TOKENS_SCIENCE_SUPREME_ENTITY_MODULES,
		mainWidget_->label_2_3, mainWidget_->node_2_3);

	tree_->appendChild(TOKENS_SCIENCE_SUPREME_ENTITY_SCIENCE, TOKENS_SCIENCE_SUPREME_ENTITY_PRODUCTION,
		mainWidget_->label_2_4, mainWidget_->node_2_4);
	
	// TOKENS_SCIENCE_SUPREME_ENTITY_DRIVE
	tree_->appendChild(TOKENS_SCIENCE_SUPREME_ENTITY_DRIVE, TOKENS_SCIENCE_SUPREME_ENTITY_SUBSPACE_DRIVE,
		mainWidget_->label_3_1, mainWidget_->node_3_1);

	tree_->appendChild(TOKENS_SCIENCE_SUPREME_ENTITY_DRIVE, TOKENS_SCIENCE_SUPREME_ENTITY_IMPULS_DRIVE,
		mainWidget_->label_3_2, mainWidget_->node_3_2);

	// TOKENS_SCIENCE_SUPREME_ENTITY_PLATFORM
	tree_->appendChild(TOKENS_SCIENCE_SUPREME_ENTITY_PLATFORM, TOKENS_SCIENCE_SUPREME_ENTITY_CONSTRUCTION,
		mainWidget_->label_3_3, mainWidget_->node_3_3);

	tree_->appendChild(TOKENS_SCIENCE_SUPREME_ENTITY_PLATFORM, TOKENS_SCIENCE_SUPREME_ENTITY_EQUIPMENT,
		mainWidget_->label_3_4, mainWidget_->node_3_4);

	// TOKENS_SCIENCE_SUPREME_ENTITY_MODULES
	tree_->appendChild(TOKENS_SCIENCE_SUPREME_ENTITY_MODULES, TOKENS_SCIENCE_SUPREME_ENTITY_ENERGETIC_RING,
		mainWidget_->label_3_5, mainWidget_->node_3_5);

	tree_->appendChild(TOKENS_SCIENCE_SUPREME_ENTITY_MODULES, TOKENS_SCIENCE_SUPREME_ENTITY_RECOGNITION_DEVICES,
		mainWidget_->label_3_6, mainWidget_->node_3_6);

	// TOKENS_SCIENCE_SUPREME_ENTITY_PRODUCTION
	tree_->appendChild(TOKENS_SCIENCE_SUPREME_ENTITY_PRODUCTION, TOKENS_SCIENCE_SUPREME_ENTITY_FACTORIES,
		mainWidget_->label_3_7, mainWidget_->node_3_7);

	tree_->appendChild(TOKENS_SCIENCE_SUPREME_ENTITY_PRODUCTION, TOKENS_SCIENCE_SUPREME_ENTITY_QUARRYING_MACHINES,
		mainWidget_->label_3_8, mainWidget_->node_3_8);

	// TOKENS_SCIENCE_SUPREME_ENTITY_SUBSPACE_DRIVE
	tree_->appendLeaf(TOKENS_SCIENCE_SUPREME_ENTITY_SUBSPACE_DRIVE,TOKENS_SCIENCE_ENTITY_QUARK_GLUON_CORE,
		mainWidget_->label_4_1, mainWidget_->node_4_1);

	tree_->appendLeaf(TOKENS_SCIENCE_SUPREME_ENTITY_SUBSPACE_DRIVE, TOKENS_SCIENCE_ENTITY_NAVIGATION_COMPUTER,
		mainWidget_->label_4_2, mainWidget_->node_4_2);

	tree_->appendLeaf(TOKENS_SCIENCE_SUPREME_ENTITY_SUBSPACE_DRIVE, TOKENS_SCIENCE_ENTITY_SUBSPACE_BEACON,
		mainWidget_->label_4_3, mainWidget_->node_4_3);

	tree_->appendLeaf(TOKENS_SCIENCE_SUPREME_ENTITY_SUBSPACE_DRIVE, TOKENS_SCIENCE_ENTITY_INERTIAL_DAMPERS,
		mainWidget_->label_4_4, mainWidget_->node_4_4);

	// TOKENS_SCIENCE_SUPREME_ENTITY_IMPULS_DRIVE
	tree_->appendLeaf(TOKENS_SCIENCE_SUPREME_ENTITY_IMPULS_DRIVE, TOKENS_SCIENCE_ENTITY_IMPULS_JET_ENGINE,
		mainWidget_->label_4_5, mainWidget_->node_4_5);

	tree_->appendLeaf(TOKENS_SCIENCE_SUPREME_ENTITY_IMPULS_DRIVE, TOKENS_SCIENCE_ENTITY_MECHANICAL_DAMPERS,
		mainWidget_->label_4_6, mainWidget_->node_4_6);

	tree_->appendLeaf(TOKENS_SCIENCE_SUPREME_ENTITY_IMPULS_DRIVE, TOKENS_SCIENCE_ENTITY_DRONE_MISSILES,
		mainWidget_->label_4_7, mainWidget_->node_4_7);

	tree_->appendLeaf(TOKENS_SCIENCE_SUPREME_ENTITY_IMPULS_DRIVE, TOKENS_SCIENCE_ENTITY_DRONE_MISSILE_LAUNCHER,
		mainWidget_->label_4_8, mainWidget_->node_4_8);

	// TOKENS_SCIENCE_SUPREME_ENTITY_CONSTRUCTION
	tree_->appendLeaf(TOKENS_SCIENCE_SUPREME_ENTITY_CONSTRUCTION, TOKENS_SCIENCE_ENTITY_SHIPS,
		mainWidget_->label_4_9, mainWidget_->node_4_9);

	tree_->appendLeaf(TOKENS_SCIENCE_SUPREME_ENTITY_CONSTRUCTION, TOKENS_SCIENCE_ENTITY_BASES,
		mainWidget_->label_4_10, mainWidget_->node_4_10);

	tree_->appendLeaf(TOKENS_SCIENCE_SUPREME_ENTITY_CONSTRUCTION, TOKENS_SCIENCE_ENTITY_HANGAR,
		mainWidget_->label_4_11, mainWidget_->node_4_11);

	tree_->appendLeaf(TOKENS_SCIENCE_SUPREME_ENTITY_CONSTRUCTION, TOKENS_SCIENCE_ENTITY_ANDROIDS,
		mainWidget_->label_4_12, mainWidget_->node_4_12);

	// TOKENS_SCIENCE_SUPREME_ENTITY_EQUIPMENT
	tree_->appendLeaf(TOKENS_SCIENCE_SUPREME_ENTITY_EQUIPMENT, TOKENS_SCIENCE_ENTITY_ENERGETIC_ACCUMULATOR,
		mainWidget_->label_4_13, mainWidget_->node_4_13);

	tree_->appendLeaf(TOKENS_SCIENCE_SUPREME_ENTITY_EQUIPMENT, TOKENS_SCIENCE_ENTITY_MAINFRAME,
		mainWidget_->label_4_14, mainWidget_->node_4_14);

	tree_->appendLeaf(TOKENS_SCIENCE_SUPREME_ENTITY_EQUIPMENT, TOKENS_SCIENCE_ENTITY_SCIENTIFIC_LAB,
		mainWidget_->label_4_15, mainWidget_->node_4_15);

	tree_->appendLeaf(TOKENS_SCIENCE_SUPREME_ENTITY_EQUIPMENT, TOKENS_SCIENCE_ENTITY_LIFE_SUPPORT,
		mainWidget_->label_4_16, mainWidget_->node_4_16);

	// TOKENS_SCIENCE_SUPREME_ENTITY_ENERGETIC_RING
	tree_->appendLeaf(TOKENS_SCIENCE_SUPREME_ENTITY_ENERGETIC_RING, TOKENS_SCIENCE_ENTITY_WEAPON_ENERGY_CIRCUIT,
		mainWidget_->label_4_17, mainWidget_->node_4_17);

	tree_->appendLeaf(TOKENS_SCIENCE_SUPREME_ENTITY_ENERGETIC_RING, TOKENS_SCIENCE_ENTITY_TRANSPORT_BEAM_GENERATOR,
		mainWidget_->label_4_18, mainWidget_->node_4_18);

	tree_->appendLeaf(TOKENS_SCIENCE_SUPREME_ENTITY_ENERGETIC_RING, TOKENS_SCIENCE_ENTITY_LASER_GUN,
		mainWidget_->label_4_19, mainWidget_->node_4_19);

	tree_->appendLeaf(TOKENS_SCIENCE_SUPREME_ENTITY_ENERGETIC_RING, TOKENS_SCIENCE_ENTITY_DEFLECTOR_SHIELDS,
		mainWidget_->label_4_20, mainWidget_->node_4_20);

	// TOKENS_SCIENCE_SUPREME_ENTITY_RECOGNITION_DEVICES
	tree_->appendLeaf(TOKENS_SCIENCE_SUPREME_ENTITY_RECOGNITION_DEVICES, TOKENS_SCIENCE_ENTITY_LONG_RANGE_SENSORS,
		mainWidget_->label_4_21, mainWidget_->node_4_21);

	tree_->appendLeaf(TOKENS_SCIENCE_SUPREME_ENTITY_RECOGNITION_DEVICES, TOKENS_SCIENCE_ENTITY_HIGH_RESOLUTION_SENSORS,
		mainWidget_->label_4_22, mainWidget_->node_4_22);

	tree_->appendLeaf(TOKENS_SCIENCE_SUPREME_ENTITY_RECOGNITION_DEVICES, TOKENS_SCIENCE_ENTITY_CLOAKING_DEVICE,
		mainWidget_->label_4_23, mainWidget_->node_4_23);

	tree_->appendLeaf(TOKENS_SCIENCE_SUPREME_ENTITY_RECOGNITION_DEVICES, TOKENS_SCIENCE_ENTITY_PROBE,
		mainWidget_->label_4_24, mainWidget_->node_4_24);

	// TOKENS_SCIENCE_SUPREME_ENTITY_FACTORIES
	tree_->appendLeaf(TOKENS_SCIENCE_SUPREME_ENTITY_FACTORIES, TOKENS_SCIENCE_ENTITY_MAGNETICAL_FILTER,
		mainWidget_->label_4_25, mainWidget_->node_4_25);

	tree_->appendLeaf(TOKENS_SCIENCE_SUPREME_ENTITY_FACTORIES, TOKENS_SCIENCE_ENTITY_DEUTERIUM_DECOMPOSER,
		mainWidget_->label_4_26, mainWidget_->node_4_26);

	tree_->appendLeaf(TOKENS_SCIENCE_SUPREME_ENTITY_FACTORIES, TOKENS_SCIENCE_ENTITY_ZIRCONIUM_REACTOR,
		mainWidget_->label_4_27, mainWidget_->node_4_27);

	tree_->appendLeaf(TOKENS_SCIENCE_SUPREME_ENTITY_FACTORIES,TOKENS_SCIENCE_ENTITY_ILMENITE_FURNACE,
		mainWidget_->label_4_28, mainWidget_->node_4_28);

	// TOKENS_SCIENCE_SUPREME_ENTITY_QUARRYING_MACHINES
	tree_->appendLeaf(TOKENS_SCIENCE_SUPREME_ENTITY_QUARRYING_MACHINES, TOKENS_SCIENCE_ENTITY_MAGNETICAL_COLLECTORS_OF_PLASMA,
		mainWidget_->label_4_29, mainWidget_->node_4_29);

	tree_->appendLeaf(TOKENS_SCIENCE_SUPREME_ENTITY_QUARRYING_MACHINES, TOKENS_SCIENCE_ENTITY_SUBMERSIBLE_FILTRATION_TANK,
		mainWidget_->label_4_30, mainWidget_->node_4_30);

	tree_->appendLeaf(TOKENS_SCIENCE_SUPREME_ENTITY_QUARRYING_MACHINES, TOKENS_SCIENCE_ENTITY_SUBMARINE_MINING_MACHINE,
		mainWidget_->label_4_31, mainWidget_->node_4_31);

	tree_->appendLeaf(TOKENS_SCIENCE_SUPREME_ENTITY_QUARRYING_MACHINES, TOKENS_SCIENCE_ENTITY_UNDERGROUND_DRILLING_MACHINE,
		mainWidget_->label_4_32, mainWidget_->node_4_32);

	/* PARAMETERS */

	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_QUARK_GLUON_CORE, SPEC_PTS_QUARK_GLUON_CORE_SPEED, GameParams::QUARK_GLUON_CORE_SPEED);
	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_QUARK_GLUON_CORE, SPEC_PTS_QUARK_GLUON_CORE_CONSUMPTION, GameParams::QUARK_GLUON_CORE_CONSUMPTION);

	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_NAVIGATION_COMPUTER, SPEC_PTS_NAVIGATION_COMPUTER_SPEED, GameParams::NAVIGATION_COMPUTER_SPEED);

	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_SUBSPACE_BEACON, SPEC_PTS_SUBSPACE_BEACON_SPEED, GameParams::SUBSPACE_BEACON_SPEED);

	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_INERTIAL_DAMPERS, SPEC_PTS_INERTIAL_DAMPERS_SPEED, GameParams::INERTIAL_DAMPERS_SPEED);

	// TOKENS_SCIENCE_SUPREME_ENTITY_IMPULS_DRIVE
	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_IMPULS_JET_ENGINE, SPEC_PTS_IMPULS_ENGINE_FORCE, GameParams::IMPULS_ENGINE_FORCE);
	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_IMPULS_JET_ENGINE, SPEC_PTS_IMPULS_ENGINE_CONSUMPTION, GameParams::IMPULS_ENGINE_CONSUMPTION);

	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_MECHANICAL_DAMPERS, SPEC_PTS_MECHANICAL_DAMPERS_ACCELERATION, GameParams::MECHANICAL_DAMPERS_ACCELERATION);

	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_DRONE_MISSILES, SPEC_PTS_DRONE_MISSILES_DAMAGE, GameParams::DRONE_MISSILES_DAMAGE);
	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_DRONE_MISSILES, SPEC_PTS_DRONE_MISSILES_PRECISION, GameParams::DRONE_MISSILES_PRECISION);
	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_DRONE_MISSILES, SPEC_PTS_DRONE_MISSILES_RANGE, GameParams::DRONE_MISSILES_RANGE);

	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_DRONE_MISSILE_LAUNCHER, SPEC_PTS_DRONE_MISSILE_LAUNCHER_ACCUMULATOR, GameParams::DRONE_MISSILE_LAUNCHER_ACCUMULATOR);
	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_DRONE_MISSILE_LAUNCHER, SPEC_PTS_DRONE_MISSILE_LAUNCHER_CHARGING, GameParams::DRONE_MISSILE_LAUNCHER_CHARGING);

	// TOKENS_SCIENCE_SUPREME_ENTITY_CONSTRUCTION
	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_SHIPS, SPEC_PTS_SHIPS_LEVEL, GameParams::SHIPS_LEVEL);

	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_BASES, SPEC_PTS_BASES_LEVEL, GameParams::BASES_LEVEL);

	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_HANGAR, SPEC_PTS_HANGARS_LEVEL, GameParams::HANGARS_LEVEL);

	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_ANDROIDS, SPEC_PTS_ANDROIDS_LEVEL, GameParams::ANDROIDS_LEVEL);
	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_ANDROIDS, SPEC_PTS_ANDROIDS_CONSUMPTION, GameParams::ANDROIDS_CONSUMPTION);

	// TOKENS_SCIENCE_SUPREME_ENTITY_EQUIPMENT
	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_ENERGETIC_ACCUMULATOR, SPEC_PTS_ENERGETIC_ACCUMULATOR_ACCUMULATOR, GameParams::ENERGETIC_ACCUMULATOR_ACCUMULATOR);
	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_ENERGETIC_ACCUMULATOR, SPEC_PTS_ENERGETIC_ACCUMULATOR_CHARGING, GameParams::ENERGETIC_ACCUMULATOR_CHARGING);

	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_MAINFRAME, SPEC_PTS_MAINFRAME_BIO_NODES, GameParams::MAINFRAME_BIO_NODES);
	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_MAINFRAME, SPEC_PTS_MAINFRAME_PREDICTION, GameParams::MAINFRAME_PREDICTION);
	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_MAINFRAME, SPEC_PTS_MAINFRAME_TIME_REUSABILITY, GameParams::MAINFRAME_TIME_REUSABILITY);
	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_MAINFRAME, SPEC_PTS_MAINFRAME_CONSUMPTION, GameParams::MAINFRAME_CONSUMPTION);

	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_SCIENTIFIC_LAB, SPEC_PTS_SCIENTIFIC_LABS_LEVEL, GameParams::SCIENTIFIC_LABS_LEVEL);

	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_LIFE_SUPPORT, SPEC_PTS_LIFE_SUPPORT_CAPACITY, GameParams::LIFE_SUPPORT_CAPACITY);
	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_LIFE_SUPPORT, SPEC_PTS_LIFE_SUPPORT_CONSUMPTION, GameParams::LIFE_SUPPORT_CONSUMPTION);

	// TOKENS_SCIENCE_SUPREME_ENTITY_ENERGETIC_RING
	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_WEAPON_ENERGY_CIRCUIT, SPEC_PTS_WEAPON_ENERGY_CIRCUIT_CHARGING, GameParams::WEAPON_ENERGY_CIRCUIT_CHARGING);

	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_TRANSPORT_BEAM_GENERATOR, SPEC_PTS_TRANSPORT_BEAM_GENERATOR_ACCUMULATOR, GameParams::TRANSPORT_BEAM_GENERATOR_ACCUMULATOR);
	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_TRANSPORT_BEAM_GENERATOR, SPEC_PTS_TRANSPORT_BEAM_GENERATOR_CHARGING, GameParams::TRANSPORT_BEAM_GENERATOR_CHARGING);
	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_TRANSPORT_BEAM_GENERATOR, SPEC_PTS_TRANSPORT_BEAM_GENERATOR_FORCIBILITY, GameParams::TRANSPORT_BEAM_GENERATOR_FORCIBILITY);
	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_TRANSPORT_BEAM_GENERATOR, SPEC_PTS_TRANSPORT_BEAM_GENERATOR_PRECISION, GameParams::TRANSPORT_BEAM_GENERATOR_PRECISION);
	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_TRANSPORT_BEAM_GENERATOR, SPEC_PTS_TRANSPORT_BEAM_GENERATOR_RANGE, GameParams::TRANSPORT_BEAM_GENERATOR_RANGE);

	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_LASER_GUN, SPEC_PTS_LASER_GUN_ACCUMULATOR, GameParams::LASER_GUN_ACCUMULATOR);
	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_LASER_GUN, SPEC_PTS_LASER_GUN_CHARGING, GameParams::LASER_GUN_CHARGING);
	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_LASER_GUN, SPEC_PTS_LASER_GUN_FORCIBILITY, GameParams::LASER_GUN_FORCIBILITY);
	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_LASER_GUN, SPEC_PTS_LASER_GUN_PRECISION, GameParams::LASER_GUN_PRECISION);
	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_LASER_GUN, SPEC_PTS_LASER_GUN_RANGE, GameParams::LASER_GUN_RANGE);

	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_DEFLECTOR_SHIELDS, SPEC_PTS_DEFLECTOR_SHIELDS_GENERATOR_SOLIDITY, GameParams::DEFLECTOR_SHIELDS_GENERATOR_SOLIDITY);
	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_DEFLECTOR_SHIELDS, SPEC_PTS_DEFLECTOR_SHIELDS_GENERATOR_ACCUMULATOR, GameParams::DEFLECTOR_SHIELDS_GENERATOR_ACCUMULATOR);
	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_DEFLECTOR_SHIELDS, SPEC_PTS_DEFLECTOR_SHIELDS_GENERATOR_CHARGING, GameParams::DEFLECTOR_SHIELDS_GENERATOR_CHARGING);

	// TOKENS_SCIENCE_SUPREME_ENTITY_RECOGNITION_DEVICES
	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_LONG_RANGE_SENSORS, SPEC_PTS_LONG_RANGE_SENSORS_LEVEL, GameParams::LONG_RANGE_SENSORS_LEVEL);
	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_LONG_RANGE_SENSORS, SPEC_PTS_LONG_RANGE_SENSORS_RANGE, GameParams::LONG_RANGE_SENSORS_RANGE);
	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_LONG_RANGE_SENSORS, SPEC_PTS_LONG_RANGE_SENSORS_CONSUMPTION, GameParams::LONG_RANGE_SENSORS_CONSUMPTION);

	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_HIGH_RESOLUTION_SENSORS, SPEC_PTS_HIGH_RESOLUTION_SENSORS_LEVEL, GameParams::HIGH_RESOLUTION_SENSORS_LEVEL);
	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_HIGH_RESOLUTION_SENSORS, SPEC_PTS_HIGH_RESOLUTION_SENSORS_RANGE, GameParams::HIGH_RESOLUTION_SENSORS_RANGE);
	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_HIGH_RESOLUTION_SENSORS, SPEC_PTS_HIGH_RESOLUTION_SENSORS_CONSUMPTION, GameParams::HIGH_RESOLUTION_SENSORS_CONSUMPTION);

	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_CLOAKING_DEVICE, SPEC_PTS_CLOAKING_DEVICE_LEVEL, GameParams::CLOAKING_DEVICE_LEVEL);
	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_CLOAKING_DEVICE, SPEC_PTS_CLOAKING_DEVICE_CONSUMPTION, GameParams::CLOAKING_DEVICE_CONSUMPTION);

	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_PROBE, SPEC_PTS_PROBE_DEVICE_LEVEL, GameParams::PROBE_DEVICE_LEVEL);

	// TOKENS_SCIENCE_SUPREME_ENTITY_FACTORIES
	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_MAGNETICAL_FILTER, SPEC_PTS_MAGNETICAL_FILTER_PRODUCTION, GameParams::MAGNETICAL_FILTER_PRODUCTION);

	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_DEUTERIUM_DECOMPOSER, SPEC_PTS_DEUTERIUM_DECOMPOSER_PRODUCTION, GameParams::DEUTERIUM_DECOMPOSER_PRODUCTION);
	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_DEUTERIUM_DECOMPOSER, SPEC_PTS_DEUTERIUM_DECOMPOSER_METAMORPHOSIS, GameParams::DEUTERIUM_DECOMPOSER_METAMORPHOSIS);

	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_ZIRCONIUM_REACTOR, SPEC_PTS_ZIRCONIUM_REACTOR_PRODUCTION, GameParams::ZIRCONIUM_REACTOR_PRODUCTION);
	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_ZIRCONIUM_REACTOR, SPEC_PTS_ZIRCONIUM_REACTOR_METAMORPHOSIS, GameParams::ZIRCONIUM_REACTOR_METAMORPHOSIS);

	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_ILMENITE_FURNACE, SPEC_PTS_ILMENITE_FURNACE_PRODUCTION, GameParams::ILMENITE_FURNACE_PRODUCTION);
	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_ILMENITE_FURNACE, SPEC_PTS_ILMENITE_FURNACE_METAMORPHOSIS, GameParams::ILMENITE_FURNACE_METAMORPHOSIS);

	// TOKENS_SCIENCE_SUPREME_ENTITY_QUARRYING_MACHINES
	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_MAGNETICAL_COLLECTORS_OF_PLASMA, SPEC_PTS_MAGNETICAL_COLLECTORS_OF_PLASMA_WINDING_SPEED, GameParams::MAGNETICAL_COLLECTORS_OF_PLASMA_WINDING_SPEED);

	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_SUBMERSIBLE_FILTRATION_TANK, SPEC_PTS_SUBMERSIBLE_FILTRATION_TANK_WINDING_SPEED, GameParams::SUBMERSIBLE_FILTRATION_TANK_WINDING_SPEED);
	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_SUBMERSIBLE_FILTRATION_TANK, SPEC_PTS_SUBMERSIBLE_FILTRATION_TANK_RANGE, GameParams::SUBMERSIBLE_FILTRATION_TANK_RANGE);

	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_SUBMARINE_MINING_MACHINE, SPEC_PTS_SUBMARINE_MINING_MACHINE_WINDING_SPEED, GameParams::SUBMARINE_MINING_MACHINE_WINDING_SPEED);
	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_SUBMARINE_MINING_MACHINE, SPEC_PTS_SUBMARINE_MINING_MACHINE_RANGE, GameParams::SUBMARINE_MINING_MACHINE_RANGE);

	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_UNDERGROUND_DRILLING_MACHINE, SPEC_PTS_UNDERGROUND_DRILLING_MACHINE_WINDING_SPEED, GameParams::UNDERGROUND_QUARRYING_WINDING_SPEED);
	tree_->appendComposite(TOKENS_SCIENCE_ENTITY_UNDERGROUND_DRILLING_MACHINE, SPEC_PTS_UNDERGROUND_DRILLING_MACHINE_RANGE, GameParams::UNDERGROUND_QUARRYING_RANGE);
	
}

ScienceTreeWidget::~ScienceTreeWidget()
{
	delete tree_;
	delete mainWidget_;
}

void ScienceTreeWidget::appendComposite(const std::string & parentName, const std::string & nodeName, QString graphicalName)
{
	tree_->appendComposite(parentName, nodeName, graphicalName);
}

void ScienceTreeWidget::setTokenMultiplicators(double * values, int count)
{
	tree_->setTokenMultiplicators(values, count);
}

void ScienceTreeWidget::setShowTokens(EShowingOptions enabled)
{
	tree_->setShowTokens(enabled);
}

void ScienceTreeWidget::setShowPoints(EShowingOptions enabled)
{
	tree_->setShowPoints(enabled);
}

sciencetree_raw_data ScienceTreeWidget::getState() {
	return tree_->getState();
}

void ScienceTreeWidget::setState(ScienceTreeRawUpdateData & data) {
	if (!isLockedForUpdate_) {
		tree_->setState(data);

		lockForUpdate(false);
	}
}

void ScienceTreeWidget::lockForUpdate(bool lock) {
	isLockedForUpdate_ = lock;

	if (lock) {
		mainWidget_->scienceSection->setAsChanged();
	}
	else {
		mainWidget_->scienceSection->setAsNormal();
	}
}
