#include "load.h"


void load_template_load_cfg(config_all* _all_config, char* cmd) {
    int size = 0;
    char** cfg = yon_config_load(cmd, &size);
    philos_array_string_remove_char(&cfg, "\n", size);
    int size_loaded_keys = 0;
    load_free_vector(_all_config, &_all_config->v_user);
    load_free_vector(_all_config, &_all_config->v_group);
    load_free_vector(_all_config, &_all_config->v_project);
    free_remove_vector(_all_config, &_all_config->v_remove);
    model_free_vector_string(&_all_config->v_disk_remove);
    device_disk_parsed();
    vector_init(&_all_config->v_user);
    vector_init(&_all_config->v_group);
    vector_init(&_all_config->v_project);
    vector_init(&_all_config->v_disk_status);
    vector_init(&_all_config->v_remove);
    vector_init(&_all_config->v_disk_remove);
    if (size == 1 && strcmp(cfg[0], "(null)") == 0) {
        for (int index_1 = 0; index_1 < get_device_vec()->vectorList.total; index_1++) {
            char* device = NULL;
            device_config* _config_disk = (device_config*)get_device_vec()->pfVectorGet(get_device_vec(), index_1);
            if (yon_window_config_get_parameter(_config_disk->name_disk, _config_disk->name_disk, &device, YON_TYPE_STRING)) {
                if (strstr(device, _config_disk->name_disk)) {
                    size_t deferring_size = 0;
                    size_t deferring_file = 0;
                    disk_status* disk_cfg =  g_malloc0(sizeof(disk_status));
                    disk_cfg->status = 1;
                    _all_config->v_disk_status.pfVectorAdd(&_all_config->v_disk_status, disk_cfg);
                    disk_cfg->device = yon_char_new(device);
                    disk_cfg->fs = yon_char_new(_config_disk->file_system);
                    disk_cfg->mount_point = yon_char_new(_config_disk->mounted);
                    disk_cfg->name = yon_char_new(_config_disk->type_dick);
                    if (yon_window_config_get_parameter("deferring_size", _config_disk->name_disk, &deferring_size, YON_TYPE_INT)) {
                        disk_cfg->deferring_size = deferring_size;
                    }
                    else {
                        disk_cfg->deferring_size = 0;
                    }
                    if (yon_window_config_get_parameter("deferring_file", _config_disk->name_disk, &deferring_file, YON_TYPE_INT)) {
                        disk_cfg->deferring_file = deferring_file;
                    }
                    else {
                        disk_cfg->deferring_file = 0;
                    }
                }
            }
        }
    }
    for (int index = 0; index < size; index++) {
        char* str_key_value = yon_char_new(cfg[index]);
        char* str_key = yon_char_divide_search(str_key_value, "=",-1);
        yon_char_divide_search(str_key, "[",-1);
        str_key = yon_char_divide_search(str_key, "]",-1);
        char* str_value = str_key_value;
        int flag_find = 0;
        for (int j=0; j < _all_config->v_disk_status.vectorList.total;j++) {
            disk_status* config_disk_status = (disk_status*)_all_config->v_disk_status.pfVectorGet(&_all_config->v_disk_status, j);
            if (strstr(str_key, config_disk_status->device)) {
                flag_find = 1;
            }
        }
        if (((strstr(str_value, "enable") || strstr(str_value, "disable")) || yon_char_find_count(str_key, ":") == 1) && flag_find == 0) {
            disk_status* disk_cfg =  g_malloc0(sizeof(disk_status));
            disk_cfg->deferring_size = 0;
            disk_cfg->deferring_file = 0;
            load_pars_values_status(disk_cfg, str_key, str_value);
            _all_config->v_disk_status.pfVectorAdd(&_all_config->v_disk_status, disk_cfg);
            for (int index = 0; index < get_device_vec()->vectorList.total; index++) {
                device_config* _config_disk = (device_config*)get_device_vec()->pfVectorGet(get_device_vec(), index);
                for (int j=0; j < _all_config->v_disk_status.vectorList.total;j++) {
                    disk_status* config_disk_status = (disk_status*)_all_config->v_disk_status.pfVectorGet(&_all_config->v_disk_status, j);
                    if (strstr(config_disk_status->device, _config_disk->name_disk)) {
                        config_disk_status->device = yon_char_new(_config_disk->name_disk);
                        config_disk_status->fs = yon_char_new(_config_disk->file_system);
                        config_disk_status->mount_point = yon_char_new(_config_disk->mounted);
                        config_disk_status->name = yon_char_new(_config_disk->type_dick);
                        if (yon_char_find_count(str_key, ":") == 1 && (!strstr(str_value, "enable") && !strstr(str_value, "disable"))) {
                            int size_value = 0;
                            char** arr_values = philos_str_split(str_value, &size_value, ":");
                            config_disk_status->deferring_size = atoll(arr_values[4]);
                            config_disk_status->deferring_file = atoll(arr_values[5]);
                            config_disk_status->status = 2;
                            philos_free_string_array(&arr_values, size_value);
                        }
                        else {
                            
                        }
                        
                    }           
                }
            }
        }
        else if ((strstr(str_value, "enable") || strstr(str_value, "disable")) && flag_find) {

        }
        else {
            if (yon_char_find_count(str_key, ":") == 1) {
                
            }
            else {
                config_u_g_p* _config =  g_malloc0(sizeof(config_u_g_p));
                model_null_str_writing(_config);
                _config->loaded_keys = yon_char_new(str_key);
                load_pars_keys(_config, yon_char_new(str_key), index);
                load_pars_values(_config, _all_config,str_key, yon_char_new(str_value), index);
                
                if (strstr(str_key, "prjquota")) {
                    _all_config->v_project.pfVectorAdd(&_all_config->v_project,_config);
                }
                else if (strstr(str_key, "usrquota")) {
                    _all_config->v_user.pfVectorAdd(&_all_config->v_user,_config);
                }
                else if (strstr(str_key, "grpquota")){
                    _all_config->v_group.pfVectorAdd(&_all_config->v_group,_config);
                }
            }
            
        }
        
    }
    _all_config->flag_set_data = 0;
    _all_config->flag_not_data = 1;
    _all_config->flag_save = 0;
    
}

void load_pars_keys(config_u_g_p* _config, char* str_key, int index) {
    load_pars_keys_u_g(_config, yon_char_new(str_key), index);
    load_pars_keys_p(_config, yon_char_new(str_key), index);
}

void load_pars_keys_u_g(config_u_g_p* _config, char* str_key, int index) {
    if (strstr(str_key, "prjquota")) {
        return;
    }
    if (yon_char_find_count(str_key, ":") == 1) {
        return;
    }
    if (yon_char_find_count(str_key, ":") || yon_char_find_count(str_key, ",")) {
        int size_point = 0;
        char** arr_keys = philos_str_split(yon_char_new(str_key), &size_point, ":");
        _config->type_arr = yon_char_new(arr_keys[0]);
        int size_users = 0;
        if (yon_char_find_count(str_key, ":") == 1) {
            int size_comma = 0;
            char** arr_user = philos_str_split(arr_keys[size_point-1], &size_comma, ",");
            _config->UNIX_file = yon_char_new(arr_keys[1]);
        }
        else if (yon_char_find_count(str_key, ":") == 2) {
            if (yon_char_find_count(str_key, ",")) {
                int size_comma = 0;
                char** arr_user = philos_str_split(arr_keys[size_point-1], &size_comma, ",");
                _config->UNIX_file = yon_char_new(arr_keys[1]);
                for (int i = 0; i < size_comma; i++) {
                    _config->actors.pfVectorAdd(&_config->actors,yon_char_new(arr_user[i]));
                }
            }
            else {
                _config->UNIX_file = yon_char_new(arr_keys[1]);
                if (size_point == 3) {
                    _config->actors.pfVectorAdd(&_config->actors,yon_char_new(arr_keys[2]));
                }
                
            }
        }
    }
    else {

    }
}

void load_pars_keys_p(config_u_g_p* _config, char* str_key, int index) {
    if (yon_char_find_count(str_key, ":") == 1) {
        return;
    }
    if (strstr(str_key, "prjquota")) {
        if (yon_char_find_count(str_key, ":") || yon_char_find_count(str_key, ",")) {
            int size_point = 0;
            char** arr_keys = philos_str_split(yon_char_new(str_key), &size_point, ":");
            _config->type_arr = yon_char_new(arr_keys[0]);
            int size_users = 0;
            if (yon_char_find_count(str_key, ":") == 2) {
                _config->UNIX_file = yon_char_new(arr_keys[1]);
                int size_comma = 0;
                if (yon_char_find_count(str_key, ",")) {
                    char** arr_user = philos_str_split(arr_keys[size_point-1], &size_comma, ",");
                    if (strstr(str_key, "AUTO")) {
                        _config->id = -1;
                    }
                    else {
                        _config->id = atoi(arr_user[0]);
                    }
                    _config->actors.pfVectorAdd(&_config->actors,yon_char_new(arr_user[size_comma-1]));
                }
                else {
                    if (strstr(str_key, "AUTO")) {
                        _config->id = -1;
                    }
                    else {
                        _config->id = atoi(arr_keys[size_point-1]);
                    }
                }
            }  
    }
    }
    else { 
    }
}

void load_pars_values_status(disk_status* _config, char* key, char* value) {
    _config->device = yon_char_new(key);
    if (strstr(value,"enable")) {
        _config->status = 2;
        return;
    }
    else if (strstr(value,"disable")) {
        _config->status = 1;
        return;
    }
}

void load_pars_values(config_u_g_p* _config, config_all* _all_config, char* str_key, char* str_value, int index) {
    int size_array_size_pow = 0;
    char** array_size_pow = NULL;
    array_size_pow = yon_char_parsed_append(array_size_pow,&size_array_size_pow, STR_KB);
    array_size_pow = yon_char_parsed_append(array_size_pow,&size_array_size_pow, STR_MB);
    array_size_pow = yon_char_parsed_append(array_size_pow,&size_array_size_pow, STR_GB);
    array_size_pow = yon_char_parsed_append(array_size_pow,&size_array_size_pow, STR_TB);
    int size = 0;
    char* new_value = yon_char_new(str_value);
    char** arr_values = philos_str_split(new_value, &size, ":");
    int index_split = yon_char_find_count(str_value, ":");
    if (index_split == 3 || index_split == 5) {
        philos_set_pow_size_memory(yon_char_new(arr_values[0]), &_config->soft_restriction_size_pow, array_size_pow);
        philos_set_size_memory_integer(yon_char_new(arr_values[0]), &_config->soft_restriction_size);
        philos_set_pow_size_memory(yon_char_new(arr_values[1]), &_config->severe_limitation_size_pow, array_size_pow);
        philos_set_size_memory_integer(yon_char_new(arr_values[1]), &_config->severe_limitation_size);
        philos_set_size_memory_integer(yon_char_new(arr_values[2]), &_config->soft_restriction_file);
        philos_set_size_memory_integer(yon_char_new(arr_values[3]), &_config->severe_limitation_file);
        if (index_split == 5) {
            _config->deferring_size = atoll(arr_values[4]);
            _config->deferring_file = atoll(arr_values[5]);
        }
        else {
            philos_set_size_memory_integer("-", &_config->deferring_size);
            philos_set_size_memory_integer("-", &_config->deferring_file);
        }
        for (int j=0; j < _all_config->v_disk_status.vectorList.total;j++) {
            disk_status* config_disk_status = (disk_status*)_all_config->v_disk_status.pfVectorGet(&_all_config->v_disk_status, j);
            if (strstr(str_key, config_disk_status->device)){
                config_disk_status->deferring_file = _config->deferring_file;
                config_disk_status->deferring_size = _config->deferring_size;
            }
        }
        
    }
    else {

    }
}

void load_free_vector(config_all* _all_config, vector* vec_config) {
    
    if (_all_config->flag_not_data == 1) {
        for (int i = 0; i < vec_config->pfVectorTotal(vec_config); i++) {
        config_u_g_p* _config = (config_u_g_p*)vec_config->pfVectorGet(vec_config, i);
            model_clear_cfg(_config);
        }
        vec_config->pfVectorFree(vec_config);
    }
    
}

void free_remove_vector(config_all* _all_config, vector* vec_remove) {
    if (_all_config->flag_not_data == 1) {
        if (vec_remove->vectorList.total > 0) {
            for (int i = 0; i < vec_remove->vectorList.total; i++) {
                char* str_remove = (char*)vec_remove->pfVectorGet(vec_remove, i);
                free(str_remove);
            }
            vec_remove->pfVectorFree(vec_remove);
        }
    }
}



