/*
	Copyright (C) 2021-2025 Mark Tyler

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program in the file COPYING.
*/

#include "lanter_be.h"



int CommandLine::parse (
	int			const	argc,
	char	const * const *	const	argv
	)
{
	mtKit::Arg args;

	auto print_version = []()
	{
		printf ( BIN_NAME " - Part of %s\n\n", VERSION );
		return 1;		// Stop parsing
	};

	auto print_help = [print_version]()
	{
		print_version ();
		printf ("For further information consult the man page "
			"%s(1) or the mtCellEdit Handbook.\n"
			"\n"
			, BIN_NAME );
		return 1;		// Stop parsing
	};

	int	stop = 0;
	char	const *	prefs_filename = nullptr;

	args.add ( "-help",	stop, 1, print_help );
	args.add ( "-version",	stop, 1, print_version );
	args.add ( "animate",	m_anim_tsv );
	args.add ( "dir",	m_anim_dir );
	args.add ( "fps",	m_anim_fps );
	args.add ( "oh",	m_output_height );
	args.add ( "ow",	m_output_width );
	args.add ( "prefs",	prefs_filename );
	args.add ( "savemap",	m_save_map );
	args.add ( "seed",	m_map_seed );
	args.add ( "size",	m_map_size );
	args.add ( "v",		m_verbose, 1 );

	if ( args.parse ( argc, argv ) || stop )
	{
		return 1;		// Quit program
	}

	prefs_init ( prefs_filename );

	return 0;			// Continue program
}

void CommandLine::prefs_init ( char const * const filename )
{
	m_userprefs.add_int ( PREFS_WINDOW_X, m_memprefs.window_x, 50 );
	m_userprefs.add_int ( PREFS_WINDOW_Y, m_memprefs.window_y, 50 );
	m_userprefs.add_int ( PREFS_WINDOW_W, m_memprefs.window_w, 500 );
	m_userprefs.add_int ( PREFS_WINDOW_H, m_memprefs.window_h, 500 );
	m_userprefs.add_int ( PREFS_WINDOW_MAXIMIZED,
		m_memprefs.window_maximized, 0 );

	m_userprefs.set_invisible ( PREFS_WINDOW_X );
	m_userprefs.set_invisible ( PREFS_WINDOW_Y );
	m_userprefs.set_invisible ( PREFS_WINDOW_W );
	m_userprefs.set_invisible ( PREFS_WINDOW_H );
	m_userprefs.set_invisible ( PREFS_WINDOW_MAXIMIZED );

	m_userprefs.add_ui_defaults ( m_memprefs.ui_editor );

	m_userprefs.load ( filename, BIN_NAME );
}

