#include <gtk/gtk.h>
#include <gtk/gtkx.h>
#include <libublsettings.h>
#include <libublsettings-gtk3.h>
#include <locale.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <libintl.h>
#include <getopt.h>
#include <libintl.h>
#ifdef WEBKIT_INCLUDE
    #include <webkit2/webkit2.h>
#endif
#define ui_WIKI_LINK "https://wiki.ublinux.ru/ru/Программное_обеспечение/Программы_и_утилиты/Все/ublsettingsui-gtk3"

#define _(String) gettext(String)

#define ui_glade_path "/com/ublinux/ui/libublsettingsui-gtk3.glade"
#define ui_glade_path_documentation "/com/ublinux/ui/libublsettingsui-gtk3-documentation.glade"
#define ui_glade_path_about "/com/ublinux/ui/libublsettingsui-gtk3-about.glade"
#define ui_glade_path_saving "/com/ublinux/ui/libublsettingsui-gtk3-saving.glade"
#define ui_banner_path "/com/ublinux/images/libublsettingsui-gtk3-banner.png"
#define ui_CssPath "/com/ublinux/css/libublsettingsui-gtk3.css"
#define ui_config_path yon_char_unite(yon_ubl_user_get_home_directory(),"/.config/",template_app_information.app_tech_name,"/",template_app_information.app_tech_name,".conf",NULL)

#define remove_config_dir_command yon_char_unite("rm -rfd ", yon_ubl_user_get_home_directory(),"/.config/",template_app_information.app_tech_name,"/",NULL)

#ifndef UBL_UI
    #define VERSION_LABEL yon_char_unite(_("Version:")," ",!yon_char_is_empty(template_app_information.app_version)?template_app_information.app_version:"","\n",NULL)
    #define HELP_LABEL(rest) yon_char_unite(template_app_information.app_tech_name,_(" version:")," ", !yon_char_is_empty(template_app_information.app_version)?template_app_information.app_version:"","\n",template_app_information.app_title,"\n",_("Usage:"), " ",template_app_information.app_tech_name," ",_("[OPTIONS]"),"\n",_("Options:"),"\n\t--help, -h\t\t\t",_("Show this help"),"\n\t--version, -V\t\t\t",_("Show package version"),"\n\t--lock-help\t\t\t",_("Lock this help menu"),"\n\t--lock-save\t\t\t",_("Lock configuration saving"),"\n\t--lock-save-local\t\t",_("Lock local configration saving"),"\n\t--lock-save-global\t\t",_("Lock global configration saving"),"\n\t--lock-load-global\t\t",_("Lock global configration loading"),"\n\t--clear-config\t\t\t",_("Reset application settings"),"\n",!yon_char_is_empty(rest)?rest:NULL,NULL)

    #define template_ui_LocalePath "/usr/share/locale" 
    #define template_ui_LocaleName "libublsettingsui-gtk3"

    #define SUCCESS_LABEL _("Operation succeeded")
    #define ROOT_WARNING_LABEL _("Warning! Application was launched without root - root-dependent actions are locked")

    #define ABOUT_LABEL _("About")
    #define DOCUMENTATION_LABEL _("Documentation")

    #define SAVE_LOCAL_LABEL _("Save to local configuration")
    #define SAVE_GLOBAL_LABEL _("Save to global configuration")
    #define SAVE_CONFIGURATION_LABEL _("Save configuration")
    #define SAVE_LABEL _("Save")

    #define LOAD_LOCAL_LABEL _("Load local configuration")
    #define LOAD_GLOBAL_LABEL _("Load global configuration")
    #define LOAD_LABEL _("Load")

    #define CANCEL_LABEL _("Cancel")
    #define ACCEPT_LABEL _("Accept")

    #define HELP_TITLE_LABEL _("Would you like to read documentation in the Web?")
    #define HELP_INFO_LABEL _("You will be redirected to documentation website where documentation is\ntranslated and supported by community.")
    #define HELP_ALWAYS_OPEN_LABEL _("Always redirect to online documentation")
    #define OPEN_HELP_LABEL _("Open documentation")
    #define PROJECT_HOME_LABEL _("Project Home Page")
    #define NOTHING_CHOSEN_LABEL _("Nothing were chosen")
    #define AUTHORS_LABEL _(L"Copyright © 2022 - 2023, UBSoft LLC")


    #define GLOBAL_LOAD_SUCCESS_LABEL _("Global configuration loading succeeded.")
    #define LOCAL_LOAD_SUCCESS_LABEL _("Local configuration loading succeeded.")
    #define LOAD_FAILED_LABEL _("Config loading failed")

    #define GLOBAL_LOCAL_SAVE_SUCCESS_LABEL _("Local and global configuration saving succeeded.")
    #define GLOBAL_SAVE_SUCCESS_LABEL _("Global configuration saving succeeded.")
    #define LOCAL_SAVE_SUCCESS_LABEL _("Local configuration saving succeeded.")

    #define PARAMETER_LABEL _("Parameter")
    #define OLD_VALUE_LABEL _("Old value")
    #define NEW_VALUE_LABEL _("New value")
    #define COMPARE_VALUE_LABEL _("Value\n(Old/New)")

    #define NOTHING_TO_SAVE_LABEL _("Nothing to save")
    #define SAVING_LOCAL_STATE_LABEL _("Saving into local configuration")
    #define SAVING_GLOBAL_STATE_LABEL _("Saving into global configuration")
    #define SAVING_GLOBAL_LOCAL_STATE_LABEL _("Saving into global and local configuration")

    #define SYSTEMBOOT_STATEMODE "SYSTEMBOOT_STATEMODE"
    #define SYSTEMBOOT_STATEMODE_SECTION "[system]"

    #define SAVE_MODE_FULL_LABEL _("Full saving mode")
    #define SAVE_MODE_MODULE_LABEL _("Saving into module")
    #define SAVE_MODE_SANDBOX_LABEL _("Sandbox mode")
    #define SAVE_MODE_RAM_HOME_LABEL _("Sandbox with profile saving")
    #define SAVE_MODE_HDD_LABEL _("HDD sandbox")
    #define SAVE_MODE_HDD_HOME_LABEL _("HDD sandbox with profile saving")
#endif

#define SAVE_MODE_GET_COMMAND "ubconfig --raw --source system get [config] SYSTEMBOOT_STATEMODE"

typedef enum SAVE_MODE_TYPE {
    SAVE_MODE_FULL,
    SAVE_MODE_MODULE,
    SAVE_MODE_SANDBOX,
    SAVE_MODE_RAM_HOME,
    SAVE_MODE_HDD,
    SAVE_MODE_HDD_HOME,
} SAVE_MODE_TYPE;

char *yon_ubl_save_mode_get_icon_name(SAVE_MODE_TYPE type);

SAVE_MODE_TYPE yon_ubl_save_mode_get_type(char *type);

typedef struct {
    GtkWidget *interface;

    char *app_locale;
    char *css_path;
    
    char *app_title;
    char *app_description;

    char *app_tech_name;
    char *app_version;
    char *wiki_link;
    
    gboolean always_open_documentation;
} template_app_info;
static template_app_info template_app_information;

typedef struct {
    int socket_id;
    int load_socket_id;
    int save_socket_id;
    
    int lock_help;
    int lock_save_local;
    int lock_save_global;
    int lock_load_global;
    int lock_load_system;

    int load_mode;

    int always_open_documentation;
} _template_config;
static _template_config *template_config;

typedef struct {
    GtkWidget *Window;
    GtkWidget *HeadLabel;
    GtkWidget *PlugBox;

    GtkWidget *HeadOverlay;
    GtkWidget *HeadImage;
    GtkWidget *HeadBox;
    GtkWidget *HeadTitleLabel;
    GtkWidget *HeadInfoLabel;

    GtkWidget *StatusBox;
    GtkWidget *StatusIcon;
    GtkWidget *StatusLabel;

    GtkWidget *SaveMenuItem;
    GtkWidget *SaveGlobalMenuItem;
    GtkWidget *SaveLocalMenuItem;
    GtkWidget *RightBox;

    GtkWidget *LoadGlobalMenuItem;
    GtkWidget *LoadLocalMenuItem;
    GtkWidget *LeftBox;

    GtkWidget *DocumentationMenuItem;
    GtkWidget *AboutMenuItem;

    GtkWidget *InterfaceBox;
} template_main_window;


typedef struct {
    GtkWidget *Window;
    GtkWidget *HeaderLabel;

    GtkWidget *AlwaysOpenCheck;

    GtkWidget *CloseButton;
    GtkWidget *AcceptButton;
} template_documentation_confirmation_window;

typedef struct {
    GtkWidget *Window;
    GtkWidget *HeaderTopic;
    GtkWidget *HeaderImage;
    GtkWidget *StatusBox;
    GtkWidget *ParametersTree;
    GtkCellRenderer *ToggleCell;
    GtkWidget *CancelButton;
    GtkWidget *SaveButton;
    GtkListStore *list;
    GtkTreeModel *filteredModel;
    GtkTreeViewColumn *OldValueColumn;
    GtkCellRenderer *OldValueCell;
    GtkTreeViewColumn *NewValueColumn;
    GtkCellRenderer *NewValueCell;
    YON_CONFIG_TYPE type;
} template_saving_window;

typedef struct {
    void (*function)(void*,void*);
    void *data;
    char *action_text;
} dialog_confirmation_data;

int yon_confirmation_dialog_call(GtkWidget *self,dialog_confirmation_data *data);

template_saving_window *yon_save_proceed(char *path,YON_CONFIG_TYPE type, ...);

/**yon_open_browser(GtkWidget *self, char *link)
 * [EN]
 * Opens browser with [link] link.
 * [RU]
 * Открывает браузер с [link] ссылкой.
*/
void yon_open_browser(GtkWidget *self, char *link);

/**yon_ubl_connect_config(_template_config *config)
 * [EN]
 * 
 * [RU]
 * Присоединяет конфиг основной утилиты (struct config) к стандартному интерфейсу. 
*/
int yon_ubl_connect_config(_template_config *config);

/**yon_ubl_window_init(int argc, char *argv[])
 * [EN]
 * 
 * [RU]
 * Создаёт и настраивает основное окно приложения.
*/
template_main_window *yon_ubl_window_setup();

/**yon_ubl_window_setup(GtkWidget *interface, char *app_title, char *app_description, char *locale, char *css, char *tech_name, char *version, char *wiki)
 * [EN]
 * 
 * [RU]
 * Установить важные значения для конфига;
 * [app_title] - оботражаемое имя утилиты;
 * [app_description] - отображаемое описание утилиты;
 * [css] - путь к ресурсу css файла;
 * [tech_name] - техническое имя утилиты (ubl-settings-)
 * [version] - строка с версией приложения;
 * [wiki] - ссылка на вики страницу; 
*/
int yon_ubl_window_init(char *app_title, char *app_description, char *locale, char *css, char *tech_name, char *version, char *wiki);

/**on_about()
 * [EN]
 * Function for setting up and showing AboutDialog.
 * Connect it to "activate" signal of Documentation MenuItem.
 * [RU]
 * Функиця для настройки и показа окна AboutDialog.
 * Присоединять к сигналу "activate" кнопки справки типа MenuItem.
*/
void on_about(GtkWidget *self, char *version_application);

/**on_open_documentation_confirmation(GtkWidget *self, char *link)
 * [EN]
 * Opens confirmation window for [link] link.
 * [RU]
 * Открывает окно подтверждения перехода по ссылке [link].
*/
void on_open_documentation_confirmation(GtkWidget *self, char *link);

/**on_subwindow_close(GtkWidget *self)
 * [EN]
 * Closes window in which [self] is contained.
 * [RU]
 * Закрывает окно, в котором расположен виджет [self].
*/
void on_subwindow_close(GtkWidget *self);

int yon_ubl_setup_arguments(int argc, char *argv[],config_str *unfound_arguments, int *arguments_size, char *additional_options_output);