#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);
    }

    GList *users = gtk_container_get_children(GTK_CONTAINER(widgets->UserAddBox));
    GList *iter;
        {
            int size = 0;
            config_str users = yon_config_get_all_by_key(USERADD_parameter_search,&size);
            for(int i=0;i<size;i++){
                if (strstr(users[i],"=")){
                    char *temp = yon_char_divide_search(users[i],"=",-1);
                    free(users[i]);
                    users[i] = temp;
                }
            }
            yon_config_remove_by_list(users,size);
        }
    if (!users){
        yon_config_register(DEFAULTUSER_parameter,DEFAULTUSER_parameter_command,"root");
    } else {
        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;
                }
            }
        }
        yon_config_remove_by_key(DEFAULTUSER_parameter);

    }
    if (!main_config.configure_mode){
        int users_size;
        config_str users = yon_config_get_all_keys_by_key(USERADD_parameter_search,&users_size);
        yon_char_parsed_add_or_create_if_exists(users,&users_size,root_password_parameter);
        yon_char_parsed_add_or_create_if_exists(users,&users_size,autologin_parameter);
        yon_char_parsed_add_or_create_if_exists(users,&users_size,DEFAULTUSER_parameter);
        yon_char_parsed_add_or_create_if_exists(users,&users_size,ADDADM_parameter);
        yon_debug_output("%s\n",yon_config_save_list_simple(users,users_size,main_config.config_load_path));
        for (int i=0;i<users_size;i++){
            yon_config_set_status(users[i],0);
        }
    }
    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;
    }
    char *default_user = config(DEFAULTUSER_parameter);
    if (yon_char_is_empty(default_user)){
        default_user = config(DEFAULTUSER_system_parameter);
    }
    if (!strcmp(default_user,login)){
        if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(user->AdminCheck))){
            yon_config_register(ADDADM_parameter,ADDADM_parameter_command,"yes");
        } else {
            yon_config_register(ADDADM_parameter,ADDADM_parameter_command,"no");
        }
    }
    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 *admin_uid = NULL;
    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(user->AdminCheck))){
        admin_uid = yon_config_get(admuid_parameter);
    }
    char *parameter = yon_char_unite(yon_char_return_if_exist(name,""),":",yon_char_return_if_exist(admin_uid,""),"::::",yon_char_return_if_exist(password,""),NULL);
    yon_config_register(USERADD_parameter(login),USERADD_parameter_command(login),parameter);
    if (!yon_char_is_empty(user->old_login)&&strcmp(user->old_login,login)){
        yon_config_remove_by_key(USERADD_parameter(user->old_login));
    }
    free(parameter);
    return 1;
}

gboolean on_user_autoloin_switch(GtkWidget *, int status, main_window *){
    if (status){
        yon_config_register(autologin_parameter,autologin_parameter_command,"yes");
    } else {
        yon_config_register(autologin_parameter,autologin_parameter_command,"no");
    }
    return 0;
}

void on_user_add(GtkWidget *,main_window *widgets){
    int admin=1;
    {
        GList *list = gtk_container_get_children(GTK_CONTAINER(widgets->UserAddBox));
        GList *iter;
        for(iter=list;iter;iter=iter->next){
            yon_user_struct *cur_user = g_object_get_data(G_OBJECT(iter->data),"yon_user_struct");
            if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cur_user->AdminCheck))){
                admin=0;
            }
        }
    }
    yon_user_struct *user = yon_user_struct_new();
    g_signal_connect(G_OBJECT(user->AdminCheck),"clicked",G_CALLBACK(yon_user_admin_check),widgets);
    g_object_set_data(G_OBJECT(user->MainBox),"widgets",widgets);
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(user->AdminCheck),admin);
    gtk_box_pack_start(GTK_BOX(widgets->UserAddBox),user->MainBox,0,0,0);
    user->changed=1;
}

void on_user_remove_clicked(GtkWidget *,yon_user_struct *user){
    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(user->AdminCheck))){
        main_window *widgets = g_object_get_data(G_OBJECT(user->MainBox),"widgets");
        GList *list = gtk_container_get_children(GTK_CONTAINER(widgets->UserAddBox));
        GList *iter;
        for(iter=list;iter;iter=iter->next){
            if (iter->data == user->MainBox){
                if (iter->next){
                    yon_user_struct *cur_user = g_object_get_data(G_OBJECT(iter->next->data),"yon_user_struct");
                    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cur_user->AdminCheck),1);
                } else if (iter->prev){
                    yon_user_struct *cur_user = g_object_get_data(G_OBJECT(iter->prev->data),"yon_user_struct");
                    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cur_user->AdminCheck),1);
                }
            }
        }
    }
    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));
    yon_password_function_set(window,(double*)yon_password_check_func);
    yon_password_hash_list_set(window,NULL,NULL,0);
}

void yon_password_new(GtkWidget *, yon_user_struct *user){
    yon_password_window *window = yon_password_open(GTK_ENTRY(user->PasswordEntry));
    yon_password_function_set(window,(double*)yon_password_check_func);
    yon_password_hash_list_set(window,NULL,NULL,0);
}

void yon_user_admin_check(GtkWidget *self, main_window *widgets){
    yon_user_struct *user = g_object_get_data(G_OBJECT(self),"yon_user_struct");
    GList *list = gtk_container_get_children(GTK_CONTAINER(widgets->UserAddBox));
    user->changed=1;
    GList *iter;
    for(iter=list;iter;iter=iter->next){
        if (iter->data!=user->MainBox){
            yon_user_struct *cur_user = g_object_get_data(G_OBJECT(iter->data),"yon_user_struct");
            g_signal_handlers_block_by_func(G_OBJECT(cur_user->AdminCheck),G_CALLBACK(yon_user_admin_check),widgets);
            gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cur_user->AdminCheck),0);
            g_signal_handlers_unblock_by_func(G_OBJECT(cur_user->AdminCheck),G_CALLBACK(yon_user_admin_check),widgets);
        }
    }
}

yon_user_struct *yon_user_struct_new(){
    yon_user_struct *user = new(yon_user_struct);
    memset(user,0,sizeof(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");
    user->AdminCheck = yon_gtk_builder_get_widget(builder,"AdminCheck");
	yon_gtk_entry_block_symbols(GTK_ENTRY(user->LoginEntry),"[]'\"=:\n");
	yon_gtk_entry_block_symbols(GTK_ENTRY(user->UsernameEntry),"[]'\":\n");

    g_object_set_data(G_OBJECT(user->MainBox),"yon_user_struct",user);
    g_object_set_data(G_OBJECT(user->AdminCheck),"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){

    if (!main_config.configure_mode){
        int size = 0;
        config_str users = yon_config_get_all_by_key(USERADD_parameter_search,&size);
        for(int i=0;i<size;i++){
            if (strstr(users[i],"=")){
                char *temp = yon_char_divide_search(users[i],"=",-1);
                free(users[i]);
                users[i] = temp;
            }
        }
        yon_config_remove_by_list(users,size);
        yon_config_update_by_args(main_config.config_load_path, autologin_parameter,DEFAULTPASSWD_parameter,root_password_parameter,DEFAULTPASSWD_system_parameter,DEFAULTROOTPASSWD_system_parameter,NULL);
        config_str parameters = yon_config_load(config_get_command(main_config.config_load_path),&size);
        for (int i=0;i<size;i++){
            if (strstr(parameters[i],USERADD_parameter_search)){
                yon_config_import_string(main_config.configure_mode,parameters[i]);
            }
        }
    }
    gtk_combo_box_text_remove_all(GTK_COMBO_BOX_TEXT(widgets->UserRootPasswordCombo));
    gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(widgets->UserRootPasswordCombo),NULL,DEFAULT_LABEL);
    gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(widgets->UserRootPasswordCombo),NULL,SET_PASSWORD_LABEL);
    char *autologin = config(autologin_parameter);
    char *default_passwd = yon_config_get_by_key(DEFAULTPASSWD_parameter);
    char *root_password = yon_config_get_by_key(root_password_parameter);
    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);
    char *addadm = config(ADDADM_parameter);
    if (yon_char_is_empty(addadm)){
        addadm = config(ADDADM_system_parameter);
    }

    char *default_user = config(DEFAULTUSER_parameter);
    if (yon_char_is_empty(default_user)){
        default_user = config(DEFAULTUSER_system_parameter);
    }
    if (!users_size){
        char *def_usr = config(DEFAULTUSER_parameter);
        if (yon_char_is_empty(def_usr)||(!yon_char_is_empty(def_usr)&&strcmp(def_usr,"root"))){
            yon_user_struct *user = yon_user_struct_new();
            gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(user->AdminCheck),1);
            g_signal_connect(G_OBJECT(user->AdminCheck),"clicked",G_CALLBACK(yon_user_admin_check),widgets);
            g_object_set_data(G_OBJECT(user->MainBox),"widgets",widgets);
            gtk_box_pack_start(GTK_BOX(widgets->UserAddBox),user->MainBox,0,0,0);
            gtk_entry_set_text(GTK_ENTRY(user->UsernameEntry),ADMINISTRATOR_LABEL);
            if (!yon_char_is_empty(default_user))
                gtk_entry_set_text(GTK_ENTRY(user->LoginEntry),default_user);
            else {
                gtk_entry_set_text(GTK_ENTRY(user->LoginEntry),"superadmin");
            }
            user->changed=1;
                gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(user->AdminCheck),1);
            if (yon_char_is_empty(default_passwd)||yon_config_check_ignore(DEFAULTPASSWD_parameter)){
                gtk_combo_box_set_active(GTK_COMBO_BOX(user->PasswordCombo),0);
                gtk_entry_set_placeholder_text(GTK_ENTRY(widgets->UserRootPasswordEntry),yon_config_default_get_by_key(DEFAULTPASSWD_parameter));
            } else {
                gtk_combo_box_set_active(GTK_COMBO_BOX(user->PasswordCombo),1);
                gtk_entry_set_text(GTK_ENTRY(widgets->UserRootPasswordEntry),config(DEFAULTPASSWD_system_parameter));
            }
        }
    }
    for (int i=0;i<users_size;i++){
        yon_user_struct *user = yon_user_struct_new();
        g_signal_connect(G_OBJECT(user->AdminCheck),"clicked",G_CALLBACK(yon_user_admin_check),widgets);
        g_object_set_data(G_OBJECT(user->MainBox),"widgets",widgets);
        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);
            if (yon_config_get_status(parameter_name)<-1) continue;
        char *key = yon_config_parameter_get_key(parameter_name);
        char *login = yon_config_parameter_get_key(strstr(key,"USERADD["));
        yon_char_remove_brackets(login);
        config_str parsed = yon_char_parse(parameter,&parsed_size,":");

        gtk_entry_set_text(GTK_ENTRY(user->LoginEntry),login);
        user->old_login = yon_char_new(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])){
            if (strcmp(parsed[5],default_passwd)){
                gtk_entry_set_text(GTK_ENTRY(user->PasswordEntry),parsed[5]);
                gtk_combo_box_set_active(GTK_COMBO_BOX(user->PasswordCombo),1);
            }
        }
        if (parsed_size>1){
            if (!yon_char_is_empty(parsed[1])){
                gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(user->AdminCheck),1);
            }
        } else if (!strcmp(login,default_user)){
            gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(user->AdminCheck),!strcmp(addadm,"yes"));
        } else {
            gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(user->AdminCheck),strcmp(addadm,"yes"));
        }
        gtk_widget_show(user->MainBox);
    }

    if (yon_char_is_empty(root_password)||yon_config_check_ignore(root_password_parameter)){
        gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->UserRootPasswordCombo),0);
        gtk_entry_set_placeholder_text(GTK_ENTRY(widgets->UserRootPasswordEntry),yon_config_default_get_by_key(root_password_parameter));
    } else {
        gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->UserRootPasswordCombo),1);
        gtk_entry_set_text(GTK_ENTRY(widgets->UserRootPasswordEntry),config(DEFAULTROOTPASSWD_system_parameter));
        
    }
}