#include "ubinstall-gtk.h"


void on_srartup_services_toggled(GtkWidget *, char *path, main_window *widgets){
    GtkTreeIter iter;
    int status;
    gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(widgets->StartupList),&iter,path);
    gtk_tree_model_get(GTK_TREE_MODEL(widgets->StartupList),&iter,0,&status,-1);
    gtk_list_store_set(widgets->StartupList,&iter,0,!status,-1);
}

void on_startup_services_selection_changed(GtkWidget *,main_window *widgets){
    GtkTreeIter iter;
    GtkTreeModel *model;
    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->StartupServicesTree)),&model,&iter)){
        int status;
        gtk_tree_model_get(model,&iter,4,&status,-1);
        if (status){
            gtk_widget_set_sensitive(widgets->StartupServicesEditButton,1);
            gtk_widget_set_sensitive(widgets->StartupServicesRemoveButton,1);
            return;
        }
    }
    gtk_widget_set_sensitive(widgets->StartupServicesEditButton,0);
    gtk_widget_set_sensitive(widgets->StartupServicesRemoveButton,0);
}

void on_startup_services_remove(GtkWidget *self,main_window *widgets){
    GtkTreeIter iter;
    GtkTreeModel *model;
    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->StartupServicesTree)),&model,&iter)){
        char *unit;
        int deletable;
        gtk_tree_model_get(model,&iter,2,&unit,4,&deletable,-1);
        if (!deletable) return;
        dialog_confirmation_data *data = yon_confirmation_dialog_data_new();
        data->action_text = SERVICE_REMOVE_CONFIRMATION_LABEL(unit);
        data->title = SERVICE_REMOVE_TITLE_LABEL;
        if (yon_confirmation_dialog_call(self,data) == GTK_RESPONSE_ACCEPT){
            gtk_list_store_remove(widgets->StartupList,&iter);
        }
    }
}

void yon_startup_services_setup(main_window *widgets){
    GtkTreeIter iter;
    if (!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(widgets->StartupList),&iter)){
        int size;
        config_str services = yon_resource_open_file(services_list_path,&size);
        for (int i=1;i<size;i++){
            int parsed_size;
            config_str parsed = yon_char_parse(services[i],&parsed_size,";");
            if (parsed_size&&!yon_char_is_empty(parsed[0])){
                gtk_list_store_append(widgets->StartupList,&iter);
                gtk_list_store_set(widgets->StartupList,&iter,0,1,1,parsed[0],2,yon_char_return_if_exist(parsed[1],""),3,yon_char_return_if_exist(parsed[2],""),-1);
            }
        }
    }
    char *startup = config(SERVICES_ENABLE_parameter);
    if (!yon_char_is_empty(startup)){
        if (!strcmp(startup,"auto")){
            for_iter(GTK_TREE_MODEL(widgets->StartupList),&iter){
                gtk_list_store_set(widgets->StartupList,&iter,0,1,-1);
            }
        } else {
            int parsed_size;
            config_str parsed = yon_char_parse(startup,&parsed_size,",");
            for_iter(GTK_TREE_MODEL(widgets->StartupList),&iter){
                char *target;
                gtk_tree_model_get(GTK_TREE_MODEL(widgets->StartupList),&iter,2,&target,-1);
                if (strstr(target,",")){
                    int target_size;
                    config_str target_parsed = yon_char_parse(target,&target_size,",");
                    int found =0;
                    for (int i=0;i<target_size;i++){
                        int pos = yon_char_parsed_check_exist(parsed,parsed_size,target_parsed[i]);
                        if (pos>-1){
                            found++;
                            parsed = yon_char_parsed_rip(parsed,&parsed_size,pos);  
                        }
                    }
                    if (found == target_size){
                        gtk_list_store_set(widgets->StartupList,&iter,0,1,-1);
                    } else {
                        gtk_list_store_set(widgets->StartupList,&iter,0,0,-1);
                    }
                    yon_char_parsed_free(target_parsed,target_size);
                } else {
                    int pos = yon_char_parsed_check_exist(parsed,parsed_size,target);
                    if (pos>-1){
                        gtk_list_store_set(widgets->StartupList,&iter,0,1,-1);
                        parsed = yon_char_parsed_rip(parsed,&parsed_size,pos);
                    } else {
                        gtk_list_store_set(widgets->StartupList,&iter,0,0,-1);
                    }
                }
            }
            if (parsed_size&&parsed){
                for (int i=0;i<parsed_size;i++){
                    gtk_list_store_append(widgets->StartupList,&iter);
                    gtk_list_store_set(widgets->StartupList,&iter,0,1,2,parsed[i],-1);
                }
            }
            yon_char_parsed_free(parsed,parsed_size);
        }
    }
}

void on_startup_add_accept(GtkWidget *self, main_window *widgets){
    startup_service_window *window = g_object_get_data(G_OBJECT(self),"startup_service_window");
        const char *unit = gtk_entry_get_text(GTK_ENTRY(window->UnitEntry));
        const char *service = gtk_entry_get_text(GTK_ENTRY(window->ServiceEntry));
        const char *description = gtk_entry_get_text(GTK_ENTRY(window->DescriptionEntry));
        if (yon_char_is_empty(service)){
            yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),_EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
            yon_ubl_status_highlight_incorrect(window->ServiceEntry);
            return;
        }
        GtkTreeIter iter;
        gtk_list_store_append(widgets->StartupList,&iter);
        gtk_list_store_set(widgets->StartupList,&iter,0,1,1,unit,2,service,3,description,4,1,-1);
    on_subwindow_close(self);
}

void on_startup_edit_accept(GtkWidget *self, main_window *widgets){
    GtkTreeIter iter;
    GtkTreeModel *model;
    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->StartupServicesTree)),&model,&iter)){
        startup_service_window *window = g_object_get_data(G_OBJECT(self),"startup_service_window");
        const char *unit = gtk_entry_get_text(GTK_ENTRY(window->UnitEntry));
        const char *service = gtk_entry_get_text(GTK_ENTRY(window->ServiceEntry));
        const char *description = gtk_entry_get_text(GTK_ENTRY(window->DescriptionEntry));
        if (yon_char_is_empty(service)){
            yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),_EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
            yon_ubl_status_highlight_incorrect(window->ServiceEntry);
            return;
        }
        gtk_list_store_set(widgets->StartupList,&iter,0,1,1,unit,2,service,3,description,4,1,-1);
    }
    on_subwindow_close(self);
}

startup_service_window *yon_startup_service_window_new(){
    startup_service_window *window = new(startup_service_window);

    GtkBuilder *builder = gtk_builder_new_from_resource(glade_path_service);

    window->Window = yon_gtk_builder_get_widget(builder,"MainWindow");
    window->StatusBox = yon_gtk_builder_get_widget(builder,"StatusBox");
    window->AcceptButton = yon_gtk_builder_get_widget(builder,"AcceptButton");
    window->CancelButton = yon_gtk_builder_get_widget(builder,"CancelButton");
    window->UnitEntry = yon_gtk_builder_get_widget(builder,"UnitEntry");
    window->ServiceEntry = yon_gtk_builder_get_widget(builder,"ServiceEntry");
    window->DescriptionEntry = yon_gtk_builder_get_widget(builder,"DescriptionEntry");

    g_signal_connect(G_OBJECT(window->CancelButton),"clicked",G_CALLBACK(on_subwindow_close),NULL);
    g_object_set_data(G_OBJECT(window->AcceptButton),"startup_service_window",window);

    return window;
}

void on_startup_service_add(GtkWidget *, main_window *widgets){
    startup_service_window *window = yon_startup_service_window_new();
    yon_gtk_window_setup(GTK_WINDOW(window->Window),GTK_WINDOW(widgets->MainWindow),SERVICE_ADD_TITLE_LABEL,icon_path,"service_add_window");
    g_signal_connect(G_OBJECT(window->AcceptButton),"clicked",G_CALLBACK(on_startup_add_accept),widgets);
    gtk_widget_show(window->Window);
}

void on_startup_service_edit(GtkWidget *, main_window *widgets){
    GtkTreeIter iter;
    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->StartupServicesTree)),NULL,&iter)){
        char *service, *unit, *description;
        int status;
        gtk_tree_model_get(GTK_TREE_MODEL(widgets->StartupList),&iter,1,&unit,2,&service,3,&description,4,&status,-1);
        if (status){
            startup_service_window *window = yon_startup_service_window_new();
            yon_gtk_window_setup(GTK_WINDOW(window->Window),GTK_WINDOW(widgets->MainWindow),SERVICE_EDIT_TITLE_LABEL,icon_path,"service_add_window");
            g_signal_connect(G_OBJECT(window->AcceptButton),"clicked",G_CALLBACK(on_startup_edit_accept),widgets);
            gtk_entry_set_text(GTK_ENTRY(window->UnitEntry),unit);
            gtk_entry_set_text(GTK_ENTRY(window->ServiceEntry),service);
            gtk_entry_set_text(GTK_ENTRY(window->DescriptionEntry),description);
            gtk_widget_show(window->Window);
        }
    }
}

int yon_startup_save(main_window *widgets){
    GtkTreeIter iter;
    GtkTreeModel *model = GTK_TREE_MODEL(widgets->StartupList);
    int size = 0;
    int list_size=0;
    config_str modules = NULL;
    for_iter(model,&iter){
        list_size++;
        char *target;
        int status;
        gtk_tree_model_get(model,&iter,0,&status,2,&target,-1);
        if (status){
            yon_char_parsed_add_or_create_if_exists(modules,&size,target);
        }
    }
    if (size){
        if (list_size==size){
            yon_config_register(SERVICES_ENABLE_parameter,SERVICES_ENABLE_parameter_command,"auto");
        } else {
            char *final = yon_char_parsed_to_string(modules,size,",");
            yon_config_register(SERVICES_ENABLE_parameter,SERVICES_ENABLE_parameter_command,final);
        }
        yon_char_parsed_free(modules,size);
    } else {
        yon_config_remove_by_key(SERVICES_ENABLE_parameter);  
    }
    if (!main_config.configure_mode){
        int size;
        config_str parameters = yon_config_get_save_parameters_by_key(&size,SERVICES_ENABLE_parameter,NULL);
        if (parameters){
            char *command_parameters = yon_char_parsed_to_string(parameters,size," -- ");
            char *command = yon_char_unite(ubconfig_dull_command,"--target system ",command_parameters,NULL);
            if (!system(command)){}
            free(command);
            yon_char_parsed_free(parameters,size);
        }
    }
    return 1;
}