#include "ubl-settings-bootloader.h"
#include <pwquality.h>

double yon_pwquality_check(const char *password);
char *yon_pwquality_error_check(const char *password);

double yon_pwquality_check(const char *password){
	pwquality_settings_t *settings = pwquality_default_settings();
	int strength = pwquality_check(settings,password,NULL,NULL,NULL);
	return (double)strength;
}

char *yon_pwquality_error_check(const char *password){
	void *auxerror = NULL;
	pwquality_settings_t *settings = pwquality_default_settings();
	int strength = pwquality_check(settings,password,NULL,NULL,&auxerror);
		const char *add_info = NULL;
		if (auxerror){
			add_info = pwquality_strerror(NULL,4094,strength,auxerror);
		}
		if (strength<30){
			return (char*)add_info;
		}
	return NULL;
}

void yon_password_change(GtkWidget *, GtkEntry *entry){
	yon_password_window *window = yon_password_open(entry);
	gtk_widget_set_size_request(window->Window,-1,200);
	int size = 0;
	config_str hash_command = yon_char_parsed_new(&size,"echo ",NULL);
	config_str hash_name = yon_char_parsed_new(&size,"no encryption",NULL);

    yon_password_hash_list_set(window,hash_name,hash_command,size);
	yon_password_function_set(window, (double(*)(const char*))yon_pwquality_check,(char*(*)(const char*))yon_pwquality_error_check);
	gtk_widget_hide(gtk_widget_get_parent(window->EncryptionCombo));
	gtk_combo_box_set_active(GTK_COMBO_BOX(window->EncryptionCombo),1);
	gtk_widget_set_sensitive(window->HashBox,1);

	const char *password = gtk_entry_get_text(GTK_ENTRY(entry));
	if (g_regex_match_simple("^grub.pdkdf.*2",password,0,0)){
		gtk_entry_set_text(GTK_ENTRY(window->PasswordEntry),"");
		gtk_entry_set_text(GTK_ENTRY(window->RepeatPasswordEntry),"");
		gtk_entry_set_text(GTK_ENTRY(window->PasswordHashEntry),password);
	}
}

yon_bootloader_password_window *yon_ubl_settings_usergroups_password_new(){
	yon_bootloader_password_window *window = malloc(sizeof(yon_bootloader_password_window));
	GtkBuilder *builder = gtk_builder_new_from_resource(glade_path_password);
	window->Window=yon_gtk_builder_get_widget(builder,"MainWindow");
	window->StatusBox=yon_gtk_builder_get_widget(builder,"StatusBox");
	window->TitleLabel=yon_gtk_builder_get_widget(builder,"userTitleNameLabel");
	window->PasswordEntry=yon_gtk_builder_get_widget(builder,"PasswordEntry");
	window->RepeatPasswordEntry=yon_gtk_builder_get_widget(builder,"RepeatPasswordEntry");
	window->PasswordHashEntry=yon_gtk_builder_get_widget(builder,"PasswordHashEntry");
	window->HashBox=yon_gtk_builder_get_widget(builder,"HashBox");
	window->PasswordBox=yon_gtk_builder_get_widget(builder,"PasswordBox");
	window->NoEncriptionCheck=yon_gtk_builder_get_widget(builder,"NoEncriptionCheck");

	window->UserCancelButton=yon_gtk_builder_get_widget(builder,"UserCancelButton");
	window->UserOkButton=yon_gtk_builder_get_widget(builder,"UserOkButton");

	yon_window_config_custom_window_setup(GTK_WINDOW(window->Window),"PasswordWindow");

	g_signal_connect(G_OBJECT(window->UserCancelButton),"clicked",G_CALLBACK(on_subwindow_close),NULL);
	g_signal_connect(G_OBJECT(window->NoEncriptionCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button),window->PasswordHashEntry);
	g_signal_connect(G_OBJECT(window->NoEncriptionCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button_inversed),window->PasswordBox);
    yon_gtk_entry_set_password_visibility_icon(GTK_ENTRY(window->PasswordEntry));
    yon_gtk_entry_set_password_visibility_icon(GTK_ENTRY(window->RepeatPasswordEntry));
    yon_gtk_entry_set_password_visibility_icon(GTK_ENTRY(window->PasswordHashEntry));
	gtk_window_set_title(GTK_WINDOW(window->Window),PASSWORD_TITLE_LABEL);
	
return window;
}


int yon_check_password_blocked(char *password){
	if (!yon_char_is_empty(password)){
		if (!strcmp(password,"!*")){
			memset(password,0,strlen(password));
			return 1;
		} else if (password[0]=='!'&&password[1]=='*'){
			char *copy = yon_char_new(password);
			memcpy(copy,password+2,strlen(password+2));
			memset(password,0,strlen(password));
			memcpy(password,copy,strlen(copy));
			free(copy);
			return 1;
		} else if (password[0]=='!'||password[0]=='*'){
			free(yon_char_divide(password,0));
			return 1;
		}
	} 
	return 0;
}