#include "ubinstall-gtk.h"

int yon_users_save(main_window *widgets){
    if (gtk_combo_box_get_active(GTK_COMBO_BOX(widgets->UserRootPasswordCombo))){
        const char *root_password = gtk_entry_get_text(GTK_ENTRY(widgets->UserRootPasswordEntry));
        if (yon_char_is_empty(root_password)){
            yon_ubl_status_box_spawn(GTK_CONTAINER(main_config.status_box),_EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
            yon_ubl_status_highlight_incorrect(widgets->UserRootPasswordEntry);
            return 0;
        }
        yon_config_register(root_password_parameter,root_password_parameter_command,(char*)root_password);
    } else {
        yon_config_remove_by_key(root_password_parameter);
    }
    int autologin = gtk_switch_get_active(GTK_SWITCH(widgets->UserAutologinSwitch));
    if (autologin){
        yon_config_register(autologin_parameter,autologin_parameter_command,"yes");
    } else {
        yon_config_register(autologin_parameter,autologin_parameter_command,"no");
    }

    GList *users = gtk_container_get_children(GTK_CONTAINER(widgets->UserAddBox));
    GList *iter;
    for (iter=users;iter;iter=iter->next){
        yon_user_struct *user = g_object_get_data(G_OBJECT(iter->data),"yon_user_struct");
        if (user){
            if (!yon_user_save(user)){
                yon_ubl_status_box_spawn(GTK_CONTAINER(main_config.status_box),_EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);

                return 0;
            }
        }
    }

    if (!main_config.configure_mode){
        int size, users_size, final_size, save_size;
        config_str users = yon_config_find_keys(USERADD_parameter_search,&users_size);
        config_str save_parameters = yon_char_parsed_new(&size,root_password_parameter,autologin_parameter,NULL);
        config_str final = yon_char_parsed_merge(save_parameters,size,users,users_size,&final_size);
        config_str parameters = yon_config_get_save_parameters_by_list(&save_size,final,final_size);
        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,save_size);
        }
        if (users)yon_char_parsed_free(users,users_size);
        if (save_parameters)yon_char_parsed_free(save_parameters,size);
    }
    yon_debug_output("%s\n",yon_config_get_all_info());
    return 1;
}

int yon_user_save(yon_user_struct *user){
    char *name = (char *)gtk_entry_get_text(GTK_ENTRY(user->UsernameEntry));
    char *login = (char *)gtk_entry_get_text(GTK_ENTRY(user->LoginEntry));
    char *password = NULL;
    if (yon_char_is_empty(login)){
        yon_ubl_status_box_spawn(GTK_CONTAINER(main_config.status_box),_EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
        yon_ubl_status_highlight_incorrect(user->LoginEntry);
        return 0;
    }
    if (gtk_combo_box_get_active(GTK_COMBO_BOX(user->PasswordCombo))){
        password = (char *)gtk_entry_get_text(GTK_ENTRY(user->PasswordEntry));
        if (yon_char_is_empty(password)){
            yon_ubl_status_highlight_incorrect(user->PasswordEntry);
            return 0;
        }
    }
    char *parameter = yon_char_unite(yon_char_return_if_exist(name,""),":::::",yon_char_return_if_exist(password,""),NULL);
    yon_config_register(USERADD_parameter(login),USERADD_parameter_command(login),parameter);
    free(parameter);
    return 1;
}

void on_user_add(GtkWidget *,main_window *widgets){
    yon_user_struct *user = yon_user_struct_new();
    gtk_box_pack_start(GTK_BOX(widgets->UserAddBox),user->MainBox,0,0,0);
}

void on_user_remove_clicked(GtkWidget *,yon_user_struct *user){
    gtk_widget_destroy(user->MainBox);
    free(user);
}

void yon_password_root_new(GtkWidget *, main_window *widgets){
    yon_password_window *window = yon_password_open(GTK_ENTRY(widgets->UserRootPasswordEntry));
    gtk_widget_hide(gtk_widget_get_parent(window->EncryptionCombo));
    gtk_widget_show(window->Window);
}

void yon_password_new(GtkWidget *, yon_user_struct *user){
    yon_password_window *window = yon_password_open(GTK_ENTRY(user->PasswordEntry));
    gtk_widget_hide(gtk_widget_get_parent(window->EncryptionCombo));
    gtk_widget_show(window->Window);
}

yon_user_struct *yon_user_struct_new(){
    yon_user_struct *user = new(yon_user_struct);
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_path_user);

    user->MainBox = yon_gtk_builder_get_widget(builder,"MainBox");
    user->RemoveButton = yon_gtk_builder_get_widget(builder,"RemoveButton");
    user->UsernameEntry = yon_gtk_builder_get_widget(builder,"UsernameEntry");
    user->LoginEntry = yon_gtk_builder_get_widget(builder,"LoginEntry");
    user->PasswordCombo = yon_gtk_builder_get_widget(builder,"PasswordCombo");
    user->PasswordEntry = yon_gtk_builder_get_widget(builder,"PasswordEntry");
    user->PasswordButton = yon_gtk_builder_get_widget(builder,"PasswordButton");

    g_object_set_data(G_OBJECT(user->MainBox),"yon_user_struct",user);

    yon_gtk_entry_set_password_visibility_icon(GTK_ENTRY(user->PasswordEntry));

    g_signal_connect(G_OBJECT(user->PasswordCombo),"changed",G_CALLBACK(yon_gtk_widget_set_sensitive_from_combo_box),user->PasswordEntry);
    g_signal_connect(G_OBJECT(user->PasswordCombo),"changed",G_CALLBACK(yon_gtk_widget_set_sensitive_from_combo_box),user->PasswordButton);
    g_signal_connect(G_OBJECT(user->PasswordButton),"clicked",G_CALLBACK(yon_password_new),user);
    g_signal_connect(G_OBJECT(user->RemoveButton),"clicked",G_CALLBACK(on_user_remove_clicked),user);
    return user;
}

void yon_user_init(main_window *widgets){
    yon_gtk_entry_set_password_visibility_icon(GTK_ENTRY(widgets->UserRootPasswordEntry));
    char *root_password = config(root_password_parameter);
    char *autologin = config(autologin_parameter);
    if (!yon_char_is_empty(root_password)){
        gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->UserRootPasswordCombo),1);
        gtk_entry_set_text(GTK_ENTRY(widgets->UserRootPasswordEntry),root_password);
    } else {
        gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->UserRootPasswordCombo),0);
        gtk_entry_set_text(GTK_ENTRY(widgets->UserRootPasswordEntry),"");
    }
    if (!yon_char_is_empty(autologin)&&(!strcmp(autologin,"yes")||!strcmp(autologin,"enable"))){
        gtk_switch_set_active(GTK_SWITCH(widgets->UserAutologinSwitch),1);
    } else {
        gtk_switch_set_active(GTK_SWITCH(widgets->UserAutologinSwitch),0);
    }

    GList *list = gtk_container_get_children(GTK_CONTAINER(widgets->UserAddBox));
    GList *iter;
    for (iter = list; iter; iter = iter->next){
        yon_user_struct *user = g_object_get_data(G_OBJECT(iter->data),"yon_user_struct");
        gtk_widget_destroy(user->MainBox);
        free(user);
    }
    int users_size;
    config_str users = yon_config_get_all_by_key(USERADD_parameter_search,&users_size);
    if (!users_size){
        yon_user_struct *user = yon_user_struct_new();
        gtk_box_pack_start(GTK_BOX(widgets->UserAddBox),user->MainBox,0,0,0);
        gtk_entry_set_text(GTK_ENTRY(user->UsernameEntry),ADMINISTRATOR_LABEL);
        gtk_entry_set_text(GTK_ENTRY(user->LoginEntry),"superadmin");
    }
    for (int i=0;i<users_size;i++){
        yon_user_struct *user = yon_user_struct_new();
        gtk_box_pack_start(GTK_BOX(widgets->UserAddBox),user->MainBox,0,0,0);

        int parsed_size;
        char *parameter = yon_char_new(users[i]);
        char *parameter_name = yon_char_divide_search(parameter,"=",-1);
        char *key = yon_config_parameter_get_key(parameter_name);
        char *login = yon_config_parameter_get_key(strstr(key,"USERADD["));
        config_str parsed = yon_char_parse(parameter,&parsed_size,":");

        gtk_entry_set_text(GTK_ENTRY(user->LoginEntry),login);
        if (parsed_size>0)
            gtk_entry_set_text(GTK_ENTRY(user->UsernameEntry),parsed[0]);
        if (parsed_size>5&&!yon_char_is_empty(parsed[5])){
            gtk_entry_set_text(GTK_ENTRY(user->PasswordEntry),parsed[5]);
            gtk_combo_box_set_active(GTK_COMBO_BOX(user->PasswordCombo),1);
        }
        gtk_widget_show(user->MainBox);
    }
}