/*
	Copyright (C) 2024 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 "be.h"



int CommandLine::parse (
	int			const	argc,
	char	const * const *	const	argv
	)
{
	mtKit::Arg args ( [this]( char const * const filename )
		{
			m_cline_files.push_back ( filename );

			return 0; 	// Continue parsing
		} );

	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 ( "prefs",	prefs_filename );

	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
		, 1 );
	m_userprefs.add_int ( PREFS_AUDIO_OUTPUT_DEVICE, m_memprefs.audio_output_device,
		-1, -1, 1000 );

	m_userprefs.add_int ( PREFS_FILE_RECENT_MAXLEN, m_memprefs.file_recent_maxlen,
		PREFS_RECENT_MAXLEN_DEFAULT, PREFS_RECENT_MAXLEN_MIN,
		PREFS_RECENT_MAXLEN_MAX );

	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.set_invisible ( PREFS_AUDIO_OUTPUT_DEVICE );

	m_userprefs.add_ui_defaults ( m_memprefs.ui_editor );

	m_recent_files.init ( m_userprefs, PREFS_FILE_RECENT_FILENAME,
		RECENT_MENU_TOTAL );

	m_userprefs.load ( filename, BIN_NAME );
}

