#include "ubinstall-gtk.h"



// void _yon_saving_threaded(char *final_command){
//     FILE *file = popen(final_command,"r");
//     int file_save;
//     config_str file_return = yon_config_load_file(file,&file_save);
//     pclose(file);
//     file_return = yon_char_parsed_append(file_return,&file_save,final_command);
//     char *result = yon_char_parsed_to_string(file_return,file_save,"");
//     yon_debug_output("%s\n",result);
//     yon_char_parsed_free(file_return,file_save);
//     free(result);
// }

void yon_config_save_proceed(char *path, YON_CONFIG_TYPE type){
        yon_debug_output("%s\n",yon_config_save_simple(type,path));
}
 
void yon_load_proceed(YON_CONFIG_TYPE type){ 
	if (type!=YON_CONFIG_CUSTOM){
		yon_config_clean();
            yon_config_load_config(YON_CONFIG_DEFAULT,yon_config_get_command(main_config.config_load_path),NULL);
	}
    if (type==YON_CONFIG_GLOBAL){
		yon_config_load_config(type,yon_config_get_command(main_config.config_load_path),NULL);
	} else if (type==YON_CONFIG_LOCAL){
		yon_config_load_config(type,yon_config_get_command(main_config.config_load_path),NULL);
    } else if (type==YON_CONFIG_CUSTOM){
        textdomain(template_ui_LocaleName);
		GtkWidget *dialog = gtk_file_chooser_dialog_new(template_app_information.app_title,NULL,GTK_FILE_CHOOSER_ACTION_SAVE,CANCEL_LABEL,GTK_RESPONSE_CANCEL,OPEN_LABEL,GTK_RESPONSE_ACCEPT,NULL);
        textdomain(LocaleName);
        gtk_window_set_icon_name(GTK_WINDOW(dialog),"com.ublinux.ubl-settings-services");
        gtk_window_set_title(GTK_WINDOW(dialog),TITLE_LABEL);
        GtkFileFilter *filter = gtk_file_filter_new();
		gtk_file_filter_add_pattern(filter,"*.ini");
        gtk_file_filter_set_name(filter, "*.ini");
		gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog),filter);
		gtk_widget_show(dialog);
		int response = gtk_dialog_run(GTK_DIALOG(dialog));
		if (response == GTK_RESPONSE_ACCEPT){
			char *file = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
			if (!yon_char_is_empty(file)){
				main_config.config_load_path=yon_char_new(file);
                yon_config_clean();
                if (!yon_char_is_empty(config_get_default_command))
                    yon_config_load_config(YON_CONFIG_DEFAULT,config_get_default_command,NULL);
                char *command = yon_config_get_command(main_config.config_load_path);
                yon_config_load_config(type,command,NULL);
			}
			gtk_widget_destroy(dialog);
		} else {
			gtk_widget_destroy(dialog);
		}
	}
}


void on_config_local_load(GtkWidget *,main_window *widgets){
    if (!yon_char_is_empty(main_config.config_load_path)) free(main_config.config_load_path);
    main_config.config_load_path = yon_char_new("system");
    yon_load_proceed(YON_CONFIG_LOCAL);
    yon_page_init(widgets,gtk_notebook_get_current_page(GTK_NOTEBOOK(widgets->Notebook)));
    main_config.load_mode=YON_CONFIG_LOCAL;
}

void on_config_global_load(GtkWidget *,main_window *widgets){
    if (!yon_char_is_empty(main_config.config_load_path)) free(main_config.config_load_path);
    main_config.config_load_path = yon_char_new("global");
    yon_load_proceed(YON_CONFIG_GLOBAL);
    yon_page_init(widgets,gtk_notebook_get_current_page(GTK_NOTEBOOK(widgets->Notebook)));
    main_config.load_mode=YON_CONFIG_GLOBAL;
}

void on_config_custom_load(GtkWidget *,main_window *widgets){
	yon_load_proceed(YON_CONFIG_CUSTOM);
    yon_page_init(widgets,gtk_notebook_get_current_page(GTK_NOTEBOOK(widgets->Notebook)));
	main_config.load_mode=YON_CONFIG_CUSTOM;
}

void on_config_custom_load_last(GtkWidget *,main_window *widgets){
    yon_config_clean();
    if (!yon_char_is_empty(config_get_default_command))
        yon_config_load_config(YON_CONFIG_DEFAULT,config_get_default_command,NULL);
    char *command = yon_config_get_command(main_config.config_load_path);
    yon_config_load_config(YON_CONFIG_CUSTOM,command,NULL);
    yon_page_init(widgets,gtk_notebook_get_current_page(GTK_NOTEBOOK(widgets->Notebook)));
	main_config.load_mode=YON_CONFIG_CUSTOM;
}

void on_config_global_local_save(GtkWidget *,main_window *widgets){
    yon_config_save_proceed(NULL,YON_CONFIG_BOTH);
    gtk_notebook_set_current_page(GTK_NOTEBOOK(widgets->Notebook),YON_PAGE_COMPLETED);
}

void on_config_local_save(GtkWidget *,main_window *widgets){
    yon_config_save_proceed("system",YON_CONFIG_LOCAL);
    gtk_notebook_set_current_page(GTK_NOTEBOOK(widgets->Notebook),YON_PAGE_COMPLETED);

}

void on_config_global_save(GtkWidget *,main_window *widgets){
    yon_config_save_proceed("global",YON_CONFIG_GLOBAL);
    gtk_notebook_set_current_page(GTK_NOTEBOOK(widgets->Notebook),YON_PAGE_COMPLETED);

}

// void on_config_custom_save(GtkWidget *, main_window *widgets){
//     char *path = NULL;
//             GtkWidget *dialog = gtk_file_chooser_dialog_new(TITLE_LABEL,NULL,GTK_FILE_CHOOSER_ACTION_SAVE,CANCEL_LABEL,GTK_RESPONSE_CANCEL,SAVE_LABEL,GTK_RESPONSE_ACCEPT,NULL);
//             textdomain(TITLE_LABEL);
//             GtkFileFilter *filter = gtk_file_filter_new();
//             gtk_window_set_icon_name(GTK_WINDOW(dialog),yon_char_append("com.ublinux.",LocaleName));
//             gtk_file_filter_add_pattern(filter,"*.ini");
//             gtk_file_filter_set_name(filter, "*.ini");
//             gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog),filter);
//             int response = gtk_dialog_run(GTK_DIALOG(dialog));
//             if (response == GTK_RESPONSE_ACCEPT){
//                 char *file = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
//                 if (!yon_char_is_empty(file)){
//                     if (!strstr(file,".ini")) file = yon_char_append(file,".ini");
//                     if (access(file,0)!=F_OK){
//                         char *command_creation = ubconfig_file_create(file);
//                         int a = system(yon_debug_output("%s\n",command_creation));
//                         if (access(file,0)!=F_OK||a){
//                             yon_ubl_status_box_render(CUSTOM_CONFIG_CREATION_ERROR_LABEL,BACKGROUND_IMAGE_FAIL_TYPE);
//                             return;
//                         }
//                     }
//                 }
//                 path = yon_char_unite("'",file,"'",NULL);
//                 free(file);
//                 gtk_widget_destroy(dialog);
//             } else {
//                 gtk_widget_destroy(dialog);
//                 return;
//             }
// 	yon_save_proceed(path,YON_CONFIG_CUSTOM);
//     if (path) free(path);
//     gtk_notebook_set_current_page(GTK_NOTEBOOK(widgets->Notebook),YON_PAGE_COMPLETED);
// }
//
// void yon_open_browser(GtkWidget *, char *link);
// void yon_open_browser(GtkWidget *, char *link){
//     GtkWidget *window = yon_ubl_browser_window_open(link,TITLE_LABEL);
//     if (window)
//     gtk_window_set_icon_name(GTK_WINDOW(window),yon_char_append("com.ublinux.",LocaleName));
// }

// void yon_interface_update(main_window *widgets){
//     if (widgets){};
//     enum YON_PAGES page=YON_PAGE_COMPLETED;
//     char *type = config(AUTOINSTALL_TYPE_INSTALL);
//     if (!yon_char_is_empty(type)){
//         if (!strcmp(type,"fast")){
//             gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->InstallationRadio),1);
//             page = YON_PAGE_INSTALL_COMMON;
//         } else if (!strcmp(type,"next")){
//             gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->InstallationNearRadio),1);
//             page = YON_PAGE_INSTALL_SEPARATE;
//         } else if (!strcmp(type,"part")){
//             gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->InstallationLinuxRadio),1);
//             page = YON_PAGE_INSTALL_SAME_PARTITION;
//         } else if (!strcmp(type,"grub_install")){
//             gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->InstallationWindowsRadio),1);
//             gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->GrubInstallRadio),1);
//             page = YON_PAGE_OPTIONS_GRUB_INSTALL;
//         } else if (!strcmp(type,"grub_update")){
//             gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->InstallationWindowsRadio),1);
//             gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->GrubUpdateRadio),1);
//             page = YON_PAGE_OPTIONS_GRUB_UPDATE;
//         } else if (!strcmp(type,"system_only")){
//             gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->InstallationWindowsRadio),1);
//             gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->OSRadio),1);
//             page = YON_PAGE_OPTIONS_OS_ONLY;
//         } else if (!strcmp(type,"data_only")){
//             gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->InstallationWindowsRadio),1);
//             gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->UserDataOnlyRadio),1);
//             page = YON_PAGE_OPTIONS_USRDATA_ONLY;
//         } else if (!strcmp(type,"custom")){
//             gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->InstallationWindowsRadio),1);
//             gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->SeparateRadio),1);
//             page = YON_PAGE_OPTIONS_SEPARATE;
//         }
//     }
//     char *device = config(AUTOINSTALL_DEVICE);
//     char *part = NULL;
//     if (page!=YON_PAGE_INSTALL_COMMON)
//     part = config(part_parameter);
//     char *fs_type = config(part_fs_type_parameter);
//     char *device_label = config(device_label_parameter);
//     char *format = config(part_format_parameter);
//     char *part_size = config(part_size_parameter);
//     GtkListStore *device_list = widgets->DevicesList;
//     GtkListStore *part_list=widgets->PartitionsList;
//     GtkWidget *device_tree=NULL;
//     GtkWidget *part_tree=NULL;
//     switch (page){
//         case YON_PAGE_INSTALL_COMMON:{
//             device_tree = widgets->CommonInstallationDevicesTree;
//         } break;
//
//         case YON_PAGE_INSTALL_SEPARATE:{
//             device_tree = widgets->NextInstallationSysDevicesTree;
//             part_tree = widgets->NextInstallationSysSectionTree;
//             if (!yon_char_is_empty(part_size)){
//                 gtk_spin_button_set_value(GTK_SPIN_BUTTON(widgets->NextInstallationSizeSpin),atof(part_size));
//                 if (part_size[strlen(part_size)-1]=='M') gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->NextInstallationSizeTypeSpin),0);
//                 if (part_size[strlen(part_size)-1]=='G') gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->NextInstallationSizeTypeSpin),1);
//                 if (part_size[strlen(part_size)-1]=='T') gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->NextInstallationSizeTypeSpin),2);
//             }
//             if (format&&!strcmp(format,"yes")) gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->NextInstallationFormatCheck),1);
//             else  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->NextInstallationFormatCheck),main_config.format_default);
//             if (!yon_char_is_empty(fs_type))
//             gtk_combo_box_set_active_id(GTK_COMBO_BOX(widgets->NextInstallationFilesystemTypeCombo),fs_type);
//             if (device_label)
//             gtk_entry_set_text(GTK_ENTRY(widgets->NextInstallationSectionNameEntry),device_label);
//             else 
//             gtk_entry_set_text(GTK_ENTRY(widgets->NextInstallationSectionNameEntry),"");
//
//         } break;
//
//         case YON_PAGE_INSTALL_SAME_PARTITION:{
//             device_tree = widgets->SamePlaceDeviceTree;
//             part_tree = widgets->SamePlacePartTree;
//             if (!yon_char_is_empty(fs_type))
//                 gtk_combo_box_set_active_id(GTK_COMBO_BOX(widgets->SameInstallationFilesystemTypeCombo),fs_type);
//             if (format&&!strcmp(format,"yes")) gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->SameInstallationFormatCheck),1);
//             else  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->SameInstallationFormatCheck),main_config.format_default);
//             if (device_label)
//                 gtk_entry_set_text(GTK_ENTRY(widgets->SameInstallationSectionNameEntry),device_label);
//                 else 
//                 gtk_entry_set_text(GTK_ENTRY(widgets->SameInstallationSectionNameEntry),"");
//
//         } break;
//         case YON_PAGE_OPTIONS_GRUB_INSTALL:{
//             device_tree = widgets->GrubInstallDevicesTree;
//         } break;
//
//         case YON_PAGE_OPTIONS_GRUB_UPDATE:{
//             device_tree = widgets->GrubUpdateDevicesTree;
//         } break;
//
//         case YON_PAGE_OPTIONS_SEPARATE:{
//             GtkTreeIter iter;
//             device_tree = widgets->SeparateDevicesTree;
//             part_tree = widgets->SeparateSysSectionTree;
//             for_iter(GTK_TREE_MODEL(device_list),&iter){
//                 char *cur_device;
//                 gtk_tree_model_get(GTK_TREE_MODEL(device_list),&iter, 0,&cur_device,-1);
//                 if (!strcmp(cur_device,device)){
//                     gtk_tree_selection_select_iter(gtk_tree_view_get_selection(GTK_TREE_VIEW(device_tree)),&iter);
//                     break;
//                 }
//             }
//             on_near_installation_device_changed(device_tree,widgets);
//             for_iter(GTK_TREE_MODEL(part_list),&iter){
//                 char *cur_part;
//                 gtk_tree_model_get(GTK_TREE_MODEL(device_list),&iter, 0,&cur_part,-1);
//                 if (!strcmp(cur_part,part)){
//                     gtk_tree_selection_select_iter(gtk_tree_view_get_selection(GTK_TREE_VIEW(part_tree)),&iter);
//                 }
//             }
//         } break;
//
//         case YON_PAGE_OPTIONS_OS_ONLY:{
//             device_tree = widgets->OSDevicesTree;
//             part_tree = widgets->OSSysSectionTree;
//         } break;
//
//         case YON_PAGE_OPTIONS_USRDATA_ONLY:{
//             device_tree = widgets->UserdataDevicesTree;
//             part_tree = widgets->UserdataSysSectionTree;
//         } break;
//
//         default:{}break;
//     }
//     GtkTreeIter iter;
//     char *cur_device="";
//     if (page!=YON_PAGE_OPTIONS_SEPARATE && !yon_char_is_empty(device)){
//         for_iter (widgets->DevicesList,&iter){
//             gtk_tree_model_get(GTK_TREE_MODEL(widgets->DevicesList),&iter,0,&cur_device,-1);
//             if (!strcmp(device,cur_device)){
//                 gtk_tree_selection_select_iter(gtk_tree_view_get_selection(GTK_TREE_VIEW(device_tree)),&iter);
//                 break;
//             }
//         }
//         on_near_installation_device_changed(device_tree,widgets);
//         if (!yon_char_is_empty(part)){
//             for_iter (widgets->PartitionsList,&iter){
//                 gtk_tree_model_get(GTK_TREE_MODEL(widgets->PartitionsList),&iter,0,&part,-1);
//                 if (!strcmp(device,cur_device)){
//                     gtk_tree_selection_select_iter(gtk_tree_view_get_selection(GTK_TREE_VIEW(part_tree)),&iter);
//                     break;
//                 }
//             }
//         }
//     }
//     char *system_locale = config(locale_parameter);
//     if (!yon_char_is_empty(system_locale)){
//         char *chosen_langs = "";
//         for_iter(widgets->LanguagesList,&iter){
//             char *cur=NULL, *render = NULL;
//             gtk_tree_model_get(GTK_TREE_MODEL(widgets->LanguagesList),&iter,1,&render,2,&cur,-1);
//             if (strstr(system_locale,cur)){
//                 gtk_list_store_set((widgets->LanguagesList),&iter,0,1,-1);
//                 chosen_langs = yon_char_unite(chosen_langs,!yon_char_is_empty(chosen_langs)?";":"",render,NULL);
//             } else {
//                 gtk_list_store_set((widgets->LanguagesList),&iter,0,0,-1);
//             }
//         }
//         if (!yon_char_is_empty(chosen_langs)){
//             gtk_entry_set_text(GTK_ENTRY(widgets->AvailableLanguagesEntry),chosen_langs);
//             gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->LanguagesSensitiveCheck),1);
//             free(chosen_langs);
//         } else {
//             gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->LanguagesSensitiveCheck),0);
//             gtk_entry_set_text(GTK_ENTRY(widgets->AvailableLanguagesEntry),"");
//         }
//     } else {
//         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->LanguagesSensitiveCheck),0);
//         gtk_entry_set_text(GTK_ENTRY(widgets->AvailableLanguagesEntry),"");
//         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->LanguagesSensitiveCheck),0);
//         int langsize;
//         config_str lang = default_langs(&langsize);
//         for_iter(widgets->LanguagesList,&iter){
//             char *cur=NULL;
//             gtk_tree_model_get(GTK_TREE_MODEL(widgets->LanguagesList),&iter,2,&cur,-1);
//                 if (lang&&yon_char_parsed_check_exist(lang,langsize,cur)>-1){
//                     gtk_list_store_set(widgets->LanguagesList,&iter,0,1,-1);
//                 } else {
//                     gtk_list_store_set(widgets->LanguagesList,&iter,0,0,-1);
//                 }
//                 if (cur) free(cur);
//         }
//         if (langsize) yon_char_parsed_free(lang,langsize);
//     }
//     char *zone = config(zone_parameter);
//     char *region = NULL;
//
//     if (!yon_char_is_empty(zone)) region = yon_char_divide_search(zone,"/",-1); else {gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->RegionSensitiveSwitch),0);}
//     if (!yon_char_is_empty(region)){
//         gtk_combo_box_set_active_id(GTK_COMBO_BOX(widgets->RegionCombo),region);
//     } else {
//         gtk_combo_box_set_active_id(GTK_COMBO_BOX(widgets->RegionCombo),"Europe");
//         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->RegionSensitiveSwitch),0);
//     }
//     if (!yon_char_is_empty(zone)){
//         gtk_combo_box_set_active_id(GTK_COMBO_BOX(widgets->ZoneCombo),zone);
//     } else {
//         gtk_combo_box_set_active_id(GTK_COMBO_BOX(widgets->ZoneCombo),"Moscow");
//     }
//     char *language = config(lang_parameter);
//     if (!yon_char_is_empty(language)){
//         gtk_combo_box_set_active_id(GTK_COMBO_BOX(widgets->LanguageCombo),language);
//     } else {
//         gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->LanguageCombo),0);
//         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->MainLanguageSensitiveCheck),0);
//     }
//     char *kbmodel = config (xkbmodel_parameter);
//     char *optinos = config(xkboptions_parameter);
//     char *layout = config(xkblayout_parameter);
//     if (!yon_char_is_empty(kbmodel)){
//         gtk_combo_box_set_active_id(GTK_COMBO_BOX(widgets->KeyboardModelCombo),kbmodel);
//     } else {
//         gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->KeyboardModelCombo),0);
//         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->KeyboardModelSensitiveCheck),0);
//
//     }
//     if (!yon_char_is_empty(optinos)){
//         gtk_combo_box_set_active_id(GTK_COMBO_BOX(widgets->LayoutBindingCombo),optinos);
//     } else {
//         gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->LayoutBindingCombo),0);
//         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->OptionsSensitiveCheck),0);
//     }
//     if (!yon_char_is_empty(layout)){
//         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->ManualLayoutRadio),1);
//         for_iter(widgets->LayoutList,&iter){
//             char *cur=NULL;
//             gtk_tree_model_get(GTK_TREE_MODEL(widgets->LayoutList),&iter,0,&cur,-1);
//             if (strstr(layout,cur)){
//                 gtk_tree_store_set(widgets->LayoutList,&iter,3,1,-1);
//             } else {
//                 gtk_tree_store_set(widgets->LayoutList,&iter,3,0,-1);
//             }
//         }
//     } else {
//         for_iter(widgets->LayoutList,&iter){
//             char *id;
//             gtk_tree_model_get(GTK_TREE_MODEL(widgets->LayoutList),&iter,0,&id,-1);
//             if (!strcmp(id,"ru")||!strcmp(id,"us")){
//                 gtk_tree_store_set(widgets->LayoutList,&iter,3,1,-1);
//             } else {
//                 gtk_tree_store_set((widgets->LayoutList),&iter,3,0,-1);
//             }
//         }
//         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->LayoutSensitiveCheck),0);
//     }
//     char *user_name = config(user_name_parameter);
//     char *user_gecos = config(user_gecos_parameter);
//     char *user_password = config(user_password_parameter);
//     char *root_password = config(root_password_parameter);
//     char *autologin = config(autologin_parameter);
//     char *hostname = config(hostname_parameter);
//     if (!yon_char_is_empty(user_name)){
//         if (!strcmp(user_name,"root")){
//             gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->UserRootOnlyCheck),1);
//         } else {
//             gtk_entry_set_text(GTK_ENTRY(widgets->UserRootLoginEntry),user_name);
//         }
//     } else {
//         gtk_entry_set_text(GTK_ENTRY(widgets->UserRootLoginEntry),"");
//         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->LoginSensitiveCheck),0);
//     }
//     if (!yon_char_is_empty(user_gecos)){
//         gtk_entry_set_text(GTK_ENTRY(widgets->UserRootNameEntry),_(user_gecos));
//     } else {
//         gtk_entry_set_text(GTK_ENTRY(widgets->UserRootNameEntry),"");
//         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->UsernameSensitiveCheck),0);
//     }
//     int def_size=0;
//     config_str default_password = NULL;
//     if (!getuid()){
//         default_password = yon_config_load(yon_debug_output("%s\n",get_default_password_command), &def_size);
//         if (def_size>0&&default_password){
//             yon_char_remove_last_symbol(default_password[0],'\n');
//         }
//     }
//     if ((def_size>0&&!strcmp(default_password[0],user_password))||yon_char_is_empty(user_password)){
//         gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->UserRootPasswordCombo),0);
//         gtk_entry_set_text(GTK_ENTRY(widgets->UserRootPasswordEntry),"");
//         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->PasswordSensitiveCheck),0);
//
//     } else if (!yon_char_is_empty(user_password)){
//         gtk_entry_set_text(GTK_ENTRY(widgets->UserRootPasswordEntry),user_password);
//         gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->UserRootPasswordCombo),1);
//     }
//     if ((def_size>0&&!strcmp(default_password[0],user_password))||yon_char_is_empty(user_password)){
//         gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->AdminPasswordCombo),0);
//         gtk_entry_set_text(GTK_ENTRY(widgets->AdminPasswordEntry),"");
//         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->RootPasswordSensitiveCheck),0);
//
//     } else if (!yon_char_is_empty(root_password)){
//         gtk_entry_set_text(GTK_ENTRY(widgets->AdminPasswordEntry),root_password);
//         gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->AdminPasswordCombo),1);
//     }
//     if (!yon_char_is_empty(autologin)){
//         if (!strcmp(autologin,"yes"))
//             gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->UserAutologinSwitch),1);
//         else 
//             gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->UserAutologinSwitch),0);
//     } else {
//         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->UserAutologinSwitch),main_config.autologin_default);
//         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->AutologinSensitiveCheck),0);
//     }
//     if (!yon_char_is_empty(hostname)){
//         if (strcmp(hostname,"auto")){
//             gtk_entry_set_text(GTK_ENTRY(widgets->HotnameEntry),hostname);
//             gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->AutoHostnameCheck),0);
//             gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->HostnameSensitiveCheck),1);
//         } else{
//             gtk_entry_set_text(GTK_ENTRY(widgets->HotnameEntry),"");
//             gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->AutoHostnameCheck),1);
//         }
//     } else {
//         gtk_entry_set_text(GTK_ENTRY(widgets->HotnameEntry),"");
//         g_signal_handlers_block_by_func(G_OBJECT(widgets->AutoHostnameCheck),on_autohostname_check,widgets);
//         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->AutoHostnameCheck),1);
//         g_signal_handlers_unblock_by_func(G_OBJECT(widgets->AutoHostnameCheck),on_autohostname_check,widgets);
//             gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->HostnameSensitiveCheck),0);
//     }
// }

gboolean on_install_success(main_window *widgets){
    gtk_label_set_text(GTK_LABEL(widgets->InstallationLabel),"");
    gtk_notebook_set_current_page(GTK_NOTEBOOK(widgets->Notebook),YON_PAGE_COMPLETION);
    yon_config_restore(widgets);
    yon_page_update(widgets);

    return 0;
}

gboolean on_install_error(main_window *widgets){
    gtk_label_set_text(GTK_LABEL(widgets->InstallationLabel),"");
    gtk_notebook_set_current_page(GTK_NOTEBOOK(widgets->Notebook),YON_PAGE_INSTALL_ERROR);
    yon_page_update(widgets);

    return 0;
}

enum INSTALL_TYPE{
    INSTALL_COMMON,
    INSTALL_PART,
    INSTALL_NEXT,
    INSTALL_ADVANCED,
    INSTALL_GRUB_INSTALL,
    INSTALL_GRUB_UPDATE,
    INSTALL_SYSTEM_ONLY,
    INSTALL_USER_ONLY,
    INSTALL_ERROR
};

/**Функция получения текущего режима установки
 * 
 */
enum INSTALL_TYPE yon_ubl_get_install_mode(){
    
    char *value = config(AUTOINSTALL_TYPE_INSTALL);
    if (!yon_char_is_empty(value)){
        if (!strcmp(value,"fast")){
            return INSTALL_COMMON;
        } else if (!strcmp(value,"part")){
            return INSTALL_PART;
        } else if (!strcmp(value,"next")){
            return INSTALL_NEXT;
        } else if (!strcmp(value,"advanced")){
            return INSTALL_ADVANCED;
        } else if (!strcmp(value,"grub_install")){
            return INSTALL_GRUB_INSTALL;
        } else if (!strcmp(value,"grub_update")){
            return INSTALL_GRUB_UPDATE;
        } else if (!strcmp(value,"system_only")){
            return INSTALL_SYSTEM_ONLY;
        } else if (!strcmp(value,"data_only")){
            return INSTALL_USER_ONLY;
        }
    }
    return INSTALL_ERROR;
}

void *on_config_save(void *data){
    main_window *widgets = (main_window*)data;

        int size=0;
        config_str parameters = NULL;
        enum INSTALL_TYPE install_mode = yon_ubl_get_install_mode();
        switch(install_mode){
            case INSTALL_COMMON:
                parameters = yon_config_get_selection_by_key_no_ignored(&size,install_common_parameters,modules_parameter,NULL);
            break;
            case INSTALL_PART:
                parameters = yon_config_get_selection_by_key_no_ignored(&size,install_part_parameters,modules_parameter,NULL);
            break;
            case INSTALL_NEXT:
                parameters = yon_config_get_selection_by_key_no_ignored(&size,install_next_parameters,modules_parameter,NULL);
            break;
            case INSTALL_ADVANCED:
                parameters = yon_config_get_selection_by_key_no_ignored(&size,install_advanced_parameters,modules_parameter,NULL);
            break;
            case INSTALL_GRUB_INSTALL:
            case INSTALL_GRUB_UPDATE:
                parameters = yon_config_get_selection_by_key_no_ignored(&size,install_grub_install_update_parameters,NULL);
            break;
            case INSTALL_SYSTEM_ONLY:
                parameters = yon_config_get_selection_by_key_no_ignored(&size,install_system_only_parameters,NULL);
            break;
            case INSTALL_USER_ONLY:
                parameters = yon_config_get_selection_by_key_no_ignored(&size,install_userdata_only_parameters,NULL);
            break;
            default:
                yon_ubl_status_box_spawn(GTK_CONTAINER(widgets->StatusBox),ERROR_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE); 
                return 0;
        }
        FILE *file = fopen(progress_path,"w");
        if (file){
            fclose(file);
        }
            GFile *file_desc = g_file_new_for_path(progress_path);
            widgets->install_info_monitor = g_file_monitor_file(file_desc,G_FILE_MONITOR_NONE,NULL,NULL);
            g_signal_connect(G_OBJECT(widgets->install_info_monitor),"changed",G_CALLBACK(yon_installation_progress_update),widgets);
            g_object_unref(file_desc);
            gtk_widget_show(gtk_widget_get_parent(widgets->InstallationProgress));
        char *command = yon_debug_output("%s\n",save_config_command(yon_char_parsed_to_string(parameters,size," ")));
        yon_char_parsed_free(parameters,size);
        yon_debug_output("%s\n","Entered installation");
        if (system(yon_debug_output("%s\n",command))){

            gdk_threads_add_idle((GSourceFunc)on_install_error,widgets);
            free(command);
            g_mutex_lock(&main_config.install_mutex);
            main_config.install_complete=1;
            g_mutex_unlock(&main_config.install_mutex);
            return 0;
        };
        free(command);
        g_mutex_lock(&main_config.install_mutex);
        main_config.install_complete=1;
        g_mutex_unlock(&main_config.install_mutex);
        if ((!main_config.save_done)&&main_config.save_configured){
            on_setup_system_configuration(widgets);
            on_page_next_clicked(NULL,widgets);
        }
    return 0;
}

void *on_setup_system_configuration(void * data){
    yon_debug_output("%s\n","Entered thread");
    main_window *widgets = (main_window*)data;
    if (widgets){};
    int size;
    config_str all_parameters = yon_config_get_selection_by_key(&size,
    root_password_parameter,
    autologin_parameter,
    xkbmodel_parameter,
    xkblayout_parameter,
    xkbvariant_parameter,
    xkboptions_parameter,
    hostname_parameter,
    zone_parameter,
    lang_parameter,
    locale_parameter,
    SERVICES_ENABLE_parameter,
    GRUB_DEFAULT_parameter,
    GRUB_TIMEOUT_parameter,
    AUTOLOGINUSER_parameter,
    GRUB_SUPERUSERS_parameter,
    DOMAIN_parameter,
    DOMAIN_admanger_parameter,
    NTPSERVERS_parameter,
    modules_extra_parameter,
    KERNEL_BOOT_parameter,
    packages_parameter,
    NULL);
    int user_size=0;
    config_str users = yon_config_get_all_by_key(USERADD_parameter_search,&user_size);
    if (user_size){
        int final_size;
        config_str final = yon_char_parsed_merge(all_parameters,size,users,user_size,&final_size);
        yon_char_parsed_free(users,user_size);
        if (size) yon_char_parsed_free(all_parameters,size);
        all_parameters = final;
        size = final_size;
    }
    users = yon_config_get_all_by_key(GRUB_PASSWORD_parameter_search,&user_size);
    if (users){
        int final_size;
        config_str final = yon_char_parsed_merge(all_parameters,size,users,user_size,&final_size);
        yon_char_parsed_free(users,user_size);
        if (size) yon_char_parsed_free(all_parameters,size);
        all_parameters = final;
        size = final_size;
    }
    int network_size;
    config_str networks = yon_config_get_all_by_key(NETWORK_parameter_search,&network_size);
    if (network_size){
        int final_size;
        config_str final = yon_char_parsed_merge(all_parameters,size,networks,network_size,&final_size);
        yon_char_parsed_free(networks,network_size);
        if (size) yon_char_parsed_free(all_parameters,size);
        all_parameters = final;
        size = final_size;
    }

    if (all_parameters){
        for (int i=0;i<size;i++){
            char *parameter = yon_char_divide_search(all_parameters[i],"=",-1);
            if (!strstr(parameter,"'")){
                char *key = yon_config_parameter_get_key(parameter);
                free(parameter);
                char *tmp = yon_char_unite("'",key,"'",NULL);
                free(key);
                parameter = AUTOINSTALL(tmp);
                free(tmp);
            }
            if (!strstr(all_parameters[i],"'")){
                char *temp = yon_char_unite("'",all_parameters[i],"'",NULL);
                free(all_parameters[i]);
                all_parameters[i]=temp;
            }
            char *temp = yon_char_unite(parameter,"=",all_parameters[i],NULL);
            free(all_parameters[i]);
            all_parameters[i]=temp;

            
        }
        char *parameter_string = yon_char_parsed_to_string(all_parameters,size," ");
        char *command = set_user_config_command(parameter_string);
        if (system(yon_debug_output("%s\n",command))){};
        yon_char_parsed_free(all_parameters,size);
        free(command);
        if (parameter_string) free(parameter_string);
    }
    g_idle_add((GSourceFunc)on_install_success,widgets);
    return NULL;
}

int yon_config_save(main_window *widgets){
    {
        int size=0;
        config_str parameters = NULL;
        enum INSTALL_TYPE install_mode = yon_ubl_get_install_mode();
        switch(install_mode){
            case INSTALL_COMMON:
                parameters = yon_config_get_selection_by_key_no_ignored(&size,install_common_parameters,modules_parameter,NULL);
            break;
            case INSTALL_PART:
                parameters = yon_config_get_selection_by_key_no_ignored(&size,install_part_parameters,modules_parameter,NULL);
            break;
            case INSTALL_NEXT:
                parameters = yon_config_get_selection_by_key_no_ignored(&size,install_next_parameters,modules_parameter,NULL);
            break;
            case INSTALL_ADVANCED:
                parameters = yon_config_get_selection_by_key_no_ignored(&size,install_advanced_parameters,modules_parameter,NULL);
            break;
            case INSTALL_GRUB_INSTALL:
            case INSTALL_GRUB_UPDATE:
                parameters = yon_config_get_selection_by_key_no_ignored(&size,install_grub_install_update_parameters,NULL);
            break;
            case INSTALL_SYSTEM_ONLY:
                parameters = yon_config_get_selection_by_key_no_ignored(&size,install_system_only_parameters,NULL);
            break;
            case INSTALL_USER_ONLY:
                parameters = yon_config_get_selection_by_key_no_ignored(&size,install_userdata_only_parameters,NULL);
            break;
            default:
                yon_ubl_status_box_spawn(GTK_CONTAINER(widgets->StatusBox),ERROR_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE); 
                return 0;
        }
        char *command = yon_debug_output("%s\n",ubconfig_set_command_full(main_config.config_save_path,"[autoinstall]",yon_char_parsed_to_string(parameters,size," ")));
        if (system(command)){
            return 0;
        }
    }
    int size;
    config_str all_parameters = yon_config_get_selection_by_key(&size,
    root_password_parameter,
    autologin_parameter,
    xkbmodel_parameter,
    xkblayout_parameter,
    xkbvariant_parameter,
    xkboptions_parameter,
    hostname_parameter,
    zone_parameter,
    lang_parameter,
    locale_parameter,
    SERVICES_ENABLE_parameter,
    GRUB_DEFAULT_parameter,
    GRUB_TIMEOUT_parameter,
    AUTOLOGINUSER_parameter,
    GRUB_SUPERUSERS_parameter,
    DOMAIN_parameter,
    DOMAIN_admanger_parameter,
    NTPSERVERS_parameter,
    modules_extra_parameter,
    KERNEL_BOOT_parameter,
    packages_parameter,
    NULL);
    int user_size=0;
    config_str users = yon_config_get_all_by_key(USERADD_parameter_search,&user_size);
    if (user_size){
        int final_size;
        config_str final = yon_char_parsed_merge(all_parameters,size,users,user_size,&final_size);
        yon_char_parsed_free(users,user_size);
        if (size) yon_char_parsed_free(all_parameters,size);
        all_parameters = final;
        size = final_size;
    }
    users = yon_config_get_all_by_key(GRUB_PASSWORD_parameter_search,&user_size);
    if (users){
        int final_size;
        config_str final = yon_char_parsed_merge(all_parameters,size,users,user_size,&final_size);
        yon_char_parsed_free(users,user_size);
        if (size) yon_char_parsed_free(all_parameters,size);
        all_parameters = final;
        size = final_size;
    }
    int network_size;
    config_str networks = yon_config_get_all_by_key(NETWORK_parameter_search,&network_size);
    if (network_size){
        int final_size;
        config_str final = yon_char_parsed_merge(all_parameters,size,networks,network_size,&final_size);
        yon_char_parsed_free(networks,network_size);
        if (size) yon_char_parsed_free(all_parameters,size);
        all_parameters = final;
        size = final_size;
    }

    if (all_parameters){
        for (int i=0;i<size;i++){
            char *parameter = yon_char_divide_search(all_parameters[i],"=",-1);
            if (!strstr(parameter,"'")){
                char *key = yon_config_parameter_get_key(parameter);
                free(parameter);
                char *tmp = yon_char_unite("'",key,"'",NULL);
                free(key);
                parameter = AUTOINSTALL(tmp);
                free(tmp);
            }
            if (!strstr(all_parameters[i],"'")){
                char *temp = yon_char_unite("'",all_parameters[i],"'",NULL);
                free(all_parameters[i]);
                all_parameters[i]=temp;
            }
            char *temp = yon_char_unite(parameter,"=",all_parameters[i],NULL);
            free(all_parameters[i]);
            all_parameters[i]=temp;

            
        }
        char *parameter_string = yon_char_parsed_to_string(all_parameters,size," ");
        if (!yon_char_is_empty(parameter_string)){
            char *command = ubconfig_set_command_full(main_config.config_save_path,"",parameter_string);
            if (!system(command)){
                return 1;
            } else return 0;
            
        }
    }
    return 1;
}

void yon_config_restore(main_window *widgets){
    switch(main_config.load_mode){
        case YON_CONFIG_GLOBAL:
            on_config_global_load(NULL,widgets);
            break;
        case YON_CONFIG_LOCAL:
            on_config_local_load(NULL,widgets);
            break;
        case YON_CONFIG_CUSTOM:
            on_config_custom_load_last(NULL,widgets);

    }
    on_config_global_load(NULL,widgets);
    system("ubconfig --source system remove [autoinstall]");
    yon_config_save_simple(YON_CONFIG_LOCAL,"system");
}