#include "ubl-settings-keyboard.h"

void on_save_done(main_window *, config_str output, int size){
	char *final_output = yon_char_parsed_to_string(output,size,"");
	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;
	}
	
}

void yon_interface_save(main_window *widgets){
	if (gtk_switch_get_active(GTK_SWITCH(widgets->DefaultLayoutsSwitch))){
		yon_config_remove_by_key(keyboard_layout_parameter);
	} else if (yon_config_check_ignore(keyboard_layout_parameter)){
		GtkTreeModel *model = GTK_TREE_MODEL(widgets->LayoutsList);
		GtkTreeIter iter;
		int parameters_size;
		config_str parameters = NULL;
		for_iter(model,&iter){
			char *target;
			gtk_tree_model_get(model,&iter,0,&target,-1);
			yon_char_parsed_add_or_create_if_exists(parameters,&parameters_size,target);
		}
		char *final = yon_char_parsed_to_string(parameters,parameters_size,",");
		yon_config_register(keyboard_layout_parameter,keyboard_layout_parameter_command,final);
	}
}

void on_config_global_local_save(GtkWidget *, main_window *widgets){
	yon_interface_save(widgets);
    main_config.save_config=YON_CONFIG_BOTH;
    yon_save_proceed(NULL,YON_CONFIG_BOTH,config_get_command("global"),NULL);
}

void on_config_local_save(GtkWidget *, main_window *widgets){
	yon_interface_save(widgets);
    main_config.save_config=YON_CONFIG_LOCAL;
    yon_save_proceed("system",YON_CONFIG_LOCAL,config_get_command("system"),NULL);
    
}

void on_config_global_save(GtkWidget *, main_window *widgets){
	yon_interface_save(widgets);
    main_config.save_config=YON_CONFIG_GLOBAL;
    yon_save_proceed("global",YON_CONFIG_GLOBAL,config_get_command("global"),NULL);
    
}

void on_config_custom_save(GtkWidget *, main_window *widgets){
	yon_interface_save(widgets);
    main_config.save_config=YON_CONFIG_LOCAL;
	template_saving_window *window = yon_save_proceed(NULL,YON_CONFIG_CUSTOM,config_get_command("system"),NULL);
	if (window){};
}

void on_config_local_load(GtkWidget *,main_window *widgets){
        yon_load_proceed(YON_CONFIG_LOCAL);
	yon_ubl_status_box_render(yon_char_get_localised_from_lib(LOCAL_LOAD_SUCCESS_LABEL),BACKGROUND_IMAGE_SUCCESS_TYPE);
        yon_interface_update(widgets);
        main_config.load_mode=1;
}

void on_config_global_load(GtkWidget *,main_window *widgets){
        yon_load_proceed(YON_CONFIG_GLOBAL);
	yon_ubl_status_box_render(yon_char_get_localised_from_lib(GLOBAL_LOAD_SUCCESS_LABEL),BACKGROUND_IMAGE_SUCCESS_TYPE);
        yon_interface_update(widgets);
        main_config.load_mode=0;
    
}

void on_config_custom_load(GtkWidget *,main_window *widgets){
	yon_load_proceed(YON_CONFIG_CUSTOM);
	textdomain(template_ui_LocaleName);
	yon_ubl_status_box_render(yon_char_get_localised_from_lib(LOCAL_LOAD_SUCCESS_LABEL),BACKGROUND_IMAGE_SUCCESS_TYPE);
	textdomain(LocaleName);
	main_config.load_mode=3;
        yon_interface_update(widgets);
}

void yon_load_proceed(YON_CONFIG_TYPE type){
	if (type!=YON_CONFIG_CUSTOM){
		yon_config_clean();
	}
    if (!yon_char_is_empty(config_get_default_command))
        yon_config_load_config(YON_CONFIG_DEFAULT,config_get_default_command,NULL);
    switch (type){
        case YON_CONFIG_GLOBAL:
            yon_config_load_config(type,config_get_command("global"),NULL);
        yon_ubl_status_box_render(yon_char_get_localised_from_lib(GLOBAL_LOAD_SUCCESS_LABEL),BACKGROUND_IMAGE_SUCCESS_TYPE);
            break;
        case YON_CONFIG_LOCAL:
            yon_config_load_config(type,config_get_command("system"),NULL);
        yon_ubl_status_box_render(yon_char_get_localised_from_lib(LOCAL_LOAD_SUCCESS_LABEL),BACKGROUND_IMAGE_SUCCESS_TYPE);
            break;
        case YON_CONFIG_CUSTOM:
            char *path = NULL;
            path=yon_custom_config_init(GTK_FILE_CHOOSER_ACTION_OPEN);
            if (!yon_char_is_empty(path)){
                yon_config_clean();
                yon_config_load_config(type,config_get_command(path),NULL);
                yon_ubl_status_box_render(yon_char_get_localised_from_lib(LOCAL_LOAD_SUCCESS_LABEL),BACKGROUND_IMAGE_SUCCESS_TYPE);
            }
            break;
        default:
            break;
    }
}