#include "ubl-settings-system.h"


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);
	textdomain(template_ui_LocaleName);
	switch (main_config.save_config){
		case 0:
			yon_ubl_status_box_render(GLOBAL_SAVE_SUCCESS_LABEL,BACKGROUND_IMAGE_SUCCESS_TYPE);
			break;
		case 1:
			yon_ubl_status_box_render(LOCAL_SAVE_SUCCESS_LABEL,BACKGROUND_IMAGE_SUCCESS_TYPE);
			break;
		case 2:
			yon_ubl_status_box_render(GLOBAL_LOCAL_SAVE_SUCCESS_LABEL,BACKGROUND_IMAGE_SUCCESS_TYPE);
			break;
		case 3:
			yon_ubl_status_box_render(SAVE_SUCCESS_LABEL,BACKGROUND_IMAGE_SUCCESS_TYPE);
			
	}
	textdomain(LocaleName);
	
}

void on_config_reload(GtkWidget *,main_window *widgets){
    if (main_config.load_mode==0){
        yon_config_global_load(NULL,widgets);
    } else if (main_config.load_mode==1){
        yon_config_local_load(NULL,widgets);
    }
    yon_interface_update(widgets);
}

void yon_load_proceed(YON_CONFIG_TYPE type){
    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){
		yon_config_load_config(type,config_get_command("global"),NULL);
	} else if (type==YON_CONFIG_LOCAL){
		yon_config_load_config(type,config_get_command("system"),NULL);
    } 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);
        textdomain(LocaleName);
        gtk_window_set_icon_name(GTK_WINDOW(dialog),"com.ublinux.ubl-settings-services");
        gtk_window_set_title(GTK_WINDOW(dialog),TITLE_LABEL);
        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=file;
			}
			gtk_widget_destroy(dialog);
		} else {
			gtk_widget_destroy(dialog);
		}
        char *command = config_get_command(path);
		yon_config_load_config(type,command,NULL);
	}
}

void yon_config_local_load(GtkWidget *, main_window *widgets){
    yon_load_proceed(YON_CONFIG_LOCAL);
	main_config.load_mode=YON_CONFIG_LOCAL;
    yon_interface_update(widgets);
}

void yon_config_global_load(GtkWidget *, main_window *widgets){
    yon_load_proceed(YON_CONFIG_GLOBAL);
    if (getuid()==0){
        textdomain(template_ui_LocaleName);
        yon_ubl_status_box_render(GLOBAL_LOAD_SUCCESS_LABEL,BACKGROUND_IMAGE_SUCCESS_TYPE);
        textdomain(LocaleName);
    }
	main_config.load_mode=0;
    yon_interface_update(widgets);
}

void yon_config_custom_load(GtkWidget *, main_window *widgets){
    yon_load_proceed(YON_CONFIG_CUSTOM);
	main_config.load_mode=YON_CONFIG_CUSTOM;
    yon_interface_update(widgets);
}

void yon_interface_save(main_window *widgets){
    if (gtk_switch_get_active(GTK_SWITCH(widgets->LocaleDefaultSwitch))){
        yon_config_remove_by_key(language_parameter);
        yon_config_remove_by_key(locale_parameter);
    }
}

void yon_config_global_local_save(GtkWidget *, main_window *widgets){
    yon_interface_save(widgets);
	main_config.save_config=2;
    int changed = yon_config_get_status(id_parameter)==1;
    template_saving_window *window = yon_save_proceed(NULL,YON_CONFIG_BOTH,config_get_command("global"),NULL);
    if (window){
        if (changed){
            yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),SAVE_WARNING_LABEL,0,BACKGROUND_IMAGE_SUCCESS_TYPE);
        }
    } 

}

void yon_config_local_save(GtkWidget *, main_window *widgets){
    yon_interface_save(widgets);
	main_config.save_config=1;
    int changed = yon_config_get_status(id_parameter)==1;
    template_saving_window *window = yon_save_proceed("system",YON_CONFIG_LOCAL,config_get_command("system"),NULL);
    if (window){
        if (changed){
            yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),SAVE_WARNING_LABEL,0,BACKGROUND_IMAGE_SUCCESS_TYPE);
        }
    } 

}

void yon_config_global_save(GtkWidget *, main_window *widgets){
    yon_interface_save(widgets);
	main_config.save_config=0;
    int changed = yon_config_get_status(id_parameter)==1;
    template_saving_window *window = yon_save_proceed("global",YON_CONFIG_GLOBAL,config_get_command("global"),NULL);
    if (window){
        if (changed){
            yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),SAVE_WARNING_LABEL,0,BACKGROUND_IMAGE_SUCCESS_TYPE);
        }
    }

}

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