#include "ubl-settings-fstecadvised.h"

config main_config;

//functions

void on_save_done(main_window *, config_str output, int size){
	char *final_output = yon_char_parsed_to_string(output,size,"\n");
	if (final_output){
		printf("%s\n",final_output);
		free(final_output);
	}
	yon_char_parsed_free(output,size);
	switch (main_config.save_config){
		case YON_CONFIG_GLOBAL:
			yon_ubl_status_box_render(yon_char_get_localised_from_lib(GLOBAL_SAVE_SUCCESS_LABEL),BACKGROUND_IMAGE_SUCCESS_TYPE);
			break;
		case YON_CONFIG_LOCAL:
			yon_ubl_status_box_render(yon_char_get_localised_from_lib(LOCAL_SAVE_SUCCESS_LABEL),BACKGROUND_IMAGE_SUCCESS_TYPE);
			break;
		case YON_CONFIG_BOTH:
			yon_ubl_status_box_render(yon_char_get_localised_from_lib(GLOBAL_LOCAL_SAVE_SUCCESS_LABEL),BACKGROUND_IMAGE_SUCCESS_TYPE);
			break;
	}
	
}
gboolean yon_interface_update(main_window *widgets){
    g_signal_handlers_block_by_func(G_OBJECT(widgets->ConfigureSystemSwitch),G_CALLBACK(on_configure_fstec_toggled),widgets);
    char *fstec = config(FSTEC_parameter);
    gtk_switch_set_active(GTK_SWITCH(widgets->ConfigureSystemSwitch),0);
    if (!yon_char_is_empty(fstec)&&!strcmp(fstec,"enable")){
        gtk_switch_set_active(GTK_SWITCH(widgets->ConfigureSystemSwitch),1);
    }
    g_signal_handlers_unblock_by_func(G_OBJECT(widgets->ConfigureSystemSwitch),G_CALLBACK(on_configure_fstec_toggled),widgets);
    return G_SOURCE_REMOVE;
}
// standard functions


gboolean on_configure_fstec_toggled(GtkWidget *, int, main_window *widgets){
    int status = gtk_switch_get_active(GTK_SWITCH(widgets->ConfigureSystemSwitch));
    if (status) {
        yon_config_register(FSTEC_parameter,FSTEC_parameter_command,"enable");
    } else {
        yon_config_remove_by_key(FSTEC_parameter);
    }
    return 0;
}

/**yon_main_window_complete(main_window *widgets) 
 * [EN] 
 *  
 * [RU] 
 * Функция настройки основного окна приложения. [widgets] - структура со стандартным интерфейсом. 
*/ 
main_window *yon_main_window_complete(main_window *widgets){ 
    widgets = yon_remalloc(widgets,sizeof(main_window)); 
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_path); 
    gtk_box_pack_start(GTK_BOX(widgets->InterfaceBox),yon_gtk_builder_get_widget(builder,"BoxMain"),1,1,0); 
    // Custom widgets configuration 
    {
        widgets->TestStartButton = yon_gtk_builder_get_widget(builder,"TestStartButton");
        widgets->TestListbox = yon_gtk_builder_get_widget(builder,"TestListbox");
        widgets->ConfigureSystemSwitch = yon_gtk_builder_get_widget(builder,"ConfigureSystemSwitch");
        widgets->WarningLabel = yon_gtk_builder_get_widget(builder,"WarningLabel");
        widgets->LoadingOverlay = yon_gtk_builder_get_widget(builder,"LoadingOverlay");
        widgets->LoadingSpinner = yon_gtk_builder_get_widget(builder,"LoadingSpinner");
        g_signal_connect(G_OBJECT(widgets->TestStartButton),"clicked",G_CALLBACK(on_test_start),widgets);
        g_signal_connect(G_OBJECT(widgets->ConfigureSystemSwitch),"state-set",G_CALLBACK(on_configure_fstec_toggled),widgets);

    }
    gtk_overlay_add_overlay(GTK_OVERLAY(widgets->LoadingOverlay),widgets->LoadingSpinner);
    gtk_overlay_set_overlay_pass_through(GTK_OVERLAY(widgets->LoadingOverlay),widgets->LoadingSpinner,1);
    gtk_widget_hide(widgets->LoadingSpinner);
    yon_load_proceed(YON_CONFIG_LOCAL);
    yon_interface_update(widgets);
    yon_save_window_set_postsave_function(on_save_done,widgets);
    yon_ubl_settings_set_interface_update_function(yon_interface_update,widgets);
    yon_startup_tests_list(widgets);
    return widgets;
}

int main(int argc, char *argv[]){ 
    setlocale(LC_ALL, "");
    textdomain (LocaleName);
    yon_ubl_connect_config((_template_config*)&main_config); 
    yon_ubl_window_init(TITLE_LABEL,TITLE_INFO_LABEL,LocaleName,CssPath,LocaleName,version_application,WIKI_LINK);
	config_str unfound = NULL;
	int size=0;
	yon_ubl_setup_arguments(argc,argv,&unfound,&size,NULL);
    gtk_init(&argc,&argv);
    template_main_window *widgets = yon_ubl_window_setup(); 
    yon_window_remove_exit_config_check(widgets);
    yon_root_button_setup((template_main_window*)widgets,argv,argc);
    yon_ubl_settings_window_set_standard_callbacks(widgets,config_get_command("global"),config_get_command("system"),config_get_command("./"),config_get_default_command,config_get_global_only_parameters,config_get_local_only_parameters);
    yon_main_window_complete((main_window*)widgets);
    char *path = yon_char_unite(yon_ubl_user_get_home_directory(),"/.config/",LocaleName,"/",LocaleName,".conf",NULL);
    yon_window_config_load(path);
    main_config.launch_arguments=yon_char_parsed_copy(argv,argc);
    main_config.launch_size=argc;
    if (getuid()!=0){
        textdomain(template_ui_LocaleName);
        yon_ubl_status_box_render(ROOT_WARNING_LABEL,BACKGROUND_IMAGE_FAIL_TYPE);
        textdomain(LocaleName);
    }
    gtk_main();
}