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


#define _(String) gettext(String)

#define glade_path "/com/ublinux/ui/ubl-settings-environment.glade"
#define glade_path_editor "/com/ublinux/ui/ubl-settings-environment-edit.glade"
#define glade_path_env_files "/com/ublinux/ui/ubl-settings-environment-env-files.glade"
#define glade_path_inspector "/com/ublinux/ui/ubl-settings-environment-inspect.glade"
#define glade_path_add "/com/ublinux/ui/ubl-settings-environment-add.glade"
#define CssPath "/com/ublinux/css/ubl-settings-environment.css"
#define icon_path "com.ublinux.ubl-settings-environment"
#define config_path yon_char_unite(yon_ubl_user_get_home_directory(),"/.config/",LocaleName,"/",LocaleName,".conf",NULL)
#define config_envs_path yon_char_unite(yon_ubl_user_get_home_directory(),"/.config/",LocaleName,"/",LocaleName,".csv",NULL)

#define get_vars_command "printenv"
#define LocalePath "/usr/share/locale" 
#define LocaleName "ubl-settings-environment"

#define add_icon_name "com.ublinux.libublsettingsui-gtk3.increase-symbolic"
#define remove_icon_name "com.ublinux.libublsettingsui-gtk3.decrease-symbolic"
#define edit_icon_name "document-edit-symbolic"

#define get_descriptions_command "resource:///com/ublinux/csv/descriptions.csv"
#define get_min_max_uid_command "grep -E '^UID_MIN|^UID_MAX' /etc/login.defs| sed -e 's/\t/ /g' -e 's/  */ /g'|cut -d' ' -f2"
#define environment_paths_path "resource:///com/ublinux/csv/env_files.csv"

#define ENVIRONMENT(user,variable) yon_char_unite("ENVIRONMENT[",user,":",variable,"]",NULL)
#define ENVIRONMENT_ALL "ENVIRONMENT[*]"

#define ENVIRONMENT_SECTION "system"

#define config_get_command(source) yon_char_unite("ubconfig --source ",source," get system ENVIRONMENT[*]",NULL)

#define ENVIRONMENT_get_command(target) yon_char_unite("ubconfig --source global get [system] ENVIRONMENT[",target,"]",NULL)

typedef char* string;
string version_application;

char *local;

typedef struct {
    template_config_fields;
    int save_config;
    config_str launch_arguments;
    int launch_size;

    config_str env_strings;
    int env_size;
    config_str user_list;
    int user_size;

    gboolean root_permissions;

    dictionary *env_descriptions;

    GtkListStore *env_variables;
} config;

typedef struct {
    template_window_fields

    GtkWidget *MainTree;
    GtkListStore *list;
    GtkWidget *InspectButton;
    GtkWidget *AddButton;
    GtkWidget *RemoveButton;
    GtkWidget *EditButton;

    GtkWidget *RootMenuItem;
    GtkWidget *EditMenuItem;

    rmb_menu_window *menu;
    // Custom 
} main_window;


typedef struct {
    GtkWidget *Window;

    GtkWidget *HatText;
    GtkWidget *HeaderText;
    GtkWidget *InfoText;
    GtkWidget *AlwaysOpenCheck;

    GtkWidget *CloseButton;
    GtkWidget *AcceptButton;
} documentation_confirmation_window;


typedef struct {
    GtkWidget *Window;

    GtkWidget *Tree;
    GtkListStore *list;
    GtkWidget *EditButton;
    rmb_menu_window *menu;

} inspector_window;


typedef struct {
    GtkWidget *Window;

    GtkWidget *EditingEntry;
    GtkWidget *CancelButton;
    GtkWidget *AcceptButton;
    GtkWidget *HeaderLabel;
    char *path;
} editor_window;

typedef struct {
    GtkWidget *Window;

    GtkWidget *StatusBox;
    GtkWidget *VariableEntry;
    GtkWidget *ValueEntry;
    GtkWidget *SourceCombo;
    GtkWidget *SourceUserCombo;
    GtkWidget *DescriptionEntry;
    GtkWidget *HeaderTopicLabel;

    GtkWidget *AcceptButton;
    GtkWidget *CancelButton;
} add_window;

typedef struct {
    GtkListStore *liststore1;
    GtkWidget *Window;
    GtkWidget *AddButton;
    GtkWidget *RemoveButton;
    GtkWidget *ConfigureButton;
    GtkWidget *MainTree;
    GtkWidget *CancelButton;
    GtkWidget *AcceptButton;
    GtkCellRenderer *TextCell;
} env_files_window;

main_window *setup_window();

//signal emmit handlers - no header initialization

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

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

/**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_link(GtkWidget *self, char* uri, gpointer user_data)
 * [EN]
 * Signal for hadnling AboutDialog links.
 * Connect to "activate-link" signal.
 * [self] is AboutDialog window;
 * [uri] is activated link;
 * [user_data] is pointer for user data, hasn't used in standard handler;
 * [RU]
 * Функция для обработки сигнала нажатия на ссылку окна AboutDialog.
 * Присоединять к сигналу "activate-link".
 * [self] - окно AboutDialog;
 * [uri] - ссылка, по которой совершается переход;
 * [user_data] - указатель на любые другие данные, не используется в стандартном обработчике;
*/
void on_link(GtkWidget *self, char* uri, gpointer user_data);

/**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();

//functions

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

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

void yon_config_save_global_local();

void yon_config_save_local();

void yon_config_save_global();

// standard functions
void on_edit_accept(GtkWidget *self, editor_window *window);

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

void on_variable_save(GtkWidget *self,dictionary *dict);

void on_source_changed(GtkWidget *self, add_window *window);

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

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

void on_variable_edit_save(GtkWidget *self,dictionary *dict);

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

void on_inspecor_edit(GtkWidget *self, dictionary *dict);

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

void on_root_get_root(char *argline);

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

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

void on_env_files_add(GtkWidget *self, env_files_window *window);

void on_env_files_edit(GtkWidget *self, env_files_window *window);

void on_env_files_remove(GtkWidget *self, env_files_window *window);

void on_cell_editing_done(GtkCellRendererText *self, char *path, char *new_text, env_files_window *window);

void on_cell_editing_canceled(GtkCellRenderer *self, env_files_window *window);

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

void yon_menu_update(main_window *widgets);

void on_env_files_save(GtkWidget *self, dictionary *dict);

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

void on_variable_activate(GtkWidget *self, GtkTreePath *, GtkTreeViewColumn*, main_window *widgets);

add_window *yon_add_window_new();

void on_inspector_destroy(GtkWidget *,main_window *widgets);

void on_inspecor_edit_activate(GtkWidget *, GtkTreePath *,GtkTreeViewColumn *,dictionary *dict);

void config_init();
