#include "libublsettingsui-gtk3.h"

void *__interface_update_function = NULL;
void *__interface_update_data = NULL;
char *config_get_default_command = NULL;
char *config_get_global_command = NULL;
char *config_get_local_command = NULL;
char *config_get_custom_command = NULL;
char *config_get_global_only_command = NULL;
char *config_get_local_only_command = NULL;

char *config_set_global_local_command = NULL;
char *config_set_global_command = NULL;
char *config_set_local_command = NULL;
char *config_set_custom_command = NULL;
 
int __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);
    if (type==YON_CONFIG_GLOBAL){
		if (yon_config_load_config(type,config_get_global_command,NULL)){
			yon_ubl_status_box_render(GLOBAL_LOAD_SUCCESS_LABEL,BACKGROUND_IMAGE_SUCCESS_TYPE);
		}
	}
	else if (type==YON_CONFIG_LOCAL){
		if (yon_config_load_config(type,config_get_local_command,NULL)){
			yon_ubl_status_box_render(LOCAL_LOAD_SUCCESS_LABEL,BACKGROUND_IMAGE_SUCCESS_TYPE);
		}
    } else if (type==YON_CONFIG_CUSTOM){
		char *path;
        textdomain(template_ui_LocaleName);
		GtkWidget *dialog = gtk_file_chooser_dialog_new(template_app_information.app_title,NULL,GTK_FILE_CHOOSER_ACTION_SAVE,CANCEL_LABEL,GTK_RESPONSE_CANCEL,OPEN_LABEL,GTK_RESPONSE_ACCEPT,NULL);
        gtk_window_set_icon_name(GTK_WINDOW(dialog),yon_char_append("com.ublinux.",template_app_information.app_tech_name));
        gtk_window_set_title(GTK_WINDOW(dialog),template_app_information.app_title);
        GtkFileFilter *filter = gtk_file_filter_new();
		gtk_file_filter_add_pattern(filter,"*.ini");
        gtk_file_filter_set_name(filter,"*.ini");
		gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog),filter);
		gtk_widget_show(dialog);
		int response = gtk_dialog_run(GTK_DIALOG(dialog));
		if (response == GTK_RESPONSE_ACCEPT){
			char *file = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
			if (!yon_char_is_empty(file)){
				path=yon_char_unite("'",file,"'",NULL);
			}
            yon_config_clean();
			char *final_command = yon_config_parameter_prepare_command(config_get_custom_command,file,NULL,NULL);
            if (yon_config_load_config(type,final_command,NULL)){
				yon_ubl_status_box_render(LOCAL_LOAD_SUCCESS_LABEL,BACKGROUND_IMAGE_SUCCESS_TYPE);
			}
			gtk_widget_destroy(dialog);
		} else {
			gtk_widget_destroy(dialog);
			return 0;
		}
	}
    return 1;
}

void __on_config_local_load(GtkWidget *){
        __yon_load_proceed(YON_CONFIG_LOCAL);
        template_config->load_mode=1;
		if (__interface_update_function)
			((void (*)(void*))__interface_update_function)(__interface_update_data);
}

void __on_config_global_load(GtkWidget *){
        __yon_load_proceed(YON_CONFIG_GLOBAL);
        template_config->load_mode=0;
		if (__interface_update_function)
			((void (*)(void*))__interface_update_function)(__interface_update_data);
    
}

void __on_config_custom_load(GtkWidget *){
	__yon_load_proceed(YON_CONFIG_CUSTOM);
	template_config->load_mode=3;
		if (__interface_update_function)
			((void (*)(void*))__interface_update_function)(__interface_update_data);
}

void __on_config_global_local_save(){
    yon_save_proceed(NULL,YON_CONFIG_BOTH,config_get_global_command,NULL);
}

void __on_config_global_save(){
    yon_save_proceed("global",YON_CONFIG_GLOBAL,config_get_global_command,NULL);
}

void __on_config_local_save(){
    yon_save_proceed("system",YON_CONFIG_LOCAL,config_get_global_command,NULL);
}

void __on_config_custom_save(){
    yon_save_proceed(NULL,YON_CONFIG_CUSTOM,config_get_custom_command,NULL);
}

void yon_ubl_settings_set_interface_update_function(void*target_func, gpointer func_data){
	__interface_update_function = target_func;
	__interface_update_data = func_data;
}

void yon_ubl_settings_window_set_standard_callbacks(template_main_window *widgets, char *get_global_command, char *get_local_command,char *get_custom_command, char *get_default_command, char *get_global_only_command, char *get_local_only_command){
	if (!yon_char_is_empty(get_global_command)){
		g_signal_connect(G_OBJECT(widgets->LoadGlobalMenuItem),"activate",G_CALLBACK(__on_config_global_load),NULL);
		g_signal_connect(G_OBJECT(widgets->SaveGlobalMenuItem),"activate",G_CALLBACK(__on_config_global_save),NULL);
		config_get_global_command = yon_char_new(get_global_command);
		config_set_global_command = yon_char_replace_single(get_global_command,"get","set");
	}
	if (!yon_char_is_empty(get_local_command)){
		g_signal_connect(G_OBJECT(widgets->LoadLocalMenuItem),"activate",G_CALLBACK(__on_config_local_load),NULL);
		g_signal_connect(G_OBJECT(widgets->SaveLocalMenuItem),"activate",G_CALLBACK(__on_config_local_save),NULL);
		config_get_local_command = yon_char_new(get_local_command);
		config_set_local_command = yon_char_replace_single(get_local_command,"get","set");
	}
	if (!yon_char_is_empty(get_local_command)&&!yon_char_is_empty(get_global_command)){
		g_signal_connect(G_OBJECT(widgets->SaveMenuItem),"activate",G_CALLBACK(__on_config_global_local_save),NULL);
		config_set_global_local_command = yon_config_parameter_prepare_command(get_global_command,NULL,NULL,NULL);
	}
	if (!yon_char_is_empty(get_custom_command)){
		g_signal_connect(G_OBJECT(widgets->LoadCustomMenuItem),"activate",G_CALLBACK(__on_config_custom_load),NULL);
		g_signal_connect(G_OBJECT(widgets->SaveCustomMenuItem),"activate",G_CALLBACK(__on_config_custom_save),NULL);
		config_get_custom_command = yon_char_new(get_custom_command);
		config_set_custom_command = yon_char_replace_single(get_custom_command,"get","set");
	}
	if (!yon_char_is_empty(get_default_command)) config_get_default_command = yon_char_new(get_default_command);
	if (!yon_char_is_empty(get_global_only_command)) config_get_global_only_command = yon_char_new(get_global_only_command);
	if (!yon_char_is_empty(get_local_only_command)) config_get_local_only_command = yon_char_new(get_local_only_command);
}

int yon_ubl_settings_window_remove_standard_callbacks(template_main_window *window){

}