#include "ubl-settings-update.h"


void on_ftp_accept(GtkWidget *, ftp_window *window){
    main_window *widgets = g_object_get_data(G_OBJECT(window->AcceptButton),"main_window");
    GtkTreeIter iter;
    const char *storage_path = gtk_entry_get_text(GTK_ENTRY(window->StorageEntry));
    if (yon_char_is_empty(storage_path)){
        yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
        yon_ubl_status_highlight_incorrect(window->StorageEntry);
        return;
    }
    char *port = NULL;
    gboolean enable = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->EnableCheck));
    const char *user = NULL;
    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->PortCheck))){
        port = yon_char_from_long(gtk_spin_button_get_value(GTK_SPIN_BUTTON(window->PortSpin)));
    }
    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->AnonUserCheck))){
        user = gtk_entry_get_text(GTK_ENTRY(window->AnonUserEntry));
        if (yon_char_is_empty(user)){
            yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),_EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
            yon_ubl_status_highlight_incorrect(window->AnonUserEntry);
        return;
        }
    }
    char *repos = NULL;
    for_iter(window->ReposList,&iter){
        int active;
        char *target;
        gtk_tree_model_get(GTK_TREE_MODEL(window->ReposList),&iter,0,&active,1,&target,-1);
        if (active){
            char *temp = yon_char_append_element(repos,target,",");
            if (!yon_char_is_empty(repos)) free(repos);
            repos = temp;
        }
    }
    if (yon_char_is_empty(repos)){
        yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),NOTHING_CHOSEN_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
        yon_ubl_status_highlight_incorrect(gtk_widget_get_parent(window->ReposTree));
        return;
    }

    char *id_string = yon_char_unite(storage_path,":",repos,NULL);
    int size;
    config_str parameters = yon_char_parsed_new(&size,enable? "yes":"no",
            yon_char_return_if_exist(port,""),
            yon_char_return_if_exist(user,""),
            NULL);
    char *parameter_string = yon_char_parsed_to_string_full(parameters,size,":");
    
    char *parameter_name = REPOPUBLIC_FTP(id_string);
    char *parameter_command = REPOPUBLIC_FTP_command(id_string);
    if (!yon_char_is_empty(window->prev_key)&&strcmp(id_string,window->prev_key)){
        char *old_parameter = REPOPUBLIC_FTP(window->prev_key);
        yon_config_remove_by_key(old_parameter);
        free(old_parameter);
    }
    yon_config_register(parameter_name,parameter_command,parameter_string);
    free(window->prev_key);
    free(id_string);
    free(parameter_string);
    free(parameter_name);
    free(parameter_command);
    if (!yon_char_is_empty(port)) free(port);
    if (!yon_char_is_empty(repos)) free(repos);
    on_subwindow_close(window->Window);
    free(window);
    yon_interface_update(widgets);
}

void on_anon_user_select(GtkWidget *, ftp_window *window){
    unsigned int size;
    config_str users = yon_users_window_select(YON_USER_WINDOW_USERS,&size);
    if (size){
        char *users_string = yon_char_parsed_to_string(users,size," ");
        gtk_entry_set_text(GTK_ENTRY(window->AnonUserEntry),users_string);
        free(users_string);
        yon_char_parsed_free(users,size);
    }
}

ftp_window *yon_ftp_window_new(){
    ftp_window *window = malloc(sizeof(ftp_window));
    memset(window,0,sizeof(ftp_window));

    GtkBuilder *builder = gtk_builder_new_from_resource(glade_path_ftp);

    window->Window = yon_gtk_builder_get_widget(builder,"Window");
    window->StatusBox = yon_gtk_builder_get_widget(builder,"StatusBox");
    window->EnableCheck = yon_gtk_builder_get_widget(builder,"EnableCheck");
    window->StorageEntry = yon_gtk_builder_get_widget(builder,"StorageEntry");
    window->StorageButton = yon_gtk_builder_get_widget(builder,"StorageButton");
    window->ReposTree = yon_gtk_builder_get_widget(builder,"ReposTree");
    window->PortCheck = yon_gtk_builder_get_widget(builder,"PortCheck");
    window->PortSpin = yon_gtk_builder_get_widget(builder,"PortSpin");
    window->AnonUserCheck = yon_gtk_builder_get_widget(builder,"AnonUserCheck");
    window->AnonUserEntry = yon_gtk_builder_get_widget(builder,"AnonUserEntry");
    window->AnonUserButton = yon_gtk_builder_get_widget(builder,"AnonUserButton");
    window->AcceptButton = yon_gtk_builder_get_widget(builder,"AcceptButton");
    window->CancelButton = yon_gtk_builder_get_widget(builder,"CancelButton");
    window->ReposList = GTK_LIST_STORE(gtk_builder_get_object(builder,"ReposList"));
    window->SelectionCell = GTK_CELL_RENDERER(gtk_builder_get_object(builder,"SelectionCell"));

    g_signal_connect(G_OBJECT(window->SelectionCell),"toggled",G_CALLBACK(on_cell_renderer_toggle_toggled),window->ReposTree);
    g_signal_connect(G_OBJECT(window->StorageButton),"clicked",G_CALLBACK(on_file_chooser_open),window->StorageEntry);
    g_signal_connect(G_OBJECT(window->StorageEntry),"changed",G_CALLBACK(on_web_publish_path_changed),window);
    g_signal_connect(G_OBJECT(window->PortCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button),window->PortSpin);
    g_signal_connect(G_OBJECT(window->AnonUserCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button),window->AnonUserEntry);
    g_signal_connect(G_OBJECT(window->AnonUserCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button),window->AnonUserButton);
    g_signal_connect(G_OBJECT(window->AnonUserButton),"clicked",G_CALLBACK(on_anon_user_select),window);

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

    return window;
}

void yon_ftp_window_add(GtkWidget *, main_window *widgets){
    ftp_window *window = yon_ftp_window_new();
    g_object_set_data(G_OBJECT(window->AcceptButton),"main_window",widgets);
    yon_gtk_window_setup(GTK_WINDOW(window->Window),GTK_WINDOW(widgets->Window),ADD_WEB_LABEL,icon_path,"ftp_window");

}

void yon_ftp_window_edit(GtkWidget *, main_window *widgets){
    GtkTreeModel *model;
    GtkTreeIter iter;
    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->FtpTree)),&model,&iter)){
        ftp_window *window = yon_ftp_window_new();
        g_object_set_data(G_OBJECT(window->AcceptButton),"main_window",widgets);
        yon_gtk_window_setup(GTK_WINDOW(window->Window),GTK_WINDOW(widgets->Window),EDIT_WEB_LABEL,icon_path,"ftp_window");
        char *storage;
        char *repos;
        gtk_tree_model_get(model,&iter,1,&storage,2,&repos,-1);
        char *repos_replaced = yon_char_replace(repos,"\n",",");
        char *parameter_key = yon_char_unite(storage,":",repos_replaced,NULL);
        window->prev_key = yon_char_new(parameter_key);
        char *parameter_name = REPOPUBLIC_FTP(parameter_key);
        char *parameter = config(parameter_name);
        if (parameter){
            int size;
            config_str parsed = yon_char_parse(parameter,&size,":");
            if (size>0&&!yon_char_is_empty(parsed[0])){
                if (!strcmp(parsed[0],"enabled")||!strcmp(parsed[0],"yes")){
                    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->EnableCheck),1);
                }
            }
            if (size>1&&!yon_char_is_empty(parsed[1])){
                gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->PortCheck),1);
                gtk_adjustment_set_value(gtk_spin_button_get_adjustment(GTK_SPIN_BUTTON(window->PortSpin)),atol(parsed[1]));
            }
            if (size>2&&!yon_char_is_empty(parsed[2])){
                gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->AnonUserCheck),1);
                gtk_entry_set_text(GTK_ENTRY(window->AnonUserEntry),parsed[2]);
            }
        }
        gtk_entry_set_text(GTK_ENTRY(window->StorageEntry),storage);
        for_iter (window->ReposList,&iter){
            char *target;
            gtk_tree_model_get(GTK_TREE_MODEL(window->ReposList),&iter,1,&target,-1);
            if (yon_char_check_element(repos_replaced,target,",")){
                gtk_list_store_set(window->ReposList,&iter,0,1,-1);
            }
        }
    }

}

void on_ftp_remove(GtkWidget *,main_window *widgets){
    GtkTreeModel *model;
    GtkTreeIter iter;
    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->FtpTree)),&model,&iter)){
        dialog_confirmation_data *data = yon_confirmation_dialog_data_new();
        data->title = REMOVE_FTP_TITLE_LABEL;
        data->action_text = REMOVE_REPO_WARNING_LABEL;
        if (yon_confirmation_dialog_call(widgets->Window,data)==GTK_RESPONSE_ACCEPT){
            char *storage;
            char *repos;
            gtk_tree_model_get(model,&iter,1,&storage,2,&repos,-1);
            char *repos_replaced = yon_char_replace(repos,"\n",",");
            char *parameter_key = yon_char_unite(storage,!yon_char_is_empty(repos_replaced)?":":NULL,repos_replaced,NULL);
            char *parameter = REPOPUBLIC_FTP(parameter_key);
            yon_config_remove_by_key(parameter);
            free(parameter);
            free(parameter_key);
            if (!yon_char_is_empty(repos_replaced)) free(repos_replaced);
            yon_interface_update(widgets);
        }
    }
}