// GUI to launch the fn_fuzzymerge_generic
// this will perform fuzzymerge on two open tables.
//
// to operate:
// 1. pick two open tables, one main and one to merge with
// 2. pick two columns (must be continuous) to guide merge, one main and one to merge with
// 3. enjoy fuzzymerge
//
// klkriech@jpl.nasa.gov

Names Default To Here( 1 );

// get list of tables
If( N Table() < 2,
	Print( "You must have at least 2 tables open for this to do anything." );
	Stop();
,
	openDTs = List();
	For( i = 1, i <= N Table(), i++,
		Insert Into( openDTs, Data Table( i ) << Get Name() )
	);
);

// init for validate
col_main = .;
col_with = .;
dt_main = .;
dt_with = .;

win = New Window( "FuzzyMerge GUI",
	<<modal,
	<<on validate(
		// must have two different data tables and one column each selected to continue
		If( ((Is Missing( col_main ) | Is Missing( col_with )) | (dt_main == dt_with)),
			0,
			1
		)
	),
	V List Box(
		Text Box(
			"<font size=\!"12\!">Fuzzymerge - merge two tables with slightly different timestamps</font>\!N
			1. Select the main table that you want to merge data into
			2. Select the column from the main table you want to use - usually this is a time column
			3. Select the 'with' table that you want to pull data from
			4. Select the column from the 'with' table to match with the column from the main table.  This is usually a time column
			5. For each row in the main table, the closest row (forward or backward) in the with table will be joined\!N",
			<<Markup,
			<<Set Wrap( 800 )
		),
		Text Box( "" ),
		H List Box(	
		// main table
			Panel Box( "1. Select main data table",
				mtb = List Box(
					openDTs,
					max selected( 1 ),
					mtc << delete box(); // clear the box, see https://community.jmp.com/t5/Discussions/Update-Col-List-Box/td-p/17836
					pb1 << Append(
						mtc = Col List Box(
							Data Table( (mtb << get selected)[1] ),
							"all",
							maxSelected( 1 ),
							minItems( 1 ),
							<<set analysis type( "continuous" ), // force cols to only be continuous
							col_main = (mtc << get selected)[1];
							//show(col_main);
							dt_main = (mtb << get selected)[1];
							//show(dt_main);
						)
					);
				)
			),
			pb1 = Panel Box( "2. Main column to merge on", mtc = Col List Box() ), 
		// with table
			Panel Box( "3. Select with data table",
				wtb = List Box(
					openDTs,
					max selected( 1 ),
					wtc << delete box(); // clear the box, see https://community.jmp.com/t5/Discussions/Update-Col-List-Box/td-p/17836
					pb2 << Append(
						wtc = Col List Box(
							Data Table( (wtb << get selected)[1] ),
							"all",
							maxSelected( 1 ),
							minItems( 1 ),
							<<set analysis type( "continuous" ), // force cols to only be continuous
							col_with = (wtc << get selected)[1];
							//show(col_with);
							dt_with = (wtb << get selected)[1];
							//show(dt_with);
						)
					);
				)
			),
			pb2 = Panel Box( "4. With column to merge on", wtc = Col List Box() )
		)
	)
);

// stop the script if you hit x
If( win["Button"] == -1,
	Stop()
);

// other error catching here? 
// TBD as needed.

// run the generic fuzzymerge
JPL_ES:Fuzzymerge( Data Table( dt_main ), col_main, Data Table( dt_with ), col_with );
JPL_ES:Group Columns();