#include "ubl-settings-usergroups.h"



void on_password_user_changed(GtkComboBox *,ubl_settings_usergroups_user_window *window){
	char *passw = (char*)gtk_entry_get_text(GTK_ENTRY(window->userPasswordEntry));
	if ((passw[0]=='$')){
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->userSyncSAMBACheck),0);
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->userSyncSAMBAPasswordCheck),0);
		gtk_widget_set_sensitive(GTK_WIDGET(window->userSyncSAMBAPasswordCheck),0);
	} else {
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->userSyncSAMBACheck),0);
		gtk_widget_set_sensitive(GTK_WIDGET(window->userSyncSAMBACheck),1);
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->userSyncSAMBAPasswordCheck),0);
		gtk_widget_set_sensitive(GTK_WIDGET(window->userSyncSAMBAPasswordCheck),1);

	}
}

void on_password_change(GtkWidget *, dictionary *entry_dict){
	GtkEntry *output_target = yon_dictionary_get_data(entry_dict->first->next,GtkEntry*);
	ubl_settings_usergroups_password_window *window = yon_ubl_settings_usergroups_password_new();
	dictionary *dict = NULL;
	char *password = (char*)gtk_entry_get_text(output_target);
	if (!yon_char_is_empty(password)){
		if (main_config.load_mode==YON_CONFIG_GLOBAL){
			password = yon_char_new(password);
			if (!check_is_password_hash(password)){
				gtk_entry_set_text(GTK_ENTRY(window->PasswordHashEntry),password);
			} else {
				if (password[0]=='%'&&password[1]=='%'){
					free(yon_char_divide(password,1));
					gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->NoEncriptionCheck),1);
				}
				gtk_entry_set_text(GTK_ENTRY(window->PasswordEntry),password);
			}
		}
	}
	yon_dictionary_add_or_create_if_exists_with_data(dict,"window",window);
	yon_dictionary_add_or_create_if_exists_with_data(dict,"target",output_target);
    g_signal_connect(G_OBJECT(window->UserOkButton),"clicked",G_CALLBACK(on_password_accept),dict);
	g_signal_connect(G_OBJECT(window->UserCancelButton),"clicked",G_CALLBACK(on_subwindow_close),NULL);
	gtk_window_set_transient_for(GTK_WINDOW(window->CreateGroupWindow),GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(output_target))));
    gtk_widget_show(window->CreateGroupWindow);

}

void on_password_show_hide(GtkEntry *self, GtkEntryIconPosition icon_pos,  GdkEvent*){
	if (icon_pos == GTK_ENTRY_ICON_SECONDARY){
		int visible = gtk_entry_get_visibility(self);
		if (!visible){
			gtk_entry_set_visibility(self,1);
		} else {
			gtk_entry_set_visibility(self,0);
			gtk_entry_set_invisible_char(self,'*');
		}
	}
}

void on_password_accept(GtkWidget *self, dictionary *dict){
	ubl_settings_usergroups_password_window *window = yon_dictionary_get_data(dict->first,ubl_settings_usergroups_password_window*);
	GtkEntry *entry = yon_dictionary_get_data(dict->first->next,GtkEntry*);
	char *password = (char*)gtk_entry_get_text(GTK_ENTRY(window->PasswordEntry));
	char *password_check = (char*)gtk_entry_get_text(GTK_ENTRY(window->RepeatPasswordEntry));
	if (yon_char_is_empty(password)){
		char *pasted_hash = (char*)gtk_entry_get_text(GTK_ENTRY(window->PasswordHashEntry));
		if (!yon_char_is_empty(pasted_hash)){
				gtk_entry_set_text(entry,pasted_hash);
		} else {
			yon_ubl_status_highlight_incorrect(window->PasswordEntry);
			yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),PASSWORD_SHORT_LABEL(yon_char_from_int(main_config.password_min_length)),5,BACKGROUND_IMAGE_FAIL_TYPE);
			return;
		}
	} else {
		if (yon_char_is_empty(password)){
			yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
			return;
		} else if (password[0]=='%'&&password[1]=='%'){
			yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
			return;
		}
		if (strlen(password)<main_config.password_min_length){
			yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),PASSWORD_SHORT_LABEL(yon_char_from_int(main_config.password_min_length)),5,BACKGROUND_IMAGE_FAIL_TYPE);
			return;
		}
		if (!strcmp(password,password_check)){
			if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->NoEncriptionCheck))){
				char *pasw=yon_char_append("%%",password);
				password=pasw;
			}
				gtk_entry_set_text(GTK_ENTRY(entry),password);
		} else {
			yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),PASSWORD_MISMATCH_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
			return;
		}
		void *auxerror = NULL;
		pwquality_settings_t *settings = pwquality_default_settings();
		int password_strength = pwquality_check(settings,password,NULL,NULL,&auxerror);
		const char *add_info = NULL;
		if (auxerror){
			add_info = pwquality_strerror(NULL,4094,password_strength,auxerror);
		}
		if (password_strength<30){
			yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),(char*)add_info,5,BACKGROUND_IMAGE_FAIL_TYPE);
			return;
		}

	}
	on_subwindow_close(self);
}

void on_password_changed(GtkWidget *, ubl_settings_usergroups_password_window *window){
	pwquality_settings_t *settings = pwquality_default_settings();
	const char *new_password = gtk_entry_get_text(GTK_ENTRY(window->PasswordEntry));
	int password_strength = pwquality_check(settings,new_password,NULL,NULL,NULL);
	gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(window->PasswordStrengthProgress),(gdouble)password_strength/100);
	if (password_strength<30){
		gtk_style_context_add_class(gtk_widget_get_style_context(window->PasswordStrengthProgress),"redBox");
		gtk_style_context_remove_class(gtk_widget_get_style_context(window->PasswordStrengthProgress),"greenBox");
		gtk_style_context_remove_class(gtk_widget_get_style_context(window->PasswordStrengthProgress),"yellowBox");
	} else if (password_strength<60) {
		gtk_style_context_add_class(gtk_widget_get_style_context(window->PasswordStrengthProgress),"yellowBox");
		gtk_style_context_remove_class(gtk_widget_get_style_context(window->PasswordStrengthProgress),"redBox");
		gtk_style_context_remove_class(gtk_widget_get_style_context(window->PasswordStrengthProgress),"greenBox");
		
	} else {
		gtk_style_context_add_class(gtk_widget_get_style_context(window->PasswordStrengthProgress),"greenBox");
		gtk_style_context_remove_class(gtk_widget_get_style_context(window->PasswordStrengthProgress),"redBox");
		gtk_style_context_remove_class(gtk_widget_get_style_context(window->PasswordStrengthProgress),"yellowBox");
	}

}

void on_password_hash_sensitiveness(GtkWidget *,ubl_settings_usergroups_password_window *window){
	if (!yon_char_is_empty(gtk_entry_get_text(GTK_ENTRY(window->PasswordEntry)))||!yon_char_is_empty(gtk_entry_get_text(GTK_ENTRY(window->RepeatPasswordEntry)))){
		gtk_widget_set_sensitive(window->HashBox,0);
		gtk_widget_set_sensitive(window->PasswordBox,1);
	} else if (!yon_char_is_empty(gtk_entry_get_text(GTK_ENTRY(window->PasswordHashEntry)))) {
		gtk_widget_set_sensitive(window->HashBox,1);
		gtk_widget_set_sensitive(window->PasswordBox,0);
	} else {
		gtk_widget_set_sensitive(window->HashBox,1);
		gtk_widget_set_sensitive(window->PasswordBox,1);
	}
}

gboolean on_password_focus_event(GtkWidget *self, GdkEvent *, ubl_settings_usergroups_password_window *window){
	gtk_popover_set_relative_to(GTK_POPOVER(window->PasswordTooltipPopover),self);
	gtk_popover_set_position(GTK_POPOVER(window->PasswordTooltipPopover),GTK_POS_RIGHT);
	g_signal_handlers_block_by_func(G_OBJECT(self),G_CALLBACK(on_password_focus_out_event),window);
	g_signal_handlers_block_by_func(G_OBJECT(self),G_CALLBACK(on_password_focus_event),window);
	gtk_popover_popup(GTK_POPOVER(window->PasswordTooltipPopover));
	gtk_widget_grab_focus(self);
	g_signal_handlers_unblock_by_func(G_OBJECT(self),G_CALLBACK(on_password_focus_out_event),window);
	g_signal_handlers_unblock_by_func(G_OBJECT(self),G_CALLBACK(on_password_focus_event),window);
	return GDK_EVENT_PROPAGATE;
} 

gboolean on_password_focus_out_event(GtkWidget *self, GdkEvent *, ubl_settings_usergroups_password_window *window){
	gtk_popover_set_relative_to(GTK_POPOVER(window->PasswordTooltipPopover),self);
	gtk_popover_set_position(GTK_POPOVER(window->PasswordTooltipPopover),GTK_POS_RIGHT);
	gtk_popover_popdown(GTK_POPOVER(window->PasswordTooltipPopover));
	return GDK_EVENT_PROPAGATE;
}
gboolean on_password_tooltip_event(GtkWidget *self, GdkEvent *, ubl_settings_usergroups_password_window *window){
	gtk_popover_set_relative_to(GTK_POPOVER(window->PasswordTooltipPopover),self);
	gtk_popover_set_position(GTK_POPOVER(window->PasswordTooltipPopover),GTK_POS_RIGHT);
	g_signal_handlers_block_by_func(G_OBJECT(self),G_CALLBACK(on_password_focus_out_event),window);
	g_signal_handlers_block_by_func(G_OBJECT(self),G_CALLBACK(on_password_focus_event),window);
	gtk_popover_popup(GTK_POPOVER(window->PasswordTooltipPopover));
	g_signal_handlers_unblock_by_func(G_OBJECT(self),G_CALLBACK(on_password_focus_out_event),window);
	g_signal_handlers_unblock_by_func(G_OBJECT(self),G_CALLBACK(on_password_focus_event),window);
	return GDK_EVENT_PROPAGATE;
}

gboolean on_password_tooltip_out_event(GtkWidget *self, GdkEvent *, ubl_settings_usergroups_password_window *window){
	if (gtk_widget_has_focus(window->PasswordEntry)) return GDK_EVENT_PROPAGATE;
	gtk_popover_set_relative_to(GTK_POPOVER(window->PasswordTooltipPopover),self);
	gtk_popover_set_position(GTK_POPOVER(window->PasswordTooltipPopover),GTK_POS_RIGHT);
	gtk_popover_popdown(GTK_POPOVER(window->PasswordTooltipPopover));
	return GDK_EVENT_PROPAGATE;
}

ubl_settings_usergroups_password_window *yon_ubl_settings_usergroups_password_new(){
	ubl_settings_usergroups_password_window *window = malloc(sizeof(ubl_settings_usergroups_password_window));
	GtkBuilder *builder = gtk_builder_new_from_resource(glade_path_ubl_settings_usergroups_password);
	window->CreateGroupWindow=yon_gtk_builder_get_widget(builder,"CreateGroupWindow");
	window->StatusBox=yon_gtk_builder_get_widget(builder,"StatusBox");
	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->PasswordStrengthProgress=yon_gtk_builder_get_widget(builder,"PasswordStrengthProgress");
	window->PasswordTooltipPopover=yon_gtk_builder_get_widget(builder,"PasswordTooltipPopover");
	window->PassowordTooltipLabel=yon_gtk_builder_get_widget(builder,"PassowordTooltipLabel");

	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->CreateGroupWindow),"PasswordWindow");
	g_signal_connect(G_OBJECT(window->UserCancelButton),"clicked",G_CALLBACK(on_subwindow_close),NULL);
	g_signal_connect(G_OBJECT(window->PasswordEntry),"icon-press",G_CALLBACK(on_password_show_hide),NULL);
	g_signal_connect(G_OBJECT(window->PasswordEntry),"changed",G_CALLBACK(on_password_changed),window);
	g_signal_connect(G_OBJECT(window->PasswordEntry),"changed",G_CALLBACK(on_password_hash_sensitiveness),window);
	g_signal_connect(G_OBJECT(window->RepeatPasswordEntry),"changed",G_CALLBACK(on_password_hash_sensitiveness),window);
	g_signal_connect(G_OBJECT(window->PasswordHashEntry),"changed",G_CALLBACK(on_password_hash_sensitiveness),window);
	g_signal_connect(G_OBJECT(window->RepeatPasswordEntry),"icon-press",G_CALLBACK(on_password_show_hide),NULL);
	g_signal_connect(G_OBJECT(window->PasswordHashEntry),"icon-press",G_CALLBACK(on_password_show_hide),NULL);
	g_signal_connect(G_OBJECT(window->PasswordEntry),"focus-in-event",G_CALLBACK(on_password_focus_event),window);
	g_signal_connect(G_OBJECT(window->PasswordEntry),"focus-out-event",G_CALLBACK(on_password_focus_out_event),window);
	g_signal_connect(G_OBJECT(window->PasswordEntry),"enter-notify-event",G_CALLBACK(on_password_tooltip_event),window);
	g_signal_connect(G_OBJECT(window->PasswordEntry),"leave-notify-event",G_CALLBACK(on_password_tooltip_out_event),window);
	gtk_window_set_title(GTK_WINDOW(window->CreateGroupWindow),PASSWORD_TITLE_LABEL);
	
	
return window;
}

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