void ChangeNodeHeirarchy::Execute()
{
	HierarchyTreeNode* targetNode = HierarchyTreeController::Instance()->GetTree().GetNode(targetNodeID);
	HierarchyTreeNode* insertAfterNode = HierarchyTreeController::Instance()->GetTree().GetNode(afterNodeID);
	if (!targetNode)
	{
		// Possible in Redo case if some changes in tree were made.
		return;
	}

	
	for (HierarchyTreeNode::HIERARCHYTREENODESIDLIST::iterator iter = items.begin();
		 iter != items.end();
		 ++iter)
	{
		HierarchyTreeNode* node = HierarchyTreeController::Instance()->GetTree().GetNode((*iter));
		if (node)
		{
			if (dynamic_cast<HierarchyTreeScreenNode*>(node))
			{
				HierarchyTreeNode* sourceNode = node->GetParent();

				if (sourceNode != targetNode)
				{
					HierarchyTreeNode::HIERARCHYTREENODESLIST screens;
					screens.push_back(node);
					HierarchyTreeController::Instance()->DeleteNodesFiles(screens);
				}
			}

			//YZ backlight parent rect
			bool isNodeSelected = false;
			HierarchyTreeControlNode* controlNode = dynamic_cast<HierarchyTreeControlNode*>(node);
			if (controlNode)
			{
				isNodeSelected = HierarchyTreeController::Instance()->IsControlSelected(controlNode);
				HierarchyTreeController::Instance()->UnselectControl(controlNode);
			}

			node->SetParent(targetNode, insertAfterNode);
			//insertAfterNode = node;
			
			if (isNodeSelected)
			{
				HierarchyTreeController::Instance()->SelectControl(controlNode);
			}
		}
	}
	
	HierarchyTreeController::Instance()->EmitHierarchyTreeUpdated(false);
	HierarchyTreeController::Instance()->ResetSelectedControl();
}
void ChangeNodeHeirarchy::Rollback()
{
	for (PARENTNODESMAPITER iter = previousParents.begin(); iter != previousParents.end(); iter ++)
	{
		HierarchyTreeNode* currentNode = HierarchyTreeController::Instance()->GetTree().GetNode(iter->first);
		HierarchyTreeNode* prevParentNode = HierarchyTreeController::Instance()->GetTree().GetNode(iter->second.parent);
		HierarchyTreeNode* prevAddedAfter = HierarchyTreeController::Instance()->GetTree().GetNode(iter->second.addedAfter);
		
		if (!currentNode || !prevParentNode)
		{
			continue;
		}
		
		currentNode->SetParent(prevParentNode, prevAddedAfter);
	}
	
	HierarchyTreeController::Instance()->EmitHierarchyTreeUpdated();
}