PROCEDURE SelectFixtures;

{*	Builds an array of Lighting Device types and allows user to select all of each type in drawing
	Developed by: Jesse Cogswell
	Date: 4/24/2023
	Revisions:
*}

CONST

	kLightDevice = 'Speaker';
	kInstType = 'BxModel';
	
VAR

	lightTypes:DYNARRAY[] OF STRING;
	numTypes:INTEGER;
	dialog,layoutDialog:LONGINT;

PROCEDURE AddToArray(h:HANDLE);

{Adds lighting device to array}

	LABEL 99; {Escape}
	
	VAR
	
		testStr:STRING;
		i:INTEGER;

	BEGIN
		testStr:=GetRField(h,kLightDevice,kInstType);
		
		FOR i:=1 TO numTypes DO
			BEGIN
				IF(lightTypes[i] = testStr) THEN GOTO 99;
			END;
		
		numTypes:=numTypes+1;
		ALLOCATE lightTypes[1..numTypes];
		lightTypes[numTypes]:=testStr;
		
		99: {Escape}
	END;

FUNCTION DrawDialog(DName:STRING) : LONGINT;

{Creates dialog box and returns dialog ID}

	CONST

		kWidth = 50;
		kHeight = 30;

	VAR

		dia:LONGINT;
	
	BEGIN
		dia:=CreateLayout(DName,FALSE,'OK','Cancel');
		
		CreateListBoxN(dia,11,kWidth,kHeight,TRUE);
		
		CreateGroupBox(dia,101,'Instrument Types',TRUE);
		
		SetFirstGroupItem(dia,101,11);
		SetFirstLayoutItem(dia,101);
		
		DrawDialog:=dia;
	END;

PROCEDURE DialogHandler(VAR item,data:LONGINT);

{Handles dialog box}

	VAR

		i,index:INTEGER;
		selectInst:STRING;
	
	BEGIN
		CASE item OF
			SetupDialogC:
				BEGIN
					FOR i:=1 TO numTypes DO AddChoice(dialog,11,lightTypes[i],i-1); 
				END;
			1: {OK}
				BEGIN
					FOR i:=1 TO numTypes DO
						BEGIN
							GetSelectedChoiceInfo(dialog,11,i-1,index,selectInst);
							IF(index <> -1) THEN ForEachObject(SetSelect,((R IN [kLightDevice]) & (kLightDevice.kInstType = selectInst)));
						END;
				END;
			2: {Cancel}
				BEGIN
				END;
		END;
	END;

BEGIN
	numTypes:=0;
	DSelectObj(ALL);
	
	ForEachObject(AddToArray,(R IN [kLightDevice]));
	
	IF(numTypes>0) THEN
		BEGIN
			SortArray(lightTypes,numTypes,0);
			
			dialog:=DrawDialog('Selectionner les enceintes');
			layoutDialog:=RunLayoutDialog(dialog,DialogHandler);
		END
	ELSE AlrtDialog('Pas d enceinte trouvée');
END;

Run(SelectFixtures);