//========================================================================
// E3D library
//
// Selection
//
// AUTHOR:  Gabor Nagy
// DATE:    2025-Mar-12 20:14:40
//
// Copyright (C) 1996-2026 by Gabor Nagy. All rights reserved.
//========================================================================
#ifndef _E3D_PickInput_h
#define _E3D_PickInput_h


#if defined (__cplusplus) && (!(defined (__CPLUSLIBS)))
extern "C" {
#endif


//================================================================
// Pick new profile (Spline or Face) callback
//================================================================
static void _PickCB_InputGeometry(E3dPickCallbackStruct* PPickStruct, EPointer PClientData,  const EIndex PNumOfItems, E3dItem* PPickedItems)
{
	XeApp_Sync(Xe_App);

	if(PPickStruct->Reason == E3dCR_ABORT)
	{
		_TmpInfo  = NULL;
		_TmpPanel = NULL;
		return;
	}

// Cancel
//
	if(PPickStruct->Button == Button3)
	{
		E3dp_PickEnd();
		_TmpInfo  = NULL;
		_TmpPanel = NULL;
		return;
	}

	E3dScene*  LScene = E3d_Scene;

	switch(PPickStruct->Button)
	{
		case Button1:
			if(PNumOfItems)
			{
				E3dItem*  LItem = PPickedItems;
				E3dInfo*  LInfo = _TmpInfo;

//char  s[512];printf("%s() %s:%d [%s]\n", __FUNCTION__, __FILE__, __LINE__, E3d_ItemFlagsToString(LItem->Item.Type, s));fflush(stdout);
				if((LItem->Item.Type & _PickInputGeometryObjectTypes) || LItem->Item.Type == E3dItemMODEL)
				{
					E3dObject*    LDstObject = _TmpPanel ? _TmpPanel->Object : NULL;
					EChangeFlags  LChanged = 0;

					if(LDstObject && LInfo)
					{
						if(LInfo->InputGeometry)
						{
// Remove old profile Geometry and remove us from its Output list
//
							E3dBaseObject_RemoveOutput((E3dBaseObject*)LInfo->InputGeometry, &LInfo->InputInterface);
							E_Delete(LInfo->InputGeometry);
						}

						E3dModel*  LProfileModel = NULL;
						EIndex     LNInputGeometries = 0;

						switch(LItem->Item.Type)
						{
							case E3dItemMODEL:
								{
									E3dObjItem*  LObjItem = (E3dObjItem*)LItem;

									LProfileModel = (E3dModel*)(LObjItem->Node);if(LProfileModel)
									{
										E3dObject*  LProfileObject = NULL;

										LProfileModel->Selection = E3dNodeSelOBJECT;

										EIndex  LObjI = 0;const EIndex  LObjN = LProfileModel->Objects.Count;E3dObject**  LObjects = LProfileModel->Objects.Items;for(;LObjI < LObjN;LObjI++)
										{
											E3dObject*  LObject = LObjects[LObjI];

											if(EObject_IsSubClass(LObject, E3dSpline)) { LProfileObject = LObject;break; }
										}

										if(LProfileObject)
										{
											E_AssignToMember(LProfileObject, LInfo,InputGeometry);
											LProfileObject->Selection = E3dObjectSelOBJECT;
											LNInputGeometries++;
										}
									}
								}
							break;

							default:
								{
									E3dObjItem*  LObjItem = (E3dObjItem*)LItem;
									E3dObject*   LObject = LObjItem->Object;

									if(LItem->Item.Type & _PickInputGeometryObjectTypes)
									{
										E_AssignToMember(LObject, LInfo,InputGeometry);
										LObject->Selection = E3dObjectSelOBJECT;
										LNInputGeometries++;
									}
								}
							break;
						}

						if(LNInputGeometries)
						{
							if(LInfo->InputGeometry) E3dBaseObject_AddOutput((E3dBaseObject*)LInfo->InputGeometry, &LInfo->InputInterface);

							if(LProfileModel)
							{
								if(LNInputGeometries > 1) E3dp_PrintMessage(ELogMESSAGE, 3000, "%s: Picked Node '%s' for input (%d Splines)", LInfo->Class->Name, LProfileModel->Name, LNInputGeometries);
								else E3dp_PrintMessage(ELogMESSAGE, 3000, "%s: Picked '%s' for input (1 Spline)", LInfo->Class->Name, LProfileModel->Name);
							}
							else
							{
								E3dGeometry*  LObject = LInfo->InputGeometry;
								if(EObject_IsSubClass(LObject, E3dSpline)) E3dp_PrintMessage(ELogMESSAGE, 3000, "%s: Picked Spline '%s' for profile", LInfo->Class->Name, LObject->Name);
								else E3dp_PrintMessage(ELogMESSAGE, 3000, "%s: Picked Face '%s' for profile", LInfo->Class->Name, LObject->Name);
							}

							E3dObject_CustomUpdate(LDstObject, LInfo,  E3dCallALL,  &LChanged);
						}
					}

					E3dp_PickEnd();
					if(LChanged) E3dWindows_RedrawByChangeFlags(LScene, LChanged);
					else E3dp_PrintMessage(ELogMESSAGE, 3000, "%s: %s", LInfo->Class->Name, _PickInputGeometryTypeWarningStr);
				}
				else { E3dp_PrintMessage(ELogMESSAGE, 3000, "%s: %s", LInfo->Class->Name, _PickInputGeometryTypeWarningStr);return; }
			}
		break;
	}

	_TmpInfo  = NULL;
	_TmpPanel = NULL;
}



#if defined (__cplusplus) && (!(defined (__CPLUSLIBS)))
}	// Close scope of 'extern "C"' declaration
#endif

#endif	// _E3D_PickInput_h
