#include "ubinstall-gtk.h"

int yon_region_save(main_window *widgets){
    if (gtk_combo_box_get_active(GTK_COMBO_BOX(widgets->RegionCombo))==-1){
        yon_ubl_status_box_spawn(GTK_CONTAINER(widgets->StatusBox),REGION_EMPTY_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
        yon_ubl_status_highlight_incorrect(widgets->RegionCombo);
        return 0;
    } else if (gtk_combo_box_get_active(GTK_COMBO_BOX(widgets->ZoneCombo))==-1){
        yon_ubl_status_box_spawn(GTK_CONTAINER(widgets->StatusBox),ZONE_EMPTY_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
        yon_ubl_status_highlight_incorrect(widgets->ZoneCombo);
        return 0;
    } 
    char *languages = "";
    GtkTreeIter iter;
    GtkTreeModel *model = GTK_TREE_MODEL(widgets->LanguagesFilter);
    char *lang_code=NULL;
    for_iter(model,&iter){
        gtk_tree_model_get(model,&iter,2,&lang_code,-1);
        languages = yon_char_unite(languages,!yon_char_is_empty(languages)?",":"",lang_code,NULL);
    }
    if (yon_char_is_empty(languages)){
        yon_config_remove_by_key(locale_parameter);
    } else {
        yon_config_register(locale_parameter,locale_parameter_command,languages);
        free(languages);
    }
    if (gtk_combo_box_get_active(GTK_COMBO_BOX(widgets->LanguagesCombo))==-1){
        yon_config_remove_by_key(lang_parameter);
    } else {
        char *language = (char*)gtk_combo_box_get_active_id(GTK_COMBO_BOX(widgets->LanguagesCombo));
        yon_config_register(lang_parameter,lang_parameter_command,language);
    }
    if (gtk_switch_get_active(GTK_SWITCH(widgets->RegionSensitiveSwitch))){
        char *region = (char*)gtk_combo_box_get_active_id(GTK_COMBO_BOX(widgets->RegionCombo));
        char *zone = (char*)gtk_combo_box_get_active_id(GTK_COMBO_BOX(widgets->ZoneCombo));
        yon_debug_output("%s",region);
        yon_debug_output("/%s\n\n",zone);
        yon_config_register(zone_parameter,zone_parameter_command,yon_char_unite(region,"/",zone,NULL));
    } else {
        yon_config_remove_by_key(zone_parameter);
    }
    if (!main_config.configure_mode){
        int size;
        config_str parameters = yon_config_get_save_parameters_by_key(&size,locale_parameter,lang_parameter,zone_parameter,NULL);
        if (parameters){
            char *command_parameters = yon_char_parsed_to_string(parameters,size," -- ");
            char *command = yon_char_unite(ubconfig_dull_command,"--target system ",command_parameters,NULL);
            if (!system(command)){}
            free(command);
            yon_char_parsed_free(parameters,size);
        }
    }
    return 1;
}

void on_region_changed(GtkComboBox *self, main_window *widgets){
    char *active = (char*)gtk_combo_box_get_active_id(self);
    active = yon_char_append("/usr/share/zoneinfo/",active);
    int size;
    gtk_combo_box_text_remove_all(GTK_COMBO_BOX_TEXT(widgets->ZoneCombo));
    if (yon_file_is_directory(active)){
        config_str parsed = yon_file_ls(active,&size);
        for (int i=0;i<size;i++){
            gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(widgets->ZoneCombo),parsed[i],_(parsed[i]));
        }
        if (size) yon_char_parsed_free(parsed,size);
        gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->ZoneCombo),0);
    }
    free(active);
}

void yon_region_init(main_window *widgets){
    int size;
    config_str parsed = NULL;
    int langsize;
    config_str lang = default_langs(&langsize);

    GtkTreeIter iter;
    gtk_list_store_clear(widgets->LanguagesList);
    parsed = yon_file_open(languages_path,&size);
    for (int i=0;i<size;i++){
        int cur_size;
        yon_char_remove_last_symbol(parsed[i],'\n')
        config_str cur = yon_char_parse(parsed[i],&cur_size,";");
        if (cur_size){
            gtk_list_store_append(widgets->LanguagesList,&iter);
            gtk_list_store_set(widgets->LanguagesList,&iter,0,0,1,_(cur[1]),2,cur[0],-1);
        }
        yon_char_parsed_free(cur,cur_size);
    }
    yon_char_parsed_free(parsed,size);
    if (lang)
        yon_char_parsed_free(lang,langsize);

    parsed = yon_dir_get_contents(zone_path,&size);
    for (int i=0;i<size;i++){
        if (!strstr(parsed[i],".")&&strcmp(parsed[i],"posix")&&strcmp(parsed[i],"right")){
            char *path = yon_char_append(zone_path,parsed[i]);
            if (yon_file_is_directory(path)){
                gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(widgets->RegionCombo),parsed[i],_(parsed[i]));
            }
            free(path);
        }
    }
        while(gtk_events_pending()) gtk_main_iteration();
    gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->RegionCombo),0);
    yon_char_parsed_free(parsed,size);

    gtk_tree_model_filter_set_visible_column(GTK_TREE_MODEL_FILTER(widgets->LanguagesFilter),0);
    gtk_tree_model_filter_refilter(GTK_TREE_MODEL_FILTER(widgets->LanguagesFilter));
    
    char *zone = yon_char_new(config(zone_parameter));
    char *locale = config(locale_parameter);
    char *lang_param = config(lang_parameter);

    if (!yon_char_is_empty(zone)){
        char *timezone = yon_char_divide_search(zone,"/",-1);
        gtk_combo_box_set_active_id(GTK_COMBO_BOX(widgets->RegionCombo),timezone);
        while(gtk_events_pending()) gtk_main_iteration();
        gtk_combo_box_set_active_id(GTK_COMBO_BOX(widgets->ZoneCombo),zone);
        gtk_switch_set_active(GTK_SWITCH(widgets->RegionSensitiveSwitch),1);
        free(timezone);
    } else {
        gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->RegionCombo),0);
        gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->ZoneCombo),0);
        gtk_switch_set_active(GTK_SWITCH(widgets->RegionSensitiveSwitch),0);
    }

    if (!yon_char_is_empty(locale)){
        char *parameters_list = "";
        config_str parsed = yon_char_parse(locale,&size,",");
        GtkTreeModel *model = GTK_TREE_MODEL(widgets->LanguagesList);
        for_iter(model,&iter){
            char *target, *id;
            gtk_tree_model_get(model,&iter,1,&target,2,&id,-1);
            if (yon_char_parsed_check_exist(parsed,size,id)>-1){
                gtk_list_store_set(widgets->LanguagesList,&iter,0,1,-1);
                char *temp = yon_char_unite(parameters_list,!yon_char_is_empty(parameters_list)?",":"",target,NULL);
                if (!yon_char_is_empty(parameters_list)) free(parameters_list);
                parameters_list=temp;
            } else {
                gtk_list_store_set(widgets->LanguagesList,&iter,0,0,-1);
            }
        }
        gtk_entry_set_text(GTK_ENTRY(widgets->AvailableLanguagesEntry),parameters_list);
        if(!yon_char_is_empty(parameters_list)) free(parameters_list);

    }

    if (!yon_char_is_empty(lang_param)){
        gtk_combo_box_set_active_id(GTK_COMBO_BOX(widgets->LanguagesCombo),lang_param);
    }

}