#include "ubl-settings-update.h"

template_saving_window *save_window = NULL;

void on_save_done(main_window *, config_str output, int size){
    main_config.remove_repos=0;
	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 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;
	}
    // yon_config_set_ignore(REPOSITORY_parameter);
	
}

void on_save_start(main_window *, char* path){
    if (main_config.remove_repos){
        if (save_window&&GTK_IS_WINDOW(save_window->Window)){
            GtkTreeIter iter;
            for_iter(GTK_TREE_MODEL(save_window->list),&iter){
                char *target;
                int status;
                gtk_tree_model_get(GTK_TREE_MODEL(save_window->list),&iter,0,&status,1,&target,-1);
                if (strstr(target,REPOSITORY_search)&&status){
                    if (system(yon_debug_output("%s\n",config_presave_clean_command(path)))){};
                    break;
                }
            }
        }
        // int size;
        // yon_config_get_save_parameters_by_key(&size,)
    }
    save_window=NULL;
}

void yon_check_repositories_order(const char *path){
    int size = 0;
    char *command = config_get_repos_command(path);
    config_str config_repos = yon_config_load(command,&size);
    for (int i=0;i<size;i++){
        char *cur_param = yon_char_divide_search(config_repos[i],"=",-1);
        free(config_repos[i]);
        config_repos[i] = cur_param;
    }

    
    int local_size = 0;
    config_str local_repos = yon_config_get_all_by_key(REPOSITORY_search,&local_size);
    for (int i=0;i<local_size;i++){
        char *cur_param = yon_char_divide_search(local_repos[i],"=",-1);
        free(local_repos[i]);
        local_repos[i] = cur_param;
    }

    gboolean modified = 0;
    if (size!=local_size){
        modified = 1;
    } else {
        for (int i=0;i<size;i++){
            if (strcmp(config_repos[i],local_repos[i])){
                modified = 1;
            }
        }

    }
    if (modified){
        for (int i=0;i<size;i++){
            yon_config_compare_ignore_set(config_repos[i],1);
            main_config.remove_repos=1;
        }
    } else {
        for (int i=0;i<size;i++){
            yon_config_compare_ignore_set(config_repos[i],0);
            main_config.remove_repos=0;
        }
    }
}

void on_config_save(GtkWidget *, main_window *){
    yon_check_repositories_order(NULL);
    main_config.save_config=YON_CONFIG_BOTH;
    save_window = yon_save_proceed(NULL,YON_CONFIG_BOTH,config_get_command("global"),NULL);
}

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

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

void on_config_custom_save(GtkWidget *, main_window *){
    yon_check_repositories_order(NULL);
    main_config.save_config=YON_CONFIG_LOCAL;
	save_window = yon_save_proceed(NULL,YON_CONFIG_CUSTOM,config_get_command("system"),NULL);
}


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

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

void on_config_custom_load(GtkWidget *,main_window *widgets){
	yon_load_proceed(YON_CONFIG_CUSTOM);
	yon_ubl_status_box_render(LOCAL_LOAD_SUCCESS_LABEL,BACKGROUND_IMAGE_SUCCESS_TYPE);
	main_config.load_mode=YON_CONFIG_CUSTOM;
    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);
	}
    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="";
		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),"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;
			}
            yon_config_clean();
            if (!yon_char_is_empty(config_get_default_command))
                yon_config_load_config(YON_CONFIG_DEFAULT,config_get_default_command,NULL);
            char *command = config_get_command(path);
            yon_config_load_config(type,command,NULL);
			gtk_widget_destroy(dialog);
		} else {
			gtk_widget_destroy(dialog);
		}
	}
    yon_config_set_ignore(AUTOUPDATE_timestamp);
}