#include "ubl-settings-audit.h"

config main_config;

//functions

void yon_load_proceed(YON_CONFIG_TYPE type){
	if (type!=YON_CONFIG_CUSTOM){
		yon_config_clean();
	}
    if (!yon_char_is_empty(config_get_default_command))
        yon_config_load_config(YON_CONFIG_DEFAULT,config_get_default_command,NULL);
    if (type==YON_CONFIG_GLOBAL){
		yon_config_load_config(type,config_get_global_command,NULL);
	} else if (type==YON_CONFIG_LOCAL){
		yon_config_load_config(type,config_get_local_command,NULL);
    } else if (type==YON_CONFIG_CUSTOM){
		char *path="";
        textdomain(template_ui_LocaleName);
		GtkWidget *dialog = gtk_file_chooser_dialog_new(template_app_information.app_title,NULL,GTK_FILE_CHOOSER_ACTION_SAVE,CANCEL_LABEL,GTK_RESPONSE_CANCEL,OPEN_LABEL,GTK_RESPONSE_ACCEPT,NULL);
        textdomain(LocaleName);
        gtk_window_set_icon_name(GTK_WINDOW(dialog),"com.ublinux.ubl-settings-services");
        gtk_window_set_title(GTK_WINDOW(dialog),TITLE_LABEL);
        GtkFileFilter *filter = gtk_file_filter_new();
		gtk_file_filter_add_pattern(filter,"*.ini");
        gtk_file_filter_set_name(filter, "*.ini");
		gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog),filter);
		gtk_widget_show(dialog);
		int response = gtk_dialog_run(GTK_DIALOG(dialog));
		if (response == GTK_RESPONSE_ACCEPT){
			char *file = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
			if (!yon_char_is_empty(file)){
				path=file;
			}
			gtk_widget_destroy(dialog);
		} else {
			gtk_widget_destroy(dialog);
		}
        yon_config_clean();
        char *command = yon_config_get_custom_command(path);
		yon_config_load_config(type,command,NULL);
	}
}

void on_to_date_selected(GtkWidget *self,main_window *window){
    guint from_year, from_month, from_day, from_hour, from_minute, from_second;
    guint to_year, to_month, to_day, to_hour, to_minute, to_second;
    while(gtk_events_pending())gtk_main_iteration();
    gtk_calendar_get_date(GTK_CALENDAR(window->FromDateCalendar),&from_year, &from_month, &from_day);
    from_hour = gtk_spin_button_get_value(GTK_SPIN_BUTTON(window->FromHoursSpin));
    from_minute = gtk_spin_button_get_value(GTK_SPIN_BUTTON(window->FromMinutesSpin));
    from_second = gtk_spin_button_get_value(GTK_SPIN_BUTTON(window->FromSecondsSpin));
    gtk_calendar_get_date(GTK_CALENDAR(window->ToDateCalendar),&to_year, &to_month, &to_day);
    to_hour = gtk_spin_button_get_value(GTK_SPIN_BUTTON(window->ToHoursSpin));
    to_minute = gtk_spin_button_get_value(GTK_SPIN_BUTTON(window->ToMinutesSpin));
    to_second = gtk_spin_button_get_value(GTK_SPIN_BUTTON(window->ToSecondsSpin));

    GDateTime *from_datetime = g_date_time_new_local(from_year,from_month,from_day,from_hour,from_minute,from_second);
    GDateTime *to_datetime = g_date_time_new_local(to_year,to_month,to_day,to_hour,to_minute,to_second);
    if (from_datetime&&to_datetime&&!yon_char_is_empty(gtk_entry_get_text(GTK_ENTRY(window->SystemToDateEntry)))&&!yon_char_is_empty(gtk_entry_get_text(GTK_ENTRY(window->SystemFromDateEntry)))){
        if (self == window->ToDateCalendar||self == window->ToHoursSpin||self == window->ToMinutesSpin||self == window->ToSecondsSpin){
            if (g_date_time_compare(to_datetime,from_datetime)==-1){
                g_date_time_unref(to_datetime);
                to_datetime = g_date_time_new_local(from_year,from_month,from_day,from_hour,from_minute,from_second);
                g_signal_handlers_block_by_func(G_OBJECT(window->ToDateCalendar),on_to_date_selected,window);
                g_signal_handlers_block_by_func(G_OBJECT(window->ToHoursSpin),on_to_date_selected,window);
                g_signal_handlers_block_by_func(G_OBJECT(window->ToMinutesSpin),on_to_date_selected,window);
                g_signal_handlers_block_by_func(G_OBJECT(window->ToSecondsSpin),on_to_date_selected,window);

                gtk_calendar_select_day(GTK_CALENDAR(window->ToDateCalendar),from_day);
                gtk_calendar_select_month(GTK_CALENDAR(window->ToDateCalendar),from_month,from_year);
                gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->ToHoursSpin),from_hour);
                gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->ToMinutesSpin),from_minute);
                gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->ToSecondsSpin),from_second);

                g_signal_handlers_unblock_by_func(G_OBJECT(window->ToDateCalendar),on_to_date_selected,window);
                g_signal_handlers_unblock_by_func(G_OBJECT(window->ToHoursSpin),on_to_date_selected,window);
                g_signal_handlers_unblock_by_func(G_OBJECT(window->ToMinutesSpin),on_to_date_selected,window);
                g_signal_handlers_unblock_by_func(G_OBJECT(window->ToSecondsSpin),on_to_date_selected,window);
            }
        } else if (self == window->FromDateCalendar||self == window->FromHoursSpin||self == window->FromMinutesSpin||self == window->FromSecondsSpin){
            if (g_date_time_compare(from_datetime,to_datetime)==1){
                g_date_time_unref(from_datetime);
                from_datetime = g_date_time_new_local(to_year,to_month,to_day,to_hour,to_minute,to_second);
                g_signal_handlers_block_by_func(G_OBJECT(window->FromDateCalendar),on_to_date_selected,window);
                g_signal_handlers_block_by_func(G_OBJECT(window->FromHoursSpin),on_to_date_selected,window);
                g_signal_handlers_block_by_func(G_OBJECT(window->FromMinutesSpin),on_to_date_selected,window);
                g_signal_handlers_block_by_func(G_OBJECT(window->FromSecondsSpin),on_to_date_selected,window);

                gtk_calendar_select_day(GTK_CALENDAR(window->FromDateCalendar),to_day);
                gtk_calendar_select_month(GTK_CALENDAR(window->FromDateCalendar),to_month,to_year);
                gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->FromHoursSpin),to_hour);
                gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->FromMinutesSpin),to_minute);
                gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->FromSecondsSpin),to_second);

                g_signal_handlers_unblock_by_func(G_OBJECT(window->FromDateCalendar),on_to_date_selected,window);
                g_signal_handlers_unblock_by_func(G_OBJECT(window->FromHoursSpin),on_to_date_selected,window);
                g_signal_handlers_unblock_by_func(G_OBJECT(window->FromMinutesSpin),on_to_date_selected,window);
                g_signal_handlers_unblock_by_func(G_OBJECT(window->FromSecondsSpin),on_to_date_selected,window);
            }
        }
    }

    char *format;
    yon_window_config_get_parameter(yon_configuration_window_section,date_format_parameter,&format,YON_TYPE_STRING);
    if (!format) format = dmy_dot_format;
    char *final_from_date = g_date_time_format(from_datetime,format);
    char *final_from_time = g_date_time_format(from_datetime,"%H:%M:%S");

    char *final_to_date = g_date_time_format(to_datetime,format);
    char *final_to_time = g_date_time_format(to_datetime,"%H:%M:%S");
    g_date_time_unref(from_datetime);
    g_date_time_unref(to_datetime);

    if (self == window->FromDateCalendar||self == window->FromHoursSpin||self == window->FromMinutesSpin||self == window->FromSecondsSpin||!yon_char_is_empty(gtk_entry_get_text(GTK_ENTRY(window->SystemFromDateEntry)))){
        gtk_entry_set_text(GTK_ENTRY(window->SystemFromDateEntry),final_from_date);
        gtk_entry_set_text(GTK_ENTRY(window->SystemFromTimeEntry),final_from_time);
    }

    if (self == window->ToDateCalendar||self == window->ToHoursSpin||self == window->ToMinutesSpin||self == window->ToSecondsSpin||!yon_char_is_empty(gtk_entry_get_text(GTK_ENTRY(window->SystemToDateEntry)))){
        gtk_entry_set_text(GTK_ENTRY(window->SystemToDateEntry),final_to_date);
        gtk_entry_set_text(GTK_ENTRY(window->SystemToTimeEntry),final_to_time);
    }
}

char *yon_get_date_string(char *raw_date_string){
    char *format;
    yon_window_config_get_parameter(yon_configuration_window_section,date_format_parameter,&format,YON_TYPE_STRING);
    if (!format) format = dmy_dot_format;
    int size;
    raw_date_string[2]='-';
    raw_date_string[5]='-';
    config_str parsed = yon_char_parse(raw_date_string,&size,"-");
    if (size==3){

        if (strcmp(format,mdy_hyphen_format)){
            char *temp = parsed[0];
            parsed[0]=parsed[1];
            parsed[1]=temp;
        }
        char *final_date = yon_char_parsed_to_string(parsed,size,"/");

        return final_date;
    }
    return NULL;
}

void on_accept_period(GtkWidget *, main_window *widgets){

    char *from_date_str = yon_char_new((char*)gtk_entry_get_text(GTK_ENTRY(widgets->SystemFromDateEntry)));
    char *from_time_str = yon_char_new((char*)gtk_entry_get_text(GTK_ENTRY(widgets->SystemFromTimeEntry)));
    char *to_date_str = yon_char_new((char*)gtk_entry_get_text(GTK_ENTRY(widgets->SystemToDateEntry)));
    char *to_time_str = yon_char_new((char*)gtk_entry_get_text(GTK_ENTRY(widgets->SystemToTimeEntry)));

    char *from_date_final = yon_get_date_string(from_date_str);
    char *to_date_final = yon_get_date_string(to_date_str);

    if (yon_char_is_empty(from_date_str)&&yon_char_is_empty(from_time_str)&&yon_char_is_empty(from_time_str)&&yon_char_is_empty(to_time_str)){

        char *command = get_system_today_command;

        yon_terminal_integrated_start_shell(widgets->SystemStatisticsPeriodTerminal,yon_debug_output("%s\n",command),NULL,NULL);
    } else {
        if (yon_char_is_empty(from_date_final)||yon_char_is_empty(to_date_final)){
            GDateTime *datetime = g_date_time_new_now_local();
            if (yon_char_is_empty(from_date_final)){
                from_date_final = g_date_time_format(datetime,"%m/%d/%Y");
                from_time_str = "00:00:00";
            }
            if(yon_char_is_empty(to_date_final)){
                to_date_final = g_date_time_format(datetime,"%m/%d/%Y");
                to_time_str = "23:59:59";
            }
            g_date_time_unref(datetime);
        }

        char *command = get_system_period_command(from_date_final,from_time_str,to_date_final,to_time_str);

        yon_terminal_integrated_start_shell(widgets->SystemStatisticsPeriodTerminal,yon_debug_output("%s\n",command),NULL,NULL);
    }

}

void on_popover_open(GtkWidget *self, main_window *widgets){

    GtkWidget *date_entry = NULL;
    GtkWidget *time_entry = NULL;
    // GtkWidget *FromDatetimePopover = NULL;
    GtkWidget *DateCalendar = NULL;
    GtkWidget *HoursSpin = NULL;
    GtkWidget *MinutesSpin = NULL;
    GtkWidget *SecondsSpin = NULL;
    if (self == widgets->SystemToDateButton){
        gtk_popover_set_relative_to(GTK_POPOVER(widgets->ToDatetimePopover),widgets->SystemToDateButton);
        gtk_popover_popup(GTK_POPOVER(widgets->ToDatetimePopover));
        // FromDatetimePopover = widgets->ToDatetimePopover;
        DateCalendar = widgets->ToDateCalendar;
        HoursSpin = widgets->ToHoursSpin;
        MinutesSpin = widgets->ToMinutesSpin;
        SecondsSpin = widgets->ToSecondsSpin;
        date_entry = widgets->SystemToDateEntry;
        time_entry = widgets->SystemToTimeEntry;
    } else if (self == widgets->SystemFromDateButton){
        gtk_popover_set_relative_to(GTK_POPOVER(widgets->FromDatetimePopover),widgets->SystemFromDateButton);
        gtk_popover_popup(GTK_POPOVER(widgets->FromDatetimePopover));
        // FromDatetimePopover = widgets->FromDatetimePopover;
        DateCalendar = widgets->FromDateCalendar;
        HoursSpin = widgets->FromHoursSpin;
        MinutesSpin = widgets->FromMinutesSpin;
        SecondsSpin = widgets->FromSecondsSpin;
        date_entry = widgets->SystemFromDateEntry;
        time_entry = widgets->SystemFromTimeEntry;
    }
    char *date_str = yon_char_new((char*)gtk_entry_get_text(GTK_ENTRY(date_entry)));
    char *time_str = yon_char_new((char*)gtk_entry_get_text(GTK_ENTRY(time_entry)));

    g_signal_handlers_block_by_func(G_OBJECT(DateCalendar),on_to_date_selected,widgets);
    g_signal_handlers_block_by_func(G_OBJECT(HoursSpin),on_to_date_selected,widgets);
    g_signal_handlers_block_by_func(G_OBJECT(MinutesSpin),on_to_date_selected,widgets);
    g_signal_handlers_block_by_func(G_OBJECT(SecondsSpin),on_to_date_selected,widgets);

    if(!yon_char_is_empty(date_str)){
        char *format;
        yon_window_config_get_parameter(yon_configuration_window_section,date_format_parameter,&format,YON_TYPE_STRING);
        if (yon_char_is_empty(format)) format = dmy_dot_format;

        date_str[2]='-';
        date_str[5]='-';

        int size;
        config_str parsed = yon_char_parse(date_str,&size,"-");
        if (strcmp(format,mdy_hyphen_format)){
            char *temp = parsed[0];
            parsed[0]=parsed[1];
            parsed[1]=temp;
        }

        gtk_calendar_select_month(GTK_CALENDAR(DateCalendar),atoi(parsed[0]),atoi(parsed[2]));
        gtk_calendar_select_day(GTK_CALENDAR(DateCalendar),atoi(parsed[1]));
    } if (!yon_char_is_empty(time_str)){
        int size;
        config_str parsed = yon_char_parse(time_str,&size,":");
        if (size==3){
            gtk_spin_button_set_value(GTK_SPIN_BUTTON(HoursSpin),atoi(parsed[0]));
            gtk_spin_button_set_value(GTK_SPIN_BUTTON(MinutesSpin),atoi(parsed[1]));
            gtk_spin_button_set_value(GTK_SPIN_BUTTON(SecondsSpin),atoi(parsed[2]));
        }
    }
    g_signal_handlers_unblock_by_func(G_OBJECT(DateCalendar),on_to_date_selected,widgets);
    g_signal_handlers_unblock_by_func(G_OBJECT(HoursSpin),on_to_date_selected,widgets);
    g_signal_handlers_unblock_by_func(G_OBJECT(MinutesSpin),on_to_date_selected,widgets);
    g_signal_handlers_unblock_by_func(G_OBJECT(SecondsSpin),on_to_date_selected,widgets);
}

// standard functions

/**config_init() 
 * [EN] 
 *  
 * [RU] 
 * Функция инициализации всех параметров конфигурации 
*/ 
void config_init(){ 
    main_config.always_open_documentation=0; 
    main_config.socket_id=-1; 
    main_config.save_socket_id=-1; 
    main_config.load_socket_id=-1; 
    main_config.lock_help=0; 
    main_config.lock_help=0; 
    main_config.lock_load_global=0; 
    main_config.lock_save_global=0; 
    main_config.lock_save_local=0; 
    main_config.load_mode=1;
}

void on_button_clicked(GtkWidget *self, main_window *widgets){
    if (self==widgets->SystemButton||self == widgets->menu->buttons->first->data){
        gtk_widget_show(widgets->SystemNotebook);
        gtk_widget_hide(widgets->LoginNotebook);
        gtk_widget_hide(widgets->LogNotebook);
    } else if (self == widgets->LoginButton||self == widgets->menu->buttons->first->next->data){
        gtk_widget_hide(widgets->SystemNotebook);
        gtk_widget_show(widgets->LoginNotebook);
        gtk_widget_hide(widgets->LogNotebook);
    } else if (self == widgets->LogButton||self == widgets->menu->buttons->first->next->next->data){
        gtk_widget_hide(widgets->SystemNotebook);
        gtk_widget_hide(widgets->LoginNotebook);
        gtk_widget_show(widgets->LogNotebook);
    }
}


void yon_interface_update(main_window *widgets){
    const char *from_text = gtk_entry_get_text(GTK_ENTRY(widgets->SystemFromDateEntry));
    const char *to_text = gtk_entry_get_text(GTK_ENTRY(widgets->SystemToDateEntry));
    if (!yon_char_is_empty(from_text)) on_to_date_selected(widgets->FromDateCalendar,widgets);
    if (!yon_char_is_empty(to_text)) on_to_date_selected(widgets->ToDateCalendar,widgets);
    yon_date_entry_set_placeholder(widgets);
}

void on_interface_update(GtkWidget *,main_window *widgets){
    vte_terminal_reset(VTE_TERMINAL(widgets->SystemStatisticsTerminal),1,1);
    vte_terminal_reset(VTE_TERMINAL(widgets->SystemCallsTerminal),1,1);

    vte_terminal_reset(VTE_TERMINAL(widgets->LoginAttemptsTerminal),1,1);
    vte_terminal_reset(VTE_TERMINAL(widgets->FailedLoginAttemptsTerminal),1,1);
    vte_terminal_reset(VTE_TERMINAL(widgets->FailedUserAttemptsTerminal),1,1);

    vte_terminal_reset(VTE_TERMINAL(widgets->LogsReportTerminal),1,1);
    vte_terminal_reset(VTE_TERMINAL(widgets->LogsSummaryTerminal),1,1);

        yon_terminal_integrated_start(widgets->SystemStatisticsTerminal,get_general_system_statistics_command);
        yon_terminal_integrated_start(widgets->SystemCallsTerminal,get_all_system_calls_command);

        yon_terminal_integrated_start(widgets->LoginAttemptsTerminal,get_login_attempts_command);
        yon_terminal_integrated_start(widgets->FailedLoginAttemptsTerminal,get_login_failed_attempts_command);
        yon_terminal_integrated_start(widgets->FailedUserAttemptsTerminal,get_user_all_failed_attempts_command);

        yon_terminal_integrated_start(widgets->LogsReportTerminal,get_log_summary_command);
        yon_terminal_integrated_start(widgets->LogsSummaryTerminal,get_generate_log_summary_command);
}

void yon_date_tooltip_update(main_window *widgets){
    char *format = yon_settings_configuration_get(date_format_parameter);
    char *temp = yon_char_replace(format,"%Y",YEAR_FORMAT_LABEL);
    char *month_temp = yon_char_replace(temp,"%m",MONTH_FORMAT_LABEL);
    char *day_temp = yon_char_replace(month_temp,"%d",DAY_FORMAT_LABEL);
    char *tooltip = yon_char_unite(DATE_FORMAT_TOOLTIP_LABEL,": ",day_temp,NULL);
    gtk_widget_set_tooltip_text(widgets->SystemToDateEntry,tooltip);
    gtk_widget_set_tooltip_text(widgets->SystemFromDateEntry,tooltip);
    free(tooltip);
    free(temp);
    free(month_temp);
    free(day_temp);
}

void on_date_format_changed(GtkWidget *, main_window *widgets){
    yon_interface_update(widgets);
    yon_date_tooltip_update(widgets);
}

void yon_date_entry_set_placeholder(main_window *widgets){
        GDateTime *datetime = g_date_time_new_now_local();
        char *format;
        yon_window_config_get_parameter(yon_configuration_window_section,date_format_parameter,&format,YON_TYPE_STRING);
        if (yon_char_is_empty(format)) format = dmy_dot_format;
        char *cur_date = g_date_time_format(datetime,format);
        gtk_entry_set_placeholder_text(GTK_ENTRY(widgets->SystemFromDateEntry),cur_date);
        gtk_entry_set_placeholder_text(GTK_ENTRY(widgets->SystemToDateEntry),cur_date);
        g_date_time_unref(datetime);
    }


void on_closed(GtkWidget *){
    gtk_main_quit();
}

void on_service_start_clicked(GtkWidget *, confirmation_window *window){
    int size,commands_size;
    config_str commands_file = yon_resource_open_file(service_commands_path,&size);
    config_str commands = yon_char_parse(commands_file[1],&commands_size,";");
        if (system(commands[COMMAND_START_COL])){};
    on_service_update_clicked(NULL,window);

}

void on_service_stop_clicked(GtkWidget *, confirmation_window *window){
    int size,commands_size;
    config_str commands_file = yon_resource_open_file(service_commands_path,&size);
    config_str commands = yon_char_parse(commands_file[1],&commands_size,";");
    if (system(commands[COMMAND_STOP_COL])){};
    on_service_update_clicked(NULL,window);

}

void on_service_enable_clicked(GtkWidget *, confirmation_window *window){
    int size,commands_size;
    config_str commands_file = yon_resource_open_file(service_commands_path,&size);
    config_str commands = yon_char_parse(commands_file[1],&commands_size,";");


        if (system(commands[COMMAND_ENABLE_COL])){};
    on_service_update_clicked(NULL,window);

}

void on_service_disable_clicked(GtkWidget *, confirmation_window *window){
    int size,commands_size;
    config_str commands_file = yon_resource_open_file(service_commands_path,&size);
    config_str commands = yon_char_parse(commands_file[1],&commands_size,";");

    if (system(commands[COMMAND_DISABLE_COL])){};
    on_service_update_clicked(NULL,window);
}

void on_service_restart_clicked(GtkWidget *, confirmation_window *window){
    int size,commands_size;
    config_str commands_file = yon_resource_open_file(service_commands_path,&size);
    config_str commands = yon_char_parse(commands_file[1],&commands_size,";");


    if (system(commands[COMMAND_STOP_COL])){};
    if (system(commands[COMMAND_START_COL])){};
    on_service_update_clicked(NULL,window);

}

void on_service_status_clicked(GtkWidget *, confirmation_window *window){
    int size,commands_size;
    config_str commands_file = yon_resource_open_file(service_commands_path,&size);
    config_str commands = yon_char_parse(commands_file[1],&commands_size,";");
    yon_terminal_window_update_button_launch(GTK_WINDOW(window->Window),commands[COMMAND_STATUS_COL],yon_char_get_localised_from_lib(SUCCESS_LABEL),yon_char_get_localised_from_lib(FAIL_LABEL));
}

void on_service_update_clicked(GtkWidget *, confirmation_window *window){

    int size,commands_size;
    config_str commands_file = yon_resource_open_file(service_commands_path,&size);
    config_str commands = yon_char_parse(commands_file[1],&commands_size,";");


    if (!system(commands[COMMAND_STATUS_ENABLE_COL])){
        gtk_label_set_text(GTK_LABEL(window->EnableLabel),ENABLE_LABEL);
        gtk_image_set_from_icon_name(GTK_IMAGE(window->EnableImage),service_start_status_icon_name,GTK_ICON_SIZE_BUTTON);
    } else {
        gtk_label_set_text(GTK_LABEL(window->EnableLabel),DISABLE_LABEL);
        gtk_image_set_from_icon_name(GTK_IMAGE(window->EnableImage),service_stop_status_icon_name,GTK_ICON_SIZE_BUTTON);

    }
    if (!system(commands[COMMAND_STATUS_ACTIVE_COL])){
        gtk_label_set_text(GTK_LABEL(window->ActiveLabel),ACTIVE_LABEL);
        gtk_image_set_from_icon_name(GTK_IMAGE(window->ActiveImage),service_start_status_icon_name,GTK_ICON_SIZE_BUTTON);
        yon_ubl_status_box_render(AUDIT_ACTIVE_LABEL,BACKGROUND_IMAGE_SUCCESS_TYPE);

    } else {
        gtk_label_set_text(GTK_LABEL(window->ActiveLabel),INACTIVE_LABEL);
        gtk_image_set_from_icon_name(GTK_IMAGE(window->ActiveImage),service_stop_status_icon_name,GTK_ICON_SIZE_BUTTON);
        yon_ubl_status_box_render(AUDIT_UNACTIVE_LABEL,BACKGROUND_IMAGE_FAIL_TYPE);

    }
}

confirmation_window *yon_confirmation_window_new(struct confirmation_result *){
    confirmation_window *window = malloc(sizeof(confirmation_window));
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_confirmation_path);
    window->Window = yon_gtk_builder_get_widget(builder,"MainWindow");
    window->StatusBox = yon_gtk_builder_get_widget(builder,"StatusBox");
    window->HeadLabel = yon_gtk_builder_get_widget(builder,"HeadLabel");
    window->TitleLabel = yon_gtk_builder_get_widget(builder,"TitleLabel");
    window->ActiveLabel = yon_gtk_builder_get_widget(builder,"ActiveLabel");
    window->EnableLabel = yon_gtk_builder_get_widget(builder,"EnableLabel");
    window->StartButton = yon_gtk_builder_get_widget(builder,"StartButton");
    window->StopButton = yon_gtk_builder_get_widget(builder,"StopButton");
    window->RestartButton = yon_gtk_builder_get_widget(builder,"RestartButton");
    window->StatusButton = yon_gtk_builder_get_widget(builder,"StatusButton");
    window->EnableButton = yon_gtk_builder_get_widget(builder,"EnableButton");
    window->DisableButton = yon_gtk_builder_get_widget(builder,"DisableButton");
    window->ActiveImage = yon_gtk_builder_get_widget(builder,"ActiveImage");
    window->EnableImage = yon_gtk_builder_get_widget(builder,"EnableImage");
    window->UpdateButton = yon_gtk_builder_get_widget(builder,"UpdateButton");

    g_signal_connect(G_OBJECT(window->Window),"destroy",G_CALLBACK(on_closed),NULL);
    g_signal_connect(G_OBJECT(window->StartButton),"clicked",G_CALLBACK(on_service_start_clicked),window);
    g_signal_connect(G_OBJECT(window->StopButton),"clicked",G_CALLBACK(on_service_stop_clicked),window);
    g_signal_connect(G_OBJECT(window->RestartButton),"clicked",G_CALLBACK(on_service_restart_clicked),window);
    g_signal_connect(G_OBJECT(window->StatusButton),"clicked",G_CALLBACK(on_service_status_clicked),window);
    g_signal_connect(G_OBJECT(window->UpdateButton),"clicked",G_CALLBACK(on_service_update_clicked),window);
    g_signal_connect(G_OBJECT(window->EnableButton),"clicked",G_CALLBACK(on_service_enable_clicked),window);
    g_signal_connect(G_OBJECT(window->DisableButton),"clicked",G_CALLBACK(on_service_disable_clicked),window);
    on_service_update_clicked(NULL,window);
    gtk_widget_show(window->Window);
    return window;
}

void on_service_switch(GtkWidget *, main_window *widgets){
        struct confirmation_result *result = malloc(sizeof(struct confirmation_result));
        result->result=-1;
        confirmation_window *window = yon_confirmation_window_new(result);
        yon_gtk_window_setup(GTK_WINDOW(window->Window),GTK_WINDOW(widgets->Window),AUDITD_SERVICE_TITLE_LABEL,icon_path,"confirmation_window");
        if (window){
            gtk_main();
            gtk_widget_destroy(window->Window);
        }
        int error = result->result;
        GtkWidget *image = gtk_button_get_image(GTK_BUTTON(widgets->ServiceSwitchButton));
    int service_active = system(check_audit_active_command);
    if (service_active){
        gtk_widget_set_tooltip_text(widgets->ServiceSwitchButton,START_SERVICE_TOOLTIP_LABEL);
        gtk_image_set_from_icon_name(GTK_IMAGE(image),service_start_icon_name,GTK_ICON_SIZE_BUTTON);
        if (!error)yon_ubl_status_box_render(AUDIT_UNACTIVE_LABEL,BACKGROUND_IMAGE_FAIL_TYPE);
    } else {
        gtk_widget_set_tooltip_text(widgets->ServiceSwitchButton,STOP_SERVICE_TOOLTIP_LABEL);
        if (!error)yon_ubl_status_box_render(AUDIT_ACTIVE_LABEL,BACKGROUND_IMAGE_SUCCESS_TYPE);
        gtk_image_set_from_icon_name(GTK_IMAGE(image),service_stop_icon_name,GTK_ICON_SIZE_BUTTON);
    }
}

void yon_date_chooser_save_accept(GtkWidget *root){
    struct yon_date_chooser_option *parameter = g_object_get_data(G_OBJECT(root),"data_struct");
    char *path = (char*)gtk_combo_box_get_active_id(GTK_COMBO_BOX(parameter->DateCombo));
    if (!yon_char_is_empty(path)){
        yon_window_config_add_instant_parameter(date_format_parameter,yon_configuration_window_section,path,YON_TYPE_STRING);
    } else {
        yon_window_config_erase_instant_parameter(date_format_parameter,yon_configuration_window_section);
    }
    on_date_format_changed(NULL,main_config.widgets);
}

/**yon_main_window_complete(main_window *widgets) 
 * [EN] 
 *  
 * [RU] 
 * Функция настройки основного окна приложения. [widgets] - структура со стандартным интерфейсом. 
*/ 
main_window *yon_main_window_complete(main_window *widgets){ 
    widgets = yon_remalloc(widgets,sizeof(main_window));
    main_config.widgets = widgets;
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_path); 
    gtk_box_pack_start(GTK_BOX(widgets->InterfaceBox),yon_gtk_builder_get_widget(builder,"BoxMain"),1,1,0);
    // Custom widgets configuration 
    {
        widgets->ServiceSwitchButton = yon_gtk_builder_get_widget(builder,"ServiceSwitchButton");
        widgets->SystemButton = yon_gtk_builder_get_widget(builder,"SystemButton");
        widgets->SystemNotebook = yon_gtk_builder_get_widget(builder,"SystemNotebook");
        widgets->SystemStatisticsTerminal = yon_gtk_builder_get_widget(builder,"SystemStatisticsTerminal");
        widgets->SystemStatisticsPeriodTerminal = yon_gtk_builder_get_widget(builder,"SystemStatisticsPeriodTerminal");
        widgets->SystemFromTimeEntry = yon_gtk_builder_get_widget(builder,"SystemFromTimeEntry");
        widgets->SystemFromDateEntry = yon_gtk_builder_get_widget(builder,"SystemFromDateEntry");
        widgets->SystemFromDateButton = yon_gtk_builder_get_widget(builder,"SystemFromDateButton");
        widgets->SystemToTimeEntry = yon_gtk_builder_get_widget(builder,"SystemToTimeEntry");
        widgets->SystemToDateEntry = yon_gtk_builder_get_widget(builder,"SystemToDateEntry");
        widgets->SystemToDateButton = yon_gtk_builder_get_widget(builder,"SystemToDateButton");
        widgets->SystemPeriodApplyButton = yon_gtk_builder_get_widget(builder,"SystemPeriodApplyButton");
        widgets->SystemCallsTerminal = yon_gtk_builder_get_widget(builder,"SystemCallsTerminal");
        widgets->LoginButton = yon_gtk_builder_get_widget(builder,"LoginButton");
        widgets->LoginNotebook = yon_gtk_builder_get_widget(builder,"LoginNotebook");
        widgets->LogNotebook = yon_gtk_builder_get_widget(builder,"LogNotebook");
        widgets->LoginAttemptsTerminal = yon_gtk_builder_get_widget(builder,"LoginAttemptsTerminal");
        widgets->FailedLoginAttemptsTerminal = yon_gtk_builder_get_widget(builder,"FailedLoginAttemptsTerminal");
        widgets->FailedUserAttemptsTerminal = yon_gtk_builder_get_widget(builder,"FailedUserAttemptsTerminal");
        widgets->LogButton = yon_gtk_builder_get_widget(builder,"LogButton");
        widgets->LogsReportTerminal = yon_gtk_builder_get_widget(builder,"LogsReportTerminal");
        widgets->LogsSummaryTerminal = yon_gtk_builder_get_widget(builder,"LogsSummaryTerminal");
        widgets->FromDatetimePopover = yon_gtk_builder_get_widget(builder,"FromDatetimePopover");
        widgets->ToDatetimePopover = yon_gtk_builder_get_widget(builder,"ToDatetimePopover");
        widgets->FromDateCalendar = yon_gtk_builder_get_widget(builder,"FromDateCalendar");
        widgets->FromHoursSpin = yon_gtk_builder_get_widget(builder,"FromHoursSpin");
        widgets->FromMinutesSpin = yon_gtk_builder_get_widget(builder,"FromMinutesSpin");
        widgets->FromSecondsSpin = yon_gtk_builder_get_widget(builder,"FromSecondsSpin");
        widgets->ToDateCalendar = yon_gtk_builder_get_widget(builder,"ToDateCalendar");
        widgets->ToHoursSpin = yon_gtk_builder_get_widget(builder,"ToHoursSpin");
        widgets->ToMinutesSpin = yon_gtk_builder_get_widget(builder,"ToMinutesSpin");
        widgets->ToSecondsSpin = yon_gtk_builder_get_widget(builder,"ToSecondsSpin");
        widgets->UpdateButton = yon_gtk_builder_get_widget(builder,"UpdateButton");

        {
            int year,month,day;
            GDateTime *datetime = g_date_time_new_now_local();
            g_date_time_get_ymd(datetime,&year,&month,&day);
            gtk_calendar_select_month(GTK_CALENDAR(widgets->ToDateCalendar),month-1,year);
            gtk_calendar_select_day(GTK_CALENDAR(widgets->ToDateCalendar),day);
            gtk_calendar_select_month(GTK_CALENDAR(widgets->FromDateCalendar),month-1,year);
            gtk_calendar_select_day(GTK_CALENDAR(widgets->FromDateCalendar),day);
            g_date_time_unref(datetime);
        }

        g_signal_connect(G_OBJECT(widgets->ServiceSwitchButton),"clicked",G_CALLBACK(on_service_switch),widgets);
        g_signal_connect(G_OBJECT(widgets->SystemButton),"clicked",G_CALLBACK(on_button_clicked),widgets);
        g_signal_connect(G_OBJECT(widgets->LoginButton),"clicked",G_CALLBACK(on_button_clicked),widgets);
        g_signal_connect(G_OBJECT(widgets->LogButton),"clicked",G_CALLBACK(on_button_clicked),widgets);

        g_signal_connect(G_OBJECT(widgets->SystemFromDateButton),"clicked",G_CALLBACK(on_popover_open),widgets);
        g_signal_connect(G_OBJECT(widgets->SystemToDateButton),"clicked",G_CALLBACK(on_popover_open),widgets);
        g_signal_connect(G_OBJECT(widgets->UpdateButton),"clicked",G_CALLBACK(on_interface_update),widgets);

        g_signal_connect(G_OBJECT(widgets->FromDateCalendar),"day-selected",G_CALLBACK(on_to_date_selected),widgets);
        g_signal_connect(G_OBJECT(widgets->FromHoursSpin),"value-changed",G_CALLBACK(on_to_date_selected),widgets);
        g_signal_connect(G_OBJECT(widgets->FromMinutesSpin),"value-changed",G_CALLBACK(on_to_date_selected),widgets);
        g_signal_connect(G_OBJECT(widgets->FromSecondsSpin),"value-changed",G_CALLBACK(on_to_date_selected),widgets);

        g_signal_connect(G_OBJECT(widgets->ToDateCalendar),"day-selected",G_CALLBACK(on_to_date_selected),widgets);
        g_signal_connect(G_OBJECT(widgets->ToHoursSpin),"value-changed",G_CALLBACK(on_to_date_selected),widgets);
        g_signal_connect(G_OBJECT(widgets->ToMinutesSpin),"value-changed",G_CALLBACK(on_to_date_selected),widgets);
        g_signal_connect(G_OBJECT(widgets->ToSecondsSpin),"value-changed",G_CALLBACK(on_to_date_selected),widgets);

        g_signal_connect(G_OBJECT(widgets->SystemPeriodApplyButton),"clicked",G_CALLBACK(on_accept_period),widgets);

    // GtkWidget *StatusBoxParent = gtk_widget_get_parent(widgets->StatusBox);
    // gtk_widget_destroy(GTK_WIDGET(widgets->StatusBox));

    //     widgets->StatusBox = yon_gtk_builder_get_widget(builder,"StatusBox");
    //     gtk_box_pack_start(GTK_BOX(StatusBoxParent),widgets->StatusBox,1,1,0);
    //     gtk_box_reorder_child(GTK_BOX(StatusBoxParent),widgets->StatusBox,0);

        yon_on_entry_set_allowed_symbols(GTK_ENTRY(widgets->SystemToDateEntry),"1234567890./-");
        yon_on_entry_set_allowed_symbols(GTK_ENTRY(widgets->SystemToTimeEntry),"1234567890:");
        yon_on_entry_set_allowed_symbols(GTK_ENTRY(widgets->SystemFromDateEntry),"1234567890./-");
        yon_on_entry_set_allowed_symbols(GTK_ENTRY(widgets->SystemFromTimeEntry),"1234567890:");

        
        yon_terminal_integrated_start(widgets->SystemStatisticsTerminal,get_general_system_statistics_command);
        yon_terminal_integrated_start(widgets->SystemCallsTerminal,get_all_system_calls_command);

        yon_terminal_integrated_start(widgets->LoginAttemptsTerminal,get_login_attempts_command);
        yon_terminal_integrated_start(widgets->FailedLoginAttemptsTerminal,get_login_failed_attempts_command);
        yon_terminal_integrated_start(widgets->FailedUserAttemptsTerminal,get_user_all_failed_attempts_command);

        yon_terminal_integrated_start(widgets->LogsReportTerminal,get_log_summary_command);
        yon_terminal_integrated_start(widgets->LogsSummaryTerminal,get_generate_log_summary_command);
        gtk_widget_hide(gtk_menu_get_attach_widget(GTK_MENU(gtk_widget_get_parent(widgets->SaveMenuItem))));
        gtk_widget_hide(gtk_menu_get_attach_widget(GTK_MENU(gtk_widget_get_parent(widgets->LoadGlobalMenuItem))));

        char *path = yon_char_unite(yon_ubl_user_get_home_directory(),"/.config/",LocaleName,"/",LocaleName,".conf",NULL);
        yon_window_config_load(path);
        
        yon_ubl_settings_window_init(GTK_MENU(gtk_widget_get_parent(widgets->DocumentationMenuItem)));
        GList *menu_items = gtk_container_get_children(GTK_CONTAINER(gtk_widget_get_parent(widgets->DocumentationMenuItem)));
        for (guint i=0;i<g_list_length(menu_items);i++){
            if (i==0){
                gtk_style_context_remove_class(gtk_widget_get_style_context((GtkWidget*)g_list_nth_data(menu_items,i)),"menuitembottom");
                gtk_style_context_remove_class(gtk_widget_get_style_context((GtkWidget*)g_list_nth_data(menu_items,i)),"menuitemmiddle");
                gtk_style_context_add_class(gtk_widget_get_style_context((GtkWidget*)g_list_nth_data(menu_items,i)),"menuitemtop");
            } else if (i==g_list_length(menu_items)-1){
                gtk_style_context_remove_class(gtk_widget_get_style_context((GtkWidget*)g_list_nth_data(menu_items,i)),"menuitemmiddle");
                gtk_style_context_remove_class(gtk_widget_get_style_context((GtkWidget*)g_list_nth_data(menu_items,i)),"menuitemtop");
                gtk_style_context_add_class(gtk_widget_get_style_context((GtkWidget*)g_list_nth_data(menu_items,i)),"menuitembottom");
            } else {
                gtk_style_context_remove_class(gtk_widget_get_style_context((GtkWidget*)g_list_nth_data(menu_items,i)),"menuitembottom");
                gtk_style_context_remove_class(gtk_widget_get_style_context((GtkWidget*)g_list_nth_data(menu_items,i)),"menuitemtop");
                gtk_style_context_add_class(gtk_widget_get_style_context((GtkWidget*)g_list_nth_data(menu_items,i)),"menuitemmiddle");
            }
        }
        g_list_free(menu_items);

    widgets->menu = yon_rmb_menu_setup(gtk_widget_get_parent(widgets->LogNotebook),NULL,NULL,
    SYSTEM_AUDIT_LABEL,system_audit_icon_name,G_CALLBACK(on_button_clicked),widgets,
    USER_AUDIT_LABEL,login_audit_icon_name,G_CALLBACK(on_button_clicked),widgets,
    REPORTS_LABEL,summary_audit_icon_name,G_CALLBACK(on_button_clicked),widgets,
    NULL);
        // yon_configuration_window_add_boolean_parameter(CONFIGURATION_PARAMETER_BOOL,auditd_start_parameter,START_SERVICE_TOOLTIP_LABEL);
        // yon_configuration_window_add_boolean_parameter(CONFIGURATION_PARAMETER_BOOL,auditd_enable_parameter,ENABLE_SERVICE_TOOLTIP_LABEL);

		struct yon_configuration_window_custom_parameter *parameter = malloc(sizeof(struct yon_configuration_window_custom_parameter));
		parameter->custom_parameter_create_func = yon_date_chooser_new;
		parameter->custom_parameter_save_value_func = yon_date_chooser_save_accept;
		parameter->custom_parameter_update_func = yon_date_chooser_update;
		yon_configuration_window_add_custom_parameter(date_format_parameter,parameter);
        // yon_configuration_window_add_combo_box_parameter(CONFIGURATION_PARAMETER_COMBO_BOX,date_format_parameter,DATE_FORMAT_SETTINGS_LABEL,G_CALLBACK(on_date_format_changed),widgets,dmy_dot_format,DMY_FORMAT_LABEL,dmy_hyphen_format,DMY_MINUS_FORMAT_LABEL,dmy_slash_format,DMY_SLASH_FORMAT_LABEL,mdy_hyphen_format,MDY_FORMAT_LABEL,NULL);
        
    }
    yon_ubl_status_box_setup(widgets->StatusIcon,widgets->StatusBox,widgets->StatusLabel);
    if (getuid()!=0){
        textdomain(template_ui_LocaleName);
        yon_ubl_status_box_render(ROOT_WARNING_LABEL,BACKGROUND_IMAGE_FAIL_TYPE);
        gtk_widget_set_sensitive(widgets->ServiceSwitchButton,0);
        textdomain(LocaleName);
    } else if (system(check_audit_active_command)){
        yon_ubl_status_box_render(AUDIT_UNACTIVE_LABEL,BACKGROUND_IMAGE_FAIL_TYPE);
    } else {
        GtkWidget *image = gtk_button_get_image(GTK_BUTTON(widgets->ServiceSwitchButton));
        gtk_image_set_from_icon_name(GTK_IMAGE(image),service_stop_icon_name,GTK_ICON_SIZE_BUTTON);
        gtk_widget_set_tooltip_text(widgets->ServiceSwitchButton,STOP_SERVICE_TOOLTIP_LABEL);
    }
    yon_date_entry_set_placeholder(widgets);
    yon_root_button_setup((template_main_window*)widgets,main_config.launch_arguments,main_config.launch_size);
    return widgets;
}
int main(int argc, char *argv[]){ 
    setlocale(LC_ALL, "");
    textdomain (LocaleName);
    
    yon_ubl_connect_config((_template_config*)&main_config); 
    yon_ubl_window_init(TITLE_LABEL,TITLE_INFO_LABEL,LocaleName,CssPath,LocaleName,version_application,WIKI_LINK);
	config_str unfound = NULL;
	int size=0;
	yon_ubl_setup_arguments(argc,argv,&unfound,&size,NULL);
    gtk_init(&argc,&argv);
    template_main_window *widgets = yon_ubl_window_setup(); 

    //turn off if custom presented

    main_config.launch_arguments=yon_char_parsed_copy(argv,argc);
    main_config.launch_size=argc;
    yon_main_window_complete((main_window*)widgets);
    if (getuid()!=0){
        textdomain(template_ui_LocaleName);
        yon_ubl_status_box_render(ROOT_WARNING_LABEL,BACKGROUND_IMAGE_FAIL_TYPE);
        textdomain(LocaleName);
    }
    gtk_main();
    return 0;
}