#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 <json-c/json.h> 
#include <sys/statvfs.h>
#include <libublsettings.h>
#include <libublsettings-gtk3.h>
#ifdef WEBKIT_FOUND 
    #include <webkit2/webkit2.h> 
#endif  
#include "ubl-strings.h" 
 
#define WIKI_LINK "https://wiki.ublinux.ru/ru/Программное_обеспечение/Программы_и_утилиты/Все/ubinstall-gtk-output" 
 
#define _(String) gettext(String) 
 
#define glade_path "/com/ublinux/ui/ubinstall-gtk.glade"
#define glade_path_ubinstall_keyboard "/com/ublinux/ui/ubinstall-gtk-keyboard.glade"
#define glade_path_ubinstall_language "/com/ublinux/ui/ubinstall-gtk-language.glade"
 
#define CssPath "/com/ublinux/css/ubinstall-gtk.css" 
#define config_path yon_char_unite(yon_ubl_user_get_home_directory(),"/.config/",LocaleName,"/",LocaleName,".conf",NULL) 
#define icon_path "com.ublinux.ubinstall-gtk-gui"

#define LocalePath "/usr/share/locale"  
#define LocaleName "ubinstall-gtk" 
 
#define clear_config_both_command "ubconfig remove TEMP_SECTION TEMP_PATAMETER" 
#define clear_config_global_command "ubconfig --target global remove TEMP_SECTION TEMP_PATAMETER" 
#define clear_config_local_command "ubconfig --target system remove TEMP_SECTION TEMP_PATAMETER" 
 
#define regions_path "/com/ublinux/images/map-time-zone.png"
#define keyboard_path "/com/ublinux/images/keyboard.png"

#define licence_path "/usr/share/ublinux/agreement/EULA.txt"

#define languages_path "/usr/share/ubinstall-gtk/csv/locales.csv"

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

#define additional_software_path "resource:///com/ublinux/csv/modules.csv"

#define slide_0_path "/com/ublinux/images/slide-0.png"

#define slide_repeat_path "/com/ublinux/images/slide-1.png", \
"/com/ublinux/images/slide-2.png", \
"/com/ublinux/images/slide-3.png", \
"/com/ublinux/images/slide-4.png", \
"/com/ublinux/images/slide-5.png", \
"/com/ublinux/images/slide-6.png", \
"/com/ublinux/images/slide-7.png", \
"/com/ublinux/images/slide-8.png", \
"/com/ublinux/images/slide-9.png", \
"/com/ublinux/images/slide-10.png", \
"/com/ublinux/images/slide-11.png", \
"/com/ublinux/images/slide-12.png", \
NULL

#define get_models_command "sed '/<\\/modelList>/q' /usr/share/X11/xkb/rules/base.xml | grep -E '^ {8,8}<name>|^ {8,8}<description>'|sed -e 's/  *<name>//g' -e 's,</name>,,g' -e 's/  *<description>//g' -e 's,</description>,,g'"

#define check_is_password_hash(password) system(yon_char_unite("/lib/ublinux/functions is_hash_password '", password,"'",NULL))

#define get_layouts_command "xkbcli list --load-exotic | awk \"layout && /description:/ {match(\\$0,/: *(.*)/,matches);description=matches[1];printf \\\"%s|%s\\n\\\",layout,description;layout=\\\"\\\"} /layout:/ {match(\\$0, /: *'([^']+)'/,matches);l=matches[1];if (layouts[l]) next;layout=layouts[l]=l}\" | sort -u"
#define get_layouts_local_command(layout) yon_char_unite("xkbcli list --load-exotic | awk -v layout=\"",layout,"\" \"BEGIN {layout_pattern = sprintf(\\\"^ *- *layout: *'%s'\\\",layout);matched=0} matched && /variant:/ {match(\\$0, /: *'([^']+)'/, matches);variant = matches[1]} matched && /description:/ {match(\\$0, /: *(.+)/, matches);description = matches[1]} matched && /^ *-/{matched=0; if (variant) printf \\\"%s|%s\\n\\\",variant,description} \\$0 ~ layout_pattern {matched=1;variant=\\\"\\\";description=\\\"\\\";next}\" | sort -u",NULL)
#define get_devices_command "lsblk --noheadings --nodeps -Jo PATH,SIZE,MODEL,VENDOR,SERIAL --exclude 7,253"
#define get_parts_and_devices_command "lsblk --noheadings -Jo TYPE,PATH,SIZE,FSTYPE,LABEL,PARTLABEL,MOUNTPOINT,FSUSED,FSUSE% --exclude 7,253"

#define AUTOINSTALL_TYPE_INSTALL "AUTOINSTALL[type_install]"
#define AUTOINSTALL_DEVICE "AUTOINSTALL[device]"
#define AUTOSTART_PARTS "AUTOINSTALL[part]"

#define user_name_parameter "AUTOINSTALL[user_name]"
#define user_name_parameter_command "ubconfig --source global get [autoinstall] AUTOINSTALL[user_name]"
#define user_gecos_parameter "AUTOINSTALL[user_gecos]"
#define user_gecos_parameter_command "ubconfig --source global get [autoinstall] AUTOINSTALL[user_gecos]"
#define user_password_parameter "AUTOINSTALL[user_password]"
#define user_password_parameter_command "ubconfig --source global get [autoinstall] AUTOINSTALL[user_password]"
#define root_password_parameter "AUTOINSTALL[root_password]"
#define root_password_parameter_command "ubconfig --source global get [autoinstall] AUTOINSTALL[root_password]"
#define autologin_parameter "AUTOINSTALL[autologin]"
#define autologin_parameter_command "ubconfig --source global get [autoinstall] AUTOINSTALL[autologin]"
#define xkbmodel_parameter "AUTOINSTALL[xkbmodel]"
#define xkbmodel_parameter_command "ubconfig --source global get [autoinstall] AUTOINSTALL[xkbmodel]"
#define xkblayout_parameter "AUTOINSTALL[xkblayout]"
#define xkblayout_parameter_command "ubconfig --source global get [autoinstall] AUTOINSTALL[xkblayout]"
#define xkbvariant_parameter "AUTOINSTALL[xkbvariant]"
#define xkbvariant_parameter_command "ubconfig --source global get [autoinstall] AUTOINSTALL[xkbvariant]"
#define xkboptions_parameter "AUTOINSTALL[xkboptions]"
#define xkboptions_parameter_command "ubconfig --source global get [autoinstall] AUTOINSTALL[xkboptions]"
#define hostname_parameter "AUTOINSTALL[hostname]"
#define hostname_parameter_command "ubconfig --source global get [autoinstall] AUTOINSTALL[hostname]"
#define zone_parameter "AUTOINSTALL[hostname]"
#define zone_parameter_command "ubconfig --source global get [autoinstall] AUTOINSTALL[hostname]"
#define lang_parameter "AUTOINSTALL[hostname]"
#define lang_parameter_command "ubconfig --source global get [autoinstall] AUTOINSTALL[hostname]"
#define locale_parameter "AUTOINSTALL[hostname]"
#define locale_parameter_command "ubconfig --source global get [autoinstall] AUTOINSTALL[hostname]"
#define device_format_parameter "AUTOINSTALL[device_format]"
#define device_format_parameter_command "ubconfig --source global get [autoinstall] AUTOINSTALL[device_format]"
#define device_label_parameter "AUTOINSTALL[device_label]"
#define device_label_parameter_command "ubconfig --source global get [autoinstall] AUTOINSTALL[device_label]"
#define part_size_parameter "AUTOINSTALL[part_size]"
#define part_size_parameter_command "ubconfig --source global get [autoinstall] AUTOINSTALL[part_size]"
#define part_type_parameter "AUTOINSTALL[part_type]"
#define part_type_parameter_command "ubconfig --source global get [autoinstall] AUTOINSTALL[part_type]"

#define save_config_command(parameters) yon_char_append("pkexec ubconfig set [autoinstall] ",parameters)

#define AUTOINSTALL_TYPE_INSTALL_command "ubconfig --source global get autoinstall AUTOINSTALL[type_install]"
#define AUTOINSTALL_DEVICE_command "ubconfig --source global get autoinstall AUTOINSTALL[device]"
#define AUTOSTART_PARTS_command "ubconfig --source global get autoinstall AUTOINSTALL[part]"

#define start_fast_install_command "ubinstall2 --autoinstall"

#define progress_path "/tmp/ubinstall/ubinstall_progress.log"

#define password_limits_path "/etc/security/pwquiality.conf"

typedef char* string; 
string version_application;
 
char *local; 
 
enum YON_PAGES {
	YON_PAGE_WELCOME = 0,
	YON_PAGE_LICENCE,
	YON_PAGE_SECTIONS,
	YON_PAGE_OS_COMPONENTS,
	YON_PAGE_SOFTWARE,
	YON_PAGE_INSTALLATION,
	YON_PAGE_REGION,
	YON_PAGE_KEYBOARD,
	YON_PAGE_USERS,
	YON_PAGE_COMPLETION,
	YON_PAGE_COMPLETED,
	YON_PAGE_INSTALL_COMMON,
	YON_PAGE_INSTALL_SEPARATE,
	YON_PAGE_INSTALL_SAME_PARTITION
};

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 always_open_documentation; 

	int password_min_length;
 
    int debug_mode;
	guint slider_thread;
	int install_mode;
	GThread *config_save_thread;
	GThread *install_thread;
	GThread *progress_thread;
} config; 
 
typedef struct { 
	GtkBuilder *builder;
    GtkListStore *DevicesList;
	GtkListStore *LanguagesList;
	GtkTreeStore *LayoutList;
	GtkListStore *AdditionalSoftwareList;
	GtkListStore *PartitionsList;

	GtkWidget *MainWindow;

	GtkWidget *StatusBox;
	GtkWidget *WelcomeToggle;
	GtkWidget *LicenceToggle;
	GtkWidget *LocationToggle;
	GtkWidget *KeyboardToggle;
	GtkWidget *SectionsToggle;
	GtkWidget *UsersToggle;
	GtkWidget *SummaryToggle;
	GtkWidget *InstallationToggle;
	GtkWidget *LanguageCombo;
	GtkWidget *RegionCombo;
	GtkWidget *ZoneCombo;
	GtkWidget *AvailableLanguagesEntry;
	GtkWidget *AvailableLanguagesButton;
	GtkWidget *LanguagesCombo;
	GtkWidget *KeyboardModelCombo;
	GtkWidget *LayoutBindingCombo;
	GtkWidget *DefaultLayoutRadio;
	GtkWidget *ManualLayoutRadio;
	GtkWidget *LayoutTree;
	GtkWidget *AddButton;
	GtkWidget *RemoveButton;
	GtkWidget *InstallationRadio;
	GtkWidget *InstallationNearRadio;
	GtkWidget *InstallationLinuxRadio;
	GtkWidget *InstallationWindowsRadio;
	GtkWidget *InstallationOptionsRadio;
	GtkWidget *UserNameEntry;
	GtkWidget *LoginEntry;
	GtkWidget *PasswordCombo;
	GtkWidget *PasswordEntry;
	GtkWidget *PasswordButton;
	GtkWidget *AutologinCheck;
	GtkWidget *AdminPasswordCombo;
	GtkWidget *AdminPasswordEntry;
	GtkWidget *AdminPasswordButton;
	GtkWidget *HotnameEntry;
	GtkWidget *CancelInstallButton;
	GtkWidget *BackButton;
	GtkWidget *NextButton;
	GtkWidget *mainSettingsButton;
	GtkWidget *menu1;
	GtkWidget *menu2;
	GtkWidget *RegionImage;
	GtkWidget *RegionBox;
	GtkWidget *RegionAspect;
	GtkWidget *KeyboardImage;
	GtkWidget *KeyboardBox;
	GtkWidget *Notebook;
	GtkWidget *LicenceLabel;
	GtkWidget *SlidesImage;

	GtkWidget *SameInstallationFilesystemTypeCombo;
	GtkWidget *SameInstallationFormatCheck;
	GtkWidget *SameInstallationSectionNameEntry;
	GtkWidget *NextInstallationFilesystemTypeCombo;
	GtkWidget *NextInstallationFormatCheck;
	GtkWidget *NextInstallationSectionNameEntry;
	GtkWidget *CommonInstallationFilesystemTypeCombo;
	GtkWidget *CommonInstallationSectionNameEntry;

	GtkWidget *InstallationNearSysDevicesTree;
	GtkWidget *InstallationNearSysSectionTree;
	GtkWidget *InstallationNearSizeSpin;
	GtkWidget *InstallationNearSizeTypeSpin;

	GtkWidget *SamePlaceDeviceTree;
	GtkWidget *SamePlacePartTree;
	GtkWidget *SamePlaceSizeSpin;
	GtkWidget *SamePlaceSizeTypeSpin;

	GtkWidget *InstallationProgress;
	GtkWidget *InstallationLabel;
	GtkWidget *PackageInstallationProgress;
	GtkWidget *PackageInstallationLabel;

	GtkCellRenderer *AdditionalSoftwareCell;

	GtkWidget *CommonInstallationDevicesTree;
	GtkWidget *AdditionalSoftwareTree;

	GtkTreeModel *LanguagesFilter;
	GtkTreeModel *LayoutsFilter;

    GdkPixbuf *regions_original;
    GdkPixbuf *keyboard_original;
    GdkPixbuf *slides_original;
    float region_height_mult;
 
} main_window; 
 
 
typedef struct { 
    GtkWidget *Window; 
     
    GtkWidget *AlwaysOpenCheck; 
 
    GtkWidget *CloseButton; 
    GtkWidget *AcceptButton; 
} documentation_confirmation_window; 
 
typedef struct{
	GtkWidget *MainWindow;
	GtkWidget *StatusBox;
	GtkWidget *headerBar;
	GtkWidget *CancelButton;
	GtkWidget *SaveButton;
	GtkWidget *LayoutsTree;
	GtkCellRenderer *ActiveToggle;
} ubinstall_keyboard_window;

typedef struct{
	GtkListStore *liststore1;
	GtkWidget *MainWindow;
	GtkWidget *StatusBox;
	GtkWidget *DefaultCheck;
	GtkWidget *headerBar;
	GtkWidget *CancelButton;
	GtkWidget *SaveButton;
	GtkWidget *LanguagesTree;

	GtkCellRenderer *ToggleRenderer;
} ubinstall_language_window;

typedef struct{
	GtkWidget *CreateGroupWindow;
	GtkWidget *StatusBox;
	GtkWidget *UserCancelButton;
	GtkWidget *UserOkButton;
	GtkWidget *PasswordEntry;
	GtkWidget *RepeatPasswordEntry;
	GtkWidget *PasswordHashEntry;
	GtkWidget *HashBox;
	GtkWidget *PasswordBox;
	GtkWidget *NoEncriptionCheck;
} password_window;

void config_init();
main_window *yon_main_window_complete();
ubinstall_language_window *yon_ubinstall_language_new();
void NewFunction(ubinstall_language_window *window, GtkBuilder *builder);
ubinstall_keyboard_window *yon_ubinstall_keyboard_new();

password_window *yon_password_new();
void on_password_accept(GtkWidget *self, dictionary *dict);