#include <gtk/gtk.h>
#include <gtk/gtkx.h>
#include <locale.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <libintl.h>
#include <getopt.h>
#include <libintl.h>
#include <libublsettings.h>
#include <libublsettings-gtk3.h>
#include <libublsettingsui-gtk3.h>
#ifdef WEBKIT_FOUND
    #include <webkit2/webkit2.h>
#endif 
#include "ubl-strings.h"


#define _(String) gettext(String)

#define glade_path "/com/ublinux/ui/ubl-settings-audit.glade"
#define glade_confirmation_path "/com/ublinux/ui/ubl-settings-audit-confirmation.glade"
#define CssPath "/com/ublinux/css/ubl-settings-audit.css"
#define icon_path "com.ublinux.ubl-settings-audit"
#define config_path yon_char_unite(yon_ubl_user_get_home_directory(),"/.config/",LocaleName,"/",LocaleName,".conf",NULL)

#define LocalePath "/usr/share/locale" 
#define LocaleName "ubl-settings-audit"

#define yon_config_get_custom_command(target) yon_char_unite("ubconfig --source ",target," get system SERVICES_ENABLE SERVICES_DISABLE SERVICES_MASK",NULL)
#define config_get_local_command "ubconfig --source system get system SERVICES_ENABLE SERVICES_DISABLE SERVICES_MASK"
#define config_get_global_command yon_char_new("ubconfig --source global get system SERVICES_ENABLE SERVICES_DISABLE SERVICES_MASK")
#define config_get_default_command ""

#define config_get_global_only_parameters ""
#define config_get_local_only_parameters ""

#define get_general_system_statistics_command "aureport"

#define get_system_period_command(start_date,start_time,end_date,end_time) yon_char_unite("LC_TIME=en_US.UTF-8 aureport ",\
!yon_char_is_empty(start_date)?"--start ":"",!yon_char_is_empty(start_date)?start_date:""," ",!yon_char_is_empty(start_date)?start_time:"",\
!yon_char_is_empty(end_time)?" --end ":"",!yon_char_is_empty(end_time)?end_date:""," ",!yon_char_is_empty(end_time)?end_time:"",NULL)

#define get_system_today_command "aureport --start today"
#define get_all_system_calls_command "aureport -s"

#define get_login_attempts_command "aureport -au"
#define get_login_failed_attempts_command "aureport --login --summary -i"
#define get_user_all_failed_attempts_command "aureport -u --failed --summary -i"

#define get_log_summary_command "aureport -x"
#define get_generate_log_summary_command "aureport -x --summary"

#define date_format_parameter "DateFormat"
#define auditd_start_parameter "AuditStartFormat"
#define auditd_enable_parameter "AuditEnableFormat"

#define dmy_slash_format "%d/%m/%Y"
#define dmy_hyphen_format "%d-%m-%Y"
#define dmy_dot_format "%d.%m.%Y"
#define mdy_hyphen_format "%m-%d-%Y"

#define check_audit_active_command "systemctl status auditd.service --no-pager"

#define system_audit_icon_name "com.ublinux.ubl-settings-audit.system-symbolic"
#define login_audit_icon_name "com.ublinux.ubl-settings-audit.login-symbolic"
#define summary_audit_icon_name "com.ublinux.ubl-settings-audit.summary-symbolic"
#define service_start_icon_name "com.ublinux.libublsettingsui-gtk3.play-symbolic"
#define service_stop_icon_name "com.ublinux.libublsettingsui-gtk3.stop-symbolic"

#define service_start_status_icon_name "com.ublinux.ubl-settings-audit.active-symbolic"
#define service_stop_status_icon_name "com.ublinux.ubl-settings-audit.inactive-symbolic"


#define service_commands_path "resource:///com/ublinux/csv/service-commands.csv"

typedef char* string;

__attribute__((unused)) static \
string version_application;


typedef struct {
    template_window_fields
    GtkWidget *ServiceSwitchButton;
    GtkWidget *UpdateButton;
    GtkWidget *SystemButton;
    GtkWidget *SystemNotebook;
    GtkWidget *SystemStatisticsTerminal;
    GtkWidget *SystemStatisticsPeriodTerminal;
    GtkWidget *SystemFromTimeEntry;
    GtkWidget *SystemFromDateEntry;
    GtkWidget *SystemFromDateButton;
    GtkWidget *SystemToTimeEntry;
    GtkWidget *SystemToDateEntry;
    GtkWidget *SystemToDateButton;
    GtkWidget *SystemPeriodApplyButton;
    GtkWidget *FromDatetimePopover;
    GtkWidget *FromDateCalendar;
    GtkWidget *FromHoursSpin;
    GtkWidget *FromMinutesSpin;
    GtkWidget *FromSecondsSpin;
    GtkWidget *ToDatetimePopover;
    GtkWidget *ToDateCalendar;
    GtkWidget *ToHoursSpin;
    GtkWidget *ToMinutesSpin;
    GtkWidget *ToSecondsSpin;
    GtkWidget *SystemCallsTerminal;

    GtkWidget *LoginButton;
    GtkWidget *LoginNotebook;
    GtkWidget *LoginAttemptsTerminal;
    GtkWidget *FailedLoginAttemptsTerminal;
    GtkWidget *FailedUserAttemptsTerminal;

    GtkWidget *LogButton;
    GtkWidget *LogNotebook;
    GtkWidget *LogsReportTerminal;
    GtkWidget *LogsSummaryTerminal;

    rmb_menu_window *menu;
} main_window;

typedef struct {
    template_config_fields
    int save_config;
    config_str launch_arguments;
    int launch_size;
    main_window *widgets;
} config;

extern config main_config;

typedef struct {
    GtkWidget *Window;
    
    GtkWidget *StatusBox;
    GtkWidget *HeadLabel;
    GtkWidget *TitleLabel;
    GtkWidget *ActiveLabel;
    GtkWidget *EnableLabel;
    GtkWidget *ActiveImage;
    GtkWidget *EnableImage;
    GtkWidget *StartButton;
    GtkWidget *StopButton;
    GtkWidget *RestartButton;
    GtkWidget *StatusButton;
    GtkWidget *EnableButton;
    GtkWidget *DisableButton;
    GtkWidget *UpdateButton;

} confirmation_window;

enum service_commands {
    COMMAND_NAME_COL = 0,
    COMMAND_DESC_COL,
    COMMAND_START_COL,
    COMMAND_STOP_COL,
    COMMAND_ENABLE_COL,
    COMMAND_DISABLE_COL,
    COMMAND_STATUS_COL,
    COMMAND_STATUS_ACTIVE_COL,
    COMMAND_STATUS_ENABLE_COL
};

// typedef struct {
//     GtkWidget *Window;
//     GtkWidget *HeaderLabel;

//     GtkWidget *AlwaysOpenCheck;

//     GtkWidget *CloseButton;
//     GtkWidget *AcceptButton;
// } documentation_confirmation_window;

struct confirmation_result {
    int result;
};

void yon_load_proceed(YON_CONFIG_TYPE type);

void config_init();

main_window *yon_main_window_complete(main_window *widgets);

void on_button_clicked(GtkWidget *self, main_window *widgets);

void on_to_date_selected(GtkWidget *self,main_window *window);
void on_popover_open(GtkWidget *self, main_window *widgets);

void on_accept_period(GtkWidget *self, main_window *widgets);
void on_date_format_changed(GtkWidget *, main_window *widgets);
void yon_date_tooltip_update(main_window *widgets);
void yon_interface_update(main_window *widgets);
void on_interface_update(GtkWidget *,main_window *widgets);
char *yon_get_date_string(char *raw_date_string);
void yon_date_entry_set_placeholder(main_window *widgets);
void on_service_switch(GtkWidget *, main_window *widgets);
confirmation_window *yon_confirmation_window_new(struct confirmation_result *result);
void on_closed(GtkWidget *self);
void on_service_restart_clicked(GtkWidget *, confirmation_window *window);
void on_service_status_clicked(GtkWidget *, confirmation_window *window);
void on_service_disable_clicked(GtkWidget *, confirmation_window *window);
void on_service_enable_clicked(GtkWidget *, confirmation_window *window);
void on_service_stop_clicked(GtkWidget *, confirmation_window *window);
void on_service_start_clicked(GtkWidget *, confirmation_window *window);
void on_service_update_clicked(GtkWidget *, confirmation_window *window);
void yon_date_chooser_save_accept(GtkWidget *root);