#include "ubinstall-gtk.h"

int yon_configuration_path_check(const char *path){
    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 configuration_mode_accept(GtkWidget *,configuration_window *window){
    const char *path = gtk_entry_get_text(GTK_ENTRY(window->PathEntry));
    if (yon_char_is_empty(path)){
        yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),_EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
        yon_ubl_status_highlight_incorrect(window->PathEntry);
        return;
    }
    main_window *widgets = g_object_get_data(G_OBJECT(window->Window),"widgets");
    if (yon_configuration_path_check(path)){
        gtk_widget_destroy(window->Window);
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->ConfigurationModeCheck),1);
    } else {
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->ConfigurationModeCheck),0);
    }
}

void on_path_choose(GtkWidget *,configuration_window *window){
    filechooser_window *dialog = yon_config_file_chooser_window_new(GTK_FILE_CHOOSER_ACTION_SAVE);
        yon_gtk_window_setup(GTK_WINDOW(dialog->Window),GTK_WINDOW(window->Window),CONFIGURATION_MODE_TITLE_LABEL,icon_path,"filechooser_window");
    if (yon_file_chooser_start(dialog)==GTK_RESPONSE_APPLY){
        char *path = dialog->last_success_selection;
        if (!strstr(dialog->last_success_selection,".")){
            path = yon_char_append(dialog->last_any_selection,".ini");
        }
        gtk_entry_set_text(GTK_ENTRY(window->PathEntry),path);
        free(dialog);
    }
}

void on_configuration_exit(GtkWidget *,configuration_window *window){
    main_window *widgets = g_object_get_data(G_OBJECT(window->Window),"widgets");
    
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->ConfigurationModeCheck),0);
    gtk_widget_destroy(window->Window);
    if (getuid()){
        gtk_main_quit();
    }
}

void yon_config_mode_start(main_window *widgets){
    if (main_config.config_save_path){
        yon_ubl_status_box_spawn(GTK_CONTAINER(widgets->StatusBox),CONFIGURATION_MODE_STATUS_LABEL,0,BACKGROUND_IMAGE_INFO_TYPE);
        yon_ubl_status_box_spawn(GTK_CONTAINER(widgets->StatusBox2),CONFIG_PATH_LABEL(main_config.config_save_path),0,BACKGROUND_IMAGE_INFO_TYPE);
        GList *revealerlist = gtk_container_get_children(GTK_CONTAINER(widgets->StatusBox2));
        GList *list = gtk_container_get_children(GTK_CONTAINER(g_list_nth_data(revealerlist,0)));
        GtkWidget *box = GTK_WIDGET(list->data);

        g_list_free(revealerlist);
        g_list_free(list);

        gtk_style_context_remove_class(gtk_widget_get_style_context(box),"boxInfoMessOK");
        gtk_style_context_add_class(gtk_widget_get_style_context(box),"boxInfoMessGray");
        main_config.configure_mode = 1;
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->ConfigurationModeCheck),1);
    }
}

void on_configuration_mode_switch(GtkWidget *,main_window *widgets){
    int active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets->ConfigurationModeCheck));
    if (!active){
        GtkBuilder *builder = gtk_builder_new_from_resource(glade_path_configuration_mode);
        configuration_window *window = malloc(sizeof(configuration_window));
        window->Window = yon_gtk_builder_get_widget(builder,"MainWindow");
        window->StatusBox = yon_gtk_builder_get_widget(builder,"StatusBox");
        window->PathEntry = yon_gtk_builder_get_widget(builder,"PathEntry");
        window->PathButton = yon_gtk_builder_get_widget(builder,"PathButton");
        window->CancelButton = yon_gtk_builder_get_widget(builder,"CancelButton");
        window->ChooseButton = yon_gtk_builder_get_widget(builder,"ChooseButton");
        
        g_signal_connect(G_OBJECT(window->CancelButton),"clicked",G_CALLBACK(on_configuration_exit),window);
        g_signal_connect(G_OBJECT(window->Window),"destroy",G_CALLBACK(gtk_main_quit),NULL);
        g_signal_connect(G_OBJECT(window->ChooseButton),"clicked",G_CALLBACK(configuration_mode_accept),window);
        g_object_set_data(G_OBJECT(window->Window),"widgets",widgets);
        g_signal_connect(G_OBJECT(window->PathButton),"clicked",G_CALLBACK(on_path_choose),window);
        g_object_set_data(G_OBJECT(window->Window),"widgets",widgets);
        yon_gtk_window_setup(GTK_WINDOW(window->Window),GTK_WINDOW(widgets->MainWindow),CONFIGURATION_MODE_TITLE_LABEL,icon_path,"configuration_window");
        if (getuid()){
            yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),ROOT_FORCE_CONFIGURATION_MODE_LABEL,0,BACKGROUND_IMAGE_INFO_TYPE);
        }

        if (!yon_char_is_empty(main_config.config_save_path)&&strcmp(main_config.config_save_path,"system")) {
            gtk_entry_set_text(GTK_ENTRY(window->PathEntry),main_config.config_save_path);
        }
            free(main_config.config_save_path);
            main_config.config_save_path = NULL;
        gtk_widget_show(window->Window);
        gtk_main();
        yon_config_mode_start(widgets);
        gtk_widget_destroy(window->Window);

    } else {
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->ConfigurationModeCheck),0);
        yon_ubl_status_box_despawn(GTK_CONTAINER(widgets->StatusBox));
        yon_ubl_status_box_despawn(GTK_CONTAINER(widgets->StatusBox2));
        main_config.configure_mode = 0;

    }
}
