#include "ubinstall-gtk.h"

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

void on_startup_apps_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->StartupAppsTree)),&model,&iter)){
        int status;
        gtk_tree_model_get(model,&iter,4,&status,-1);
        if (status){
            gtk_widget_set_sensitive(widgets->StartupAppsEditButton,1);
            gtk_widget_set_sensitive(widgets->StartupAppsRemoveButton,1);
            return;
        }
    }
    gtk_widget_set_sensitive(widgets->StartupAppsEditButton,0);
    gtk_widget_set_sensitive(widgets->StartupAppsRemoveButton,0);
}

void on_startup_apps_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->StartupAppsTree)),&model,&iter)){
        char *unit;
        int deletable;
        gtk_tree_model_get(model,&iter,1,&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 = APPLICATIONS_REMOVE_TITLE_LABEL;
        if (yon_confirmation_dialog_call(self,data) == GTK_RESPONSE_ACCEPT){
            gtk_list_store_remove(GTK_LIST_STORE(model),&iter);
        }
    }
}

void yon_startup_apps_init(main_window *widgets){
    if (!main_config.configure_mode){
        yon_config_update_by_args(main_config.config_load_path, AUTOEXEC_parameter,NULL);
    }
    GtkTreeIter iter;
    gtk_list_store_clear(widgets->StartupAppsList);
        int size;
        config_str apps_ret = yon_resource_open_file(apps_list_path,&size);
        apps_ret = yon_char_parsed_rip(apps_ret,&size,0);
        if (!size) return;
        // qsort(apps_ret,(size_t)size,sizeof(char *),(__compar_fn_t)yon_char_parsed_compare);
        for (int i=0;i<size;i++){
            int parsed_size;
            config_str parsed = yon_char_parse(apps_ret[i],&parsed_size,";");
            if (parsed_size&&!yon_char_is_empty(parsed[0])){
                gtk_list_store_append(widgets->StartupAppsList,&iter);
                gtk_list_store_set(widgets->StartupAppsList,&iter,0,0,1,parsed[1],2,yon_char_return_if_exist(parsed[0],""),3,yon_char_return_if_exist(_(parsed[2]),""),-1);
            }
        }
    
    char *startup = config(AUTOEXEC_parameter);
    if (!yon_char_is_empty(startup)){
        if (!strcmp(startup,"auto")){
            for_iter(GTK_TREE_MODEL(widgets->StartupAppsList),&iter){
                gtk_list_store_set(widgets->StartupAppsList,&iter,0,1,-1);
            }
        } else {
            int parsed_size;
            config_str parsed = yon_char_parse(startup,&parsed_size,",");
            for_iter(GTK_TREE_MODEL(widgets->StartupAppsList),&iter){
                char *target;
                gtk_tree_model_get(GTK_TREE_MODEL(widgets->StartupAppsList),&iter,1,&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->StartupAppsList,&iter,0,1,-1);
                    } else {
                        gtk_list_store_set(widgets->StartupAppsList,&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->StartupAppsList,&iter,0,1,-1);
                        parsed = yon_char_parsed_rip(parsed,&parsed_size,pos);
                    } else {
                        gtk_list_store_set(widgets->StartupAppsList,&iter,0,0,-1);
                    }
                }
            }
            if (parsed_size&&parsed){
                for (int i=0;i<parsed_size;i++){
                    gtk_list_store_append(widgets->StartupAppsList,&iter);
                    char *app = NULL;
                    char *description = NULL;
                    yon_window_config_get_parameter("service_app",parsed[i],&app,YON_TYPE_STRING);
                    yon_window_config_get_parameter("service_desc",parsed[i],&description,YON_TYPE_STRING);
                    gtk_list_store_set(widgets->StartupAppsList,&iter,0,1,1,parsed[i],2,app,3,description,4,1,-1);
                }
            }
            yon_char_parsed_free(parsed,parsed_size);
        }
    }
}

void on_startup_app_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 *app = gtk_entry_get_text(GTK_ENTRY(window->ServiceEntry));
        const char *description = gtk_entry_get_text(GTK_ENTRY(window->DescriptionEntry));
        if (yon_char_is_empty(app)){
            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->StartupAppsList,&iter);
        gtk_list_store_set(widgets->StartupAppsList,&iter,0,1,1,app,2,unit,3,description,4,1,-1);
    on_subwindow_close(self);
}

void on_startup_app_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 *app = gtk_entry_get_text(GTK_ENTRY(window->ServiceEntry));
        const char *description = gtk_entry_get_text(GTK_ENTRY(window->DescriptionEntry));
        if (yon_char_is_empty(app)){
            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->StartupAppsList,&iter,0,1,1,unit,2,app,3,description,4,1,-1);
    }
    on_subwindow_close(self);
}

startup_service_window *yon_startup_app_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");
    window->AppLabel = yon_gtk_builder_get_widget(builder,"AppLabel");
    window->GroupLabel = yon_gtk_builder_get_widget(builder,"GroupLabel");
    window->DescriptionLabel = yon_gtk_builder_get_widget(builder,"DescriptionLabel");
    gtk_label_set_text(GTK_LABEL(window->AppLabel),APP_LABEL);
    gtk_label_set_text(GTK_LABEL(window->GroupLabel),GROUP_LABEL);

    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);
    yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),IMPORTANT_OUTLINE_LABEL,0,BACKGROUND_IMAGE_INFO_TYPE);
    return window;
}

void on_startup_app_add(GtkWidget *, main_window *widgets){
    startup_service_window *window = yon_startup_app_window_new();
    yon_gtk_window_setup(GTK_WINDOW(window->Window),GTK_WINDOW(widgets->MainWindow),APPLICATIONS_ADD_TITLE_LABEL,icon_path,"app_add_window");
    g_signal_connect(G_OBJECT(window->AcceptButton),"clicked",G_CALLBACK(on_startup_app_add_accept),widgets);
    gtk_widget_show(window->Window);
}

void on_startup_app_edit(GtkWidget *, main_window *widgets){
    GtkTreeIter iter;
    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->StartupAppsTree)),NULL,&iter)){
        char *app, *unit, *description;
        int status;
        gtk_tree_model_get(GTK_TREE_MODEL(widgets->StartupAppsList),&iter,1,&unit,2,&app,3,&description,4,&status,-1);
        if (status){
            startup_service_window *window = yon_startup_app_window_new();
            yon_gtk_window_setup(GTK_WINDOW(window->Window),GTK_WINDOW(widgets->MainWindow),APPLICATIONS_EDIT_TITLE_LABEL,icon_path,"app_add_window");
            g_signal_connect(G_OBJECT(window->AcceptButton),"clicked",G_CALLBACK(on_startup_app_edit_accept),widgets);
            gtk_entry_set_text(GTK_ENTRY(window->UnitEntry),app);
            gtk_entry_set_text(GTK_ENTRY(window->ServiceEntry),unit);
            gtk_entry_set_text(GTK_ENTRY(window->DescriptionEntry),description);
            gtk_widget_show(window->Window);
        }
    }
}

int yon_startup_app_save(main_window *widgets){
    GtkTreeIter iter;
    GtkTreeModel *model = GTK_TREE_MODEL(widgets->StartupAppsList);
    int size = 0;
    int list_size=0;
    config_str modules = NULL;
    for_iter(model,&iter){
        list_size++;
        char *target;
        int status;
        char *app, *description;
        gtk_tree_model_get(model,&iter,0,&status,1,&target,2,&app,3,&description,-1);
        if (status){
            yon_char_parsed_add_or_create_if_exists(modules,&size,target);
            yon_window_config_add_instant_parameter(target,"service_app",app,YON_TYPE_STRING);
            yon_window_config_add_instant_parameter(target,"service_desc",description,YON_TYPE_STRING);
        }
    }
    if (size){
            char *final = yon_char_parsed_to_string(modules,size,",");
            char *config_parameter = config(AUTOEXEC_parameter);
            if (!yon_char_check_elements(config_parameter,final,",")||!yon_char_check_elements(final,config_parameter,",")){
            yon_config_register(AUTOEXEC_parameter,AUTOEXEC_parameter_command,final);
            }
        yon_char_parsed_free(modules,size);
    } else {
        yon_config_remove_by_key(AUTOEXEC_parameter);  
    }
    if (!main_config.configure_mode){
        yon_debug_output("%s\n",yon_config_save_args_simple(main_config.config_load_path,AUTOEXEC_parameter,NULL));
    }
    return 1;
}