#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 <time.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-datetime.glade"
#define CssPath "/com/ublinux/css/ubl-settings-datetime.css"
#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-datetime"

#define icon_name "com.ublinux.ubl-settings-datetime"

#define config_get_command(target) yon_char_unite("ubconfig --source ",target," get clock HWCLOCK_SYNC ZONE -- get network NTPSERVERS",NULL)
#define config_get_default_command ""

#define config_get_global_only_parameters ""
#define config_get_local_only_parameters ""

#define get_ntp_default_command "ubconfig --default get [network] NTPSERVERS_DEFAULT"
#define get_ntp_ru_command "ubconfig --default get [network] NTPSERVERS_RU"

#define NTPSERVERS_parameter "NTPSERVERS"
#define NTPSERVERS_parameter_command "ubconfig --source global get network NTPSERVERS"
#define NTPSERVERS_parameter_raw_command "ubconfig --source global --raw get network NTPSERVERS"
#define HWCLOCK_SYNC_parameter "HWCLOCK_SYNC"
#define HWCLOCK_SYNC_parameter_command "ubconfig --source global get clock HWCLOCK_SYNC"
#define ZONE_parameter "ZONE"
#define ZONE_parameter_command "ubconfig --source global get clock ZONE"

#define time_sync_command(time) yon_char_unite("pkexec date +%H:%M:%S -s \"",time,"\"",NULL)
#define date_sync_command(date) yon_char_unite("pkexec date -s \"",date,"\"",NULL)

#define hardware_datetime_sync_command "hwclock --systohc"

#define zone_path "/usr/share/zoneinfo/"

#define date_format_parameter "DateFormat"

typedef char* string;

__attribute__((unused)) static \
string version_application;

typedef struct {
    template_window_fields
    GtkWidget *HourSpin;
    GtkWidget *MinuteSpin;
    GtkWidget *DateEntry;
    GtkWidget *DateButton;
    GtkWidget *SyncButton;
    GtkWidget *SyncHardwareButton;
    GtkWidget *DHCPCombo;
    GtkWidget *NTPEntry;
    GtkWidget *TimeSyncTypeCombo;
    GtkWidget *RegionCombo;
    GtkWidget *ZoneCombo;

    GtkListStore *zone_list;

    GtkWidget *DatePopover;
    GtkWidget *DateCalendar;
    GtkWidget *DateAcceptButton;
    GtkWidget *DateCancelButton;
} main_window;

typedef struct {
    template_config_fields

    int save_config;
    char *ntp_default;
    main_window *widgets;
} config;

extern config main_config;

typedef struct {
    GtkWidget *Window;
    GtkWidget *HeaderLabel;

    GtkWidget *AlwaysOpenCheck;

    GtkWidget *CloseButton;
    GtkWidget *AcceptButton;
} documentation_confirmation_window;

void yon_load_proceed(YON_CONFIG_TYPE type);

void config_init();

void yon_main_window_complete(main_window **widgets);

void on_region_changed(GtkComboBox *self, main_window *widgets);

void yon_interface_update(main_window *widgets);

gboolean on_sync_error(void*);
gboolean on_hardware_operation_success(void*);

void *yon_sync_hardware_thread_start(void*);
void *yon_sync_thread_start(void*);

void on_sync_hardware_clicked();
void on_sync_clicked(GtkWidget *, main_window *widgets);

void on_ntp_sync(GtkWidget *, main_window *widgets);

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

int check_input(main_window *widgets);

void yon_load_proceed(YON_CONFIG_TYPE type);
void on_config_local_load(GtkWidget *self,main_window *widgets);
void on_config_global_load(GtkWidget *self,main_window *widgets);
void on_config_custom_load(GtkWidget *self,main_window *widgets);
void on_config_global_local_save();
void on_config_local_save();
void on_config_global_save();
void on_config_custom_save(GtkWidget *self, main_window *widgets);

void on_date_clicked(GtkWidget *,main_window *widgets);

void on_date_accept(GtkWidget *,main_window *widgets);

void on_date_cancel(GtkWidget *, main_window *widgets);

config_str yon_char_parsed_localize(config_str parsed, int size);
config_str yon_char_parsed_sort(config_str parsed, int size);

void on_save_done(main_window *widgets, config_str output, int size);

void on_date_format_changed(GtkWidget *, main_window *widgets);
void yon_date_tooltip_update(main_window *widgets);
gboolean on_operation_success(void*);
void yon_date_chooser_save_accept(GtkWidget *root);
char *yon_date_convert_to_pattern(const char *date_string);