#ifndef SAVE_H
#define SAVE_H
#include "save.h"

char* save_str_users(config_u_g_p* config, int index, char* start_split) {
    char* cmd = yon_char_new("");
    char split_simvol[2] = {0};
    split_simvol[0] = 0;
    split_simvol[1] = 0;
    vector* vec_actors = &config->actors;
    char* str_actors = NULL;
    if (vec_actors->vectorList.total> 0) {
        for (int i = 0; i < vec_actors->vectorList.total; i++) {
            if (i==0) {
                cmd = yon_char_unite(cmd, start_split, NULL);
            }
            str_actors = (char*)vec_actors->pfVectorGet(vec_actors, i);
            cmd = yon_char_unite(cmd, split_simvol, str_actors, NULL);
            split_simvol[0] = ',';
            split_simvol[1] = '\0';
        }
    }
    return cmd;

}

char* template_format_save(vector* vec_temp) {
    char* cmd = yon_char_new("");
    if (vec_temp->vectorList.total> 0) {
        for (int i = 0; i < vec_temp->pfVectorTotal(vec_temp); i++) {
            config_u_g_p* _config = (config_u_g_p*)vec_temp->pfVectorGet(vec_temp, i);
            char* key = save_format_key(_config, i);
            char* value = save_format_str_value(_config, i);
            if (key!= NULL) {
                cmd = yon_char_unite(cmd, key, "=" , value, " ", NULL);
                free(key);
                free(value);
            }
        }
    }
    return cmd;
}

char* save_format_status_cmd_all(vector* vec_temp, vector* vec_status_disk) {
    char* cmd = yon_char_new("");
    char* str_cmd_format = NULL;
    if (vec_temp->vectorList.total > 0 && vec_status_disk->vectorList.total > 0) {
        for (int i=0; i < vec_temp->vectorList.total; i++) {
            config_u_g_p* _config = (config_u_g_p*)vec_temp->pfVectorGet(vec_temp, i);
            for (int j=0; j < vec_status_disk->vectorList.total;j++) {
                disk_status* config_disk_status = (disk_status*)vec_status_disk->pfVectorGet(vec_status_disk, j);
                if (strstr(config_disk_status->device, _config->UNIX_file) ) {
                    str_cmd_format = save_format_status_cmd(config_disk_status, _config);
                    if (str_cmd_format) {
                        cmd = yon_char_unite(cmd, " ", str_cmd_format, NULL);
                        free(str_cmd_format);
                    }
                }
            }
        }
    }
    return cmd;
}

char* set_status_and_del(config_all* _all_config) {
    char* cmd = yon_char_new("");
    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 (config_disk_status->status == 2 && (config_disk_status->deferring_size == 0 && config_disk_status->deferring_file == 0)) {
            cmd = yon_char_unite(cmd, "DISK_QUOTA[usrquota:" , config_disk_status->device, "]=enable ", NULL);
            cmd = yon_char_unite(cmd, "DISK_QUOTA[grpquota:" , config_disk_status->device, "]=enable ", NULL);
            cmd = yon_char_unite(cmd, "DISK_QUOTA[prjquota:" , config_disk_status->device, "]=enable ", NULL);
            yon_window_config_erase_custom_parameter(config_disk_status->device, "deferring_size");
            yon_window_config_erase_custom_parameter(config_disk_status->device, "deferring_file");
            yon_window_config_erase_custom_parameter(config_disk_status->device, config_disk_status->device);
        }
        else if (config_disk_status->status == 1) {
            cmd = yon_char_unite(cmd, "DISK_QUOTA[usrquota:" , config_disk_status->device, "]=disable ", NULL);
            cmd = yon_char_unite(cmd, "DISK_QUOTA[grpquota:" , config_disk_status->device, "]=disable ", NULL);
            cmd = yon_char_unite(cmd, "DISK_QUOTA[prjquota:" , config_disk_status->device, "]=disable ", NULL);
            yon_window_config_erase_custom_parameter(config_disk_status->device, "deferring_size");
            yon_window_config_erase_custom_parameter(config_disk_status->device, "deferring_file");
            yon_window_config_erase_custom_parameter(config_disk_status->device, config_disk_status->device);
        }
        if (config_disk_status->status == 2 && (config_disk_status->deferring_size != 0 || config_disk_status->deferring_file != 0)) {
            cmd = yon_char_unite(cmd, "DISK_QUOTA[usrquota:", config_disk_status->device, "]=0:0:0:0:",yon_char_from_int(config_disk_status->deferring_size), ":", yon_char_from_int(config_disk_status->deferring_file)," ", NULL);
            cmd = yon_char_unite(cmd, "DISK_QUOTA[grpquota:", config_disk_status->device, "]=0:0:0:0:",yon_char_from_int(config_disk_status->deferring_size), ":", yon_char_from_int(config_disk_status->deferring_file)," ", NULL);
            cmd = yon_char_unite(cmd, "DISK_QUOTA[prjquota:", config_disk_status->device, "]=0:0:0:0:",yon_char_from_int(config_disk_status->deferring_size), ":", yon_char_from_int(config_disk_status->deferring_file)," ", NULL);
        }
    }
    return cmd;
}

char* wrapper_save_format_status_cmd_all(config_all* _all_config) {
    return set_status_and_del(_all_config);
}

char* save_format_status_cmd(disk_status* config_disk_status, config_u_g_p* config) {
    char* cmd = yon_char_unite("DISK_QUOTA[", config->type_arr, ":", config->UNIX_file,"]=", NULL);;
    if (config_disk_status->status == 2) {
        cmd = yon_char_unite(cmd,"enable", NULL);
        return cmd;
    }
    else if (config_disk_status->status == 1) {
        return yon_char_unite(cmd,"disable", NULL);
    }
    else {
        return NULL;
    }
}

char* wrapper_template_format_save(config_all* _all_config, char* source_set_cmd) {
    char* cmd_user = template_format_save(&_all_config->v_user);
    char* cmd_group = template_format_save(&_all_config->v_group);
    char* cmd_project = template_format_save(&_all_config->v_project);
    char* cmd_status_disk =  wrapper_save_format_status_cmd_all(_all_config);
    if ((strlen(cmd_status_disk) > 3 || strlen(cmd_user) > 3 || strlen(cmd_group) > 3 || strlen(cmd_project) > 3) && !save_check_save(_all_config)) {
        return yon_char_unite(source_set_cmd," ", cmd_status_disk, " ", cmd_user, " ", cmd_group, " ", cmd_project, NULL);
    }
    return NULL;
    
}
char* template_format_remove_save(config_all* _all_config, char* source_remove_cmd) {
    if (_all_config->v_remove.vectorList.total > 0) {
        char* cmd = yon_char_new(source_remove_cmd);
        for (int i = 0; i < _all_config->v_remove.vectorList.total; i++) {
            char* str_remove = (char*)_all_config->v_remove.pfVectorGet(&_all_config->v_remove, i);
            cmd = yon_char_unite(cmd, " DISK_QUOTA[" , str_remove, "] ", NULL);
        }
        if (strlen(cmd) > strlen(source_remove_cmd) && save_check_save(_all_config)== 0) {
            return cmd;
        }
    }
    return NULL;

}
char* template_format_remove_save_device(config_all* _all_config, char* source_remove_cmd) {
    char* cmd = yon_char_new("");
    if (_all_config->v_disk_remove.vectorList.total > 0) {
        for (int i=0; i < _all_config->v_disk_remove.vectorList.total; i++) {
            char* str_del = (char*)_all_config->v_disk_remove.pfVectorGet(&_all_config->v_disk_remove, i);
            if (str_del) {
                cmd = yon_char_unite(cmd, " DISK_QUOTA[usrquota:" , str_del, "] ", NULL);
                cmd = yon_char_unite(cmd, " DISK_QUOTA[grpquota:" , str_del, "] ", NULL);
                cmd = yon_char_unite(cmd, " DISK_QUOTA[prjquota:" , str_del, "] ", NULL);
                yon_window_config_erase_custom_parameter(str_del, "deferring_size");
                yon_window_config_erase_custom_parameter(str_del, "deferring_file");
                yon_window_config_erase_custom_parameter(str_del, str_del);
            }
            
        }
    }
    if (strlen(cmd)>5) {
        for (int i=0; i < _all_config->v_disk_remove.vectorList.total; i++) {
            _all_config->v_disk_remove.pfVectorDelete(&_all_config->v_disk_remove, i);
        }
        return cmd;
    }
    else {
        return NULL;
    }
    
}


int wrapper_template_save(config_all* _all_config, char* source_set_cmd, char* source_remove_cmd) {
    char* str_cmd_remove = template_format_remove_save(_all_config,source_remove_cmd);
    char* str_cmd_remove_device = template_format_remove_save_device(_all_config,source_remove_cmd);
    char* str_cmd_set = wrapper_template_format_save(_all_config,source_set_cmd);
    if (str_cmd_set != NULL && str_cmd_remove != NULL && str_cmd_remove_device != NULL) {
        philos_config_save(yon_char_unite(str_cmd_remove, " " ,str_cmd_remove_device, "; " , str_cmd_set, NULL));
    }
    else {
        if (str_cmd_remove != NULL) {
            philos_config_save(str_cmd_remove);
        }
        if (str_cmd_remove_device != NULL) {
            philos_config_save(yon_char_unite(source_remove_cmd, " " , str_cmd_remove_device, NULL));
        }
        if (str_cmd_set != NULL) {
            philos_config_save(str_cmd_set);
        }
        
    }
    
    if (str_cmd_remove != NULL) {
        free(str_cmd_remove);
    }
    if (str_cmd_set != NULL) {
        free(str_cmd_set);
    }
    if (str_cmd_remove_device != NULL) {
        free(str_cmd_remove_device);
    }
    vector_init(&_all_config->v_remove);
    _all_config->flag_set_data = 0;
    return 1;
}

char* save_format_key(config_u_g_p* config, int index) {
    char* value = yon_char_new("");
    char* key = yon_char_new(""); 
    int number = config->id;
    key = yon_char_unite("DISK_QUOTA[", config->type_arr, ":", NULL);
    char* unix_file = NULL;
    if (strstr(config->UNIX_file, " ") && !strstr(config->UNIX_file, "\"") ) {
        unix_file = yon_char_unite("\"", config->UNIX_file, "\"", NULL);
    }
    else {
        unix_file = yon_char_new(config->UNIX_file);
    }
    if (strstr(config->type_arr, "prjquota")) {
        if (number!=-1) {
            key = yon_char_unite(key, unix_file,":",yon_char_from_int(number), NULL);
        }
        else {
            key = yon_char_unite(key, unix_file,":AUTO", NULL);
        }
        key = yon_char_unite(key, save_str_users(config, index, ",") , "]", NULL);
    }
    else {
        key = yon_char_unite(key, unix_file, NULL);
        key = yon_char_unite(key, save_str_users(config, index, ":") , "]", NULL);
    }
    free(unix_file);
    return key;
}


char* save_format_str_value(config_u_g_p* config, int index) {
    char* value = NULL;
    if (config->soft_restriction_size) {
        value = yon_char_unite(philos_format_cfg_str_size_memory("", config->soft_restriction_size,config->soft_restriction_size_pow), NULL);
    }
    else {
        value = yon_char_new("0");
    }
    if (config->severe_limitation_size) {
        value = yon_char_unite(value, ":", philos_format_cfg_str_size_memory("", config->severe_limitation_size,config->severe_limitation_size_pow), NULL);
    }
    else {
        value = yon_char_unite(value,":0", NULL);
    }
    value = yon_char_unite(value, ":", yon_char_from_int(config->soft_restriction_file), NULL);
    value = yon_char_unite(value, ":", NULL);
    value = yon_char_unite(value, yon_char_from_int(config->severe_limitation_file), NULL);
    value = yon_char_unite(value, ":", NULL);
    value = yon_char_unite(value, philos_char_from_size_t(config->deferring_size), ":", NULL);
    value = yon_char_unite(value, philos_char_from_size_t(config->deferring_file), NULL);
    return value;
}

int save_check_save(config_all* _all_config) {
    if (_all_config->flag_set_data == 1 || _all_config->flag_save>0) {
        return 0;
    }
    else if (_all_config->flag_set_data == 0 || _all_config->flag_save<=0) {
        return 1;
    }
    return 0;
}


#endif