void SProjectLauncherDeviceGroupSelector::SetSelectedGroup(const ILauncherDeviceGroupPtr& DeviceGroup)
{
	if (!DeviceGroup.IsValid() || ProfileManager->GetAllDeviceGroups().Contains(DeviceGroup))
	{
		DeviceGroupComboBox->SetSelectedItem(DeviceGroup);
	}
}
ILauncherProfilePtr FLauncherProfileManager::LoadJSONProfile(FString ProfileFile)
{
	FLauncherProfile* Profile = new FLauncherProfile(AsShared());

	FString FileContents;
	if (!FFileHelper::LoadFileToString(FileContents, *ProfileFile))
	{
		return nullptr;
	}

	TSharedPtr<FJsonObject> Object;
	TSharedRef<TJsonReader<> > Reader = TJsonReaderFactory<>::Create(FileContents);
	if (!FJsonSerializer::Deserialize(Reader, Object) || !Object.IsValid())
	{
		return nullptr;
	}

	if (Profile->Load(*(Object.Get())))
	{
		ILauncherDeviceGroupPtr DeviceGroup = GetDeviceGroup(Profile->GetDeployedDeviceGroupId());
		if (!DeviceGroup.IsValid())
		{
			DeviceGroup = AddNewDeviceGroup();
		}
		Profile->SetDeployedDeviceGroup(DeviceGroup);

		return MakeShareable(Profile);
	}

	return nullptr;
}
FText SProjectLauncherDeviceGroupSelector::HandleDeviceGroupComboBoxWidgetText(ILauncherDeviceGroupPtr Group) const
{
	if (Group.IsValid())
	{
		return FText::FromString(Group->GetName());
	}

	return FText::GetEmpty();
}
FString SProjectLauncherDeviceGroupSelector::HandleDeviceGroupComboBoxGetEditableText()
{
	ILauncherDeviceGroupPtr SelectedGroup = DeviceGroupComboBox->GetSelectedItem();

	if (SelectedGroup.IsValid())
	{
		return SelectedGroup->GetName();
	}

	return FString();
}
FText SProjectLauncherDeviceGroupSelector::HandleDeviceGroupComboBoxContent() const
{
	ILauncherDeviceGroupPtr SelectedGroup = DeviceGroupComboBox->GetSelectedItem();

	if (SelectedGroup.IsValid())
	{
		return FText::FromString(SelectedGroup->GetName());
	}

	return LOCTEXT("CreateOrSelectGroupText", "Create or select a device group...");
}
ILauncherProfilePtr FLauncherProfileManager::LoadProfile( FArchive& Archive )
{
	FLauncherProfile* Profile = new FLauncherProfile(AsShared());

	if (Profile->Serialize(Archive))
	{
		ILauncherDeviceGroupPtr DeviceGroup = GetDeviceGroup(Profile->GetDeployedDeviceGroupId());
		if (!DeviceGroup.IsValid())
		{
			DeviceGroup = AddNewDeviceGroup();	
		}
		Profile->SetDeployedDeviceGroup(DeviceGroup);

		return MakeShareable(Profile);
	}

	return nullptr;
}
void FLauncherProfileManager::AddDeviceGroup( const ILauncherDeviceGroupRef& DeviceGroup )
{
	if (!DeviceGroups.Contains(DeviceGroup))
	{
		// replace the existing device group
		ILauncherDeviceGroupPtr ExistingGroup = GetDeviceGroup(DeviceGroup->GetId());

		if (ExistingGroup.IsValid())
		{
			RemoveDeviceGroup(ExistingGroup.ToSharedRef());
		}

		// add the new device group
		DeviceGroups.Add(DeviceGroup);
		SaveDeviceGroups();

		DeviceGroupAddedDelegate.Broadcast(DeviceGroup);
	}
}
FReply SProjectLauncherDeviceGroupSelector::HandleDeviceGroupComboBoxRemoveClicked()
{
	ILauncherDeviceGroupPtr SelectedGroup = DeviceGroupComboBox->GetSelectedItem();

	if (SelectedGroup.IsValid())
	{
		ProfileManager->RemoveDeviceGroup(SelectedGroup.ToSharedRef());
	}

	if (ProfileManager->GetAllDeviceGroups().Num() > 0)
	{
		DeviceGroupComboBox->SetSelectedItem(ProfileManager->GetAllDeviceGroups()[0]);
	}
	else
	{
		DeviceGroupComboBox->SetSelectedItem(NULL);
	}

	return FReply::Handled();
}