#include "ubl-settings-manager.h"

struct config_chooser {
    GtkWidget *RootBox;
    GtkWidget *PathEntry;
    GtkWidget *PathButton;
    GtkWidget *ClearButton;
};

void on_config_destroy(GtkWidget *, struct config_chooser *parameter);
void on_config_destroy(GtkWidget *, struct config_chooser *parameter){
    free(parameter);
}

void on_config_chooser_opened(GtkWidget *, struct config_chooser *parameter);
void on_config_chooser_opened(GtkWidget *, struct config_chooser *parameter){
    filechooser_window *window = yon_file_chooser_window_new(GTK_FILE_CHOOSER_ACTION_SAVE);
    yon_gtk_window_setup(GTK_WINDOW(window->Window),NULL,CHOOSE_FILE_LABEL,icon_path,"file_chooser");
    GtkFileFilter *ini_filter = gtk_file_filter_new();
    GtkFileFilter *yaml_filter = gtk_file_filter_new();
    GtkFileFilter *json_filter = gtk_file_filter_new();
    gtk_file_filter_add_pattern(ini_filter,"*.ini");
    gtk_file_filter_set_name(ini_filter,"*.ini");
    gtk_file_filter_add_pattern(yaml_filter,"*.yaml");
    gtk_file_filter_set_name(yaml_filter,"*.yaml");
    gtk_file_filter_add_pattern(json_filter,"*.json");
    gtk_file_filter_set_name(json_filter,"*.json");
    gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(window->MainFileChooser),ini_filter);
    gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(window->MainFileChooser),yaml_filter);
    gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(window->MainFileChooser),json_filter);
    if (yon_file_chooser_start(window) != GTK_RESPONSE_CANCEL){
        char *full_name = NULL;
        gtk_entry_set_text(GTK_ENTRY(parameter->PathEntry),window->last_success_selection);
        GtkFileFilter *cur_filter = gtk_file_chooser_get_filter(GTK_FILE_CHOOSER(window->MainFileChooser));
        
        if ((cur_filter == ini_filter&&!strstr(window->last_success_selection,".ini"))){
            full_name = yon_char_append(window->last_success_selection,".ini");
        } else if ((cur_filter == yaml_filter&&!strstr(window->last_success_selection,".yaml"))){
            full_name = yon_char_append(window->last_success_selection,".yaml");
        } else if ((cur_filter == json_filter&&!strstr(window->last_success_selection,".json"))){
            full_name = yon_char_append(window->last_success_selection,".json");
        } else {
            full_name = yon_char_new(window->last_success_selection);
        } 
        if (access(full_name,F_OK)){
            yon_launch(ubconfig_file_create(full_name));
        }
    }
}

void on_config_chooser_clear(GtkWidget *,struct config_chooser *parameter);
void on_config_chooser_clear(GtkWidget *,struct config_chooser *parameter){
    yon_window_config_erase_instant_parameter("file",yon_configuration_window_section);
    gtk_entry_set_text(GTK_ENTRY(parameter->PathEntry),"");
}

GtkWidget *yon_config_chooser_new( char *){
    struct config_chooser *parameter = malloc(sizeof(struct config_chooser));
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_path_config);

    parameter->RootBox = yon_gtk_builder_get_widget(builder,"RootBox");
    parameter->PathEntry = yon_gtk_builder_get_widget(builder,"PathEntry");
    parameter->PathButton = yon_gtk_builder_get_widget(builder,"PathButton");
    parameter->ClearButton = yon_gtk_builder_get_widget(builder,"ClearButton");

    g_object_set_data(G_OBJECT(parameter->RootBox),"data_struct",parameter);
    g_signal_connect(G_OBJECT(parameter->PathButton),"clicked",G_CALLBACK(on_config_chooser_opened),parameter);
    g_signal_connect(G_OBJECT(parameter->ClearButton),"clicked",G_CALLBACK(on_config_chooser_clear),parameter);
    g_signal_connect(G_OBJECT(parameter->RootBox),"destroy",G_CALLBACK(on_config_destroy),parameter);
    return parameter->RootBox;
}

void yon_config_chooser_update(GtkWidget *root){
    struct config_chooser *parameter = g_object_get_data(G_OBJECT(root),"data_struct");
    char *target;
    if (!yon_window_config_get_parameter(yon_configuration_window_section,"file",&target,YON_TYPE_STRING)){
        target = NULL;
    }
    if (target&&strcmp(target,"system")){
        gtk_entry_set_text(GTK_ENTRY(parameter->PathEntry),target);
    }
    
}

int yon_configuration_path_check(const char *path){
    if (!strcmp(path,".")||yon_char_is_empty(path)) { 
        return 0;
    }
    char *full_path = NULL;
    int homedir_create = 0;
    int pos = yon_char_find_last((char*)path,'/');
        if (pos>-1){
            char *path_copy = yon_char_new(path);
            char *dir_path = yon_char_divide(path_copy,pos);
            if (access(dir_path,F_OK)){
                homedir_create = 1;
            }
            free(path_copy);
            free(dir_path);
        } else {
                homedir_create = 1;

        }
        if (homedir_create){
            full_path = yon_char_unite(yon_ubl_user_get_home_directory(),"/",path,NULL);
        } else {
            full_path = yon_char_new(path);
        }
        {
            if (!strstr(full_path,".")){
                char *temp = yon_char_append(full_path,".ini");
                free(full_path);
                full_path = temp;
            }
        }
    if (access(full_path,F_OK)){
        dialog_confirmation_data *data = yon_confirmation_dialog_data_new();
        data->action_text = homedir_create?CREATE_CONFIG_DIALOG_LABEL_HOMEDIR:CREATE_CONFIG_DIALOG_LABEL;
        data->title = WARNING_TITLE_LABEL;
        if ((!yon_char_is_empty(main_config.force_ini)&&!strcmp(main_config.force_ini,path))||yon_confirmation_dialog_call(NULL,data)==GTK_RESPONSE_ACCEPT){
            char *dir_path = yon_file_get_parent(full_path);
            if (access(full_path,F_OK)&&(access(dir_path,W_OK)||access(dir_path,R_OK))){
                if (system(ubconfig_file_create_pkexec(full_path))){
                    return 0;
                }
            } else if (access(full_path,F_OK)){
                yon_launch(ubconfig_file_create(full_path));
                struct passwd *user = getpwnam(yon_ubl_root_user_get());
                if (chown(full_path,user->pw_uid,user->pw_gid)){};
            }
        } else {
            return 0;
        }
    }
    main_config.config_load_path = full_path;
    if (!yon_char_is_empty(main_config.config_save_path)) free(main_config.config_save_path);
    main_config.config_save_path = yon_char_new(full_path);
    return 1;
}


void yon_config_chooser_save(GtkWidget *root){
    struct config_chooser *parameter = g_object_get_data(G_OBJECT(root),"data_struct");
    char *path = (char*)gtk_entry_get_text(GTK_ENTRY(parameter->PathEntry));
    if (!yon_char_is_empty(path)){
        if (!yon_configuration_path_check(path)) return;
        path = main_config.config_load_path;
        yon_window_config_add_instant_parameter("file",yon_configuration_window_section,path,YON_TYPE_STRING);
        yon_ubl_status_box_despawn(GTK_CONTAINER(main_config.widgets->StatusBox));
        yon_ubl_status_box_spawn(GTK_CONTAINER(main_config.widgets->StatusBox),CONFIG_MODE_LABEL(path),0,BACKGROUND_IMAGE_INFO_TYPE);
    } else {
        yon_window_config_erase_instant_parameter("file",yon_configuration_window_section);
        yon_ubl_status_box_despawn(GTK_CONTAINER(main_config.widgets->StatusBox));
    }
}