#include "model.h"

size_t check_null(size_t num) {
    if (num == 0) {
        return -1;
    }
    return num;
}

char* check_null_to_str(size_t num) {
    if (num == 0) {
        return yon_char_new("-");
    }
    return philos_char_from_size_t(num);
}

void model_clear_cfg(config_u_g_p* _config) {
    free(_config->quota);
    free(_config->size_memory);
    free(_config->device);
    free(_config->UNIX_file);
    free(_config->files);
    model_free_vector_string(&_config->actors);
    vectorFree(&_config->actors);
    free(_config->loaded_keys);
}

void model_clear_cfg_device(disk_status* _config) {
    free(_config->device);
    free(_config->fs);
    free(_config->mount_point);
    free(_config->name);
}

void model_free_vector_string(vector* vec_str) {
    if (vec_str) {
        if (vec_str->vectorList.total> 0) {
            for (int i = 0; i < vec_str->vectorList.total; i++) {
                vectorDelete(vec_str, i);
            }
        }
    }
}
void model_free_vector_disk_status(vector* vec_disk_status) {
    if (vec_disk_status) {
        if (vec_disk_status->vectorList.total> 0) {
            for (int i = 0; i < vec_disk_status->vectorList.total; i++) {
                disk_status* _config = (disk_status*)vec_disk_status->pfVectorGet(vec_disk_status, i);
                // model_clear_cfg_device(_config);
                vectorDelete(vec_disk_status, i);
            }
        }
    }
}

void model_tree_view_add(config_u_g_p* _config) {
    model_null_str_writing(_config);
}

void model_null_str_writing(config_u_g_p* _config) {
    _config->type_arr = yon_char_new(empty_str);
    _config->quota = yon_char_new(empty_str);
    _config->size_memory = yon_char_new(empty_str);
    _config->device = yon_char_new(empty_str);
    _config->UNIX_file = yon_char_new(empty_str);
    _config->files = yon_char_new(empty_str);
    _config->loaded_keys = yon_char_new(empty_str);
    _config->type_arr = yon_char_new(empty_str);
    _config->id = 0;
    _config->soft_restriction_size = 0;
    _config->soft_restriction_file = 0;
    _config->severe_limitation_size = 0;
    _config->severe_limitation_file = 0;
    _config->deferring_size = 0;
    _config->deferring_file = 0;
    _config->size_memory_pow = 0;
    _config->soft_restriction_size_pow = 0;
    _config->severe_limitation_size_pow = 0;
    vector_init(&_config->actors);
}

char* format_actors(vector* vec_actors) {
    char* str_all = yon_char_new("");
    char split_simvol[2] = {0};
    split_simvol[0] = 0;
    split_simvol[1] = 0;
    char* str_actors = NULL;
    if (vec_actors->vectorList.total> 0) {
        for (int i = 0; i < vec_actors->vectorList.total; i++) {
            str_actors = (char*)vec_actors->pfVectorGet(vec_actors, i);
            if (str_actors) {
                str_all = yon_char_unite(str_all,split_simvol, str_actors, NULL);
                split_simvol[0] = ',';
                split_simvol[1] = '\0';
            }
        }
    }
    
    if (strlen(str_all) < 3) {
        return yon_char_new("-");
    }
    return str_all;
}

int fill_tree_view_project(config_all* all_config,hotebook* widget , char* str_fill, int flag_gui_add) {
    GtkTreeModel *model = GTK_TREE_MODEL(widget->liststore);
    if (flag_gui_add == 0) {
        g_object_ref(widget->liststore);
        gtk_list_store_clear(widget->liststore);
    }
    int flag_searhc = 0;
    for (int i = 0; i < all_config->v_project.pfVectorTotal(&all_config->v_project); i++) {
        config_u_g_p* _config = (config_u_g_p*)all_config->v_project.pfVectorGet(&all_config->v_project, i);
        if (strcmp(_config->type_arr, str_fill) == 0) {
            GtkTreeIter iter;
            flag_searhc++;
            gtk_list_store_append(widget->liststore,&iter);
            char* deferring_size = wrapper_str_time(_config->deferring_size);
            char* deferring_file = wrapper_str_time(_config->deferring_file);
            gtk_list_store_set(widget->liststore,&iter,
                    0,model_find_path_to_disk(&all_config->v_disk_status, _config->UNIX_file),
                    1,_config->UNIX_file,
                    2,fill_tree_view_id(_config->id),
                    3,format_actors(&_config->actors),
                    4,philos_format_cfg_str_size_memory("", check_null(_config->soft_restriction_size),_config->soft_restriction_size_pow),
                    5,philos_format_cfg_str_size_memory("", check_null(_config->severe_limitation_size),_config->severe_limitation_size_pow),
                    6,deferring_size,
                    7,check_null_to_str(_config->soft_restriction_file),
                    8,check_null_to_str(_config->severe_limitation_file),
                    9,deferring_file,
                    -1);
        
        }
    }
    if (flag_gui_add != 0) {
        gtk_tree_view_set_model(GTK_TREE_VIEW(widget->tree_view), model);
    }
    return flag_searhc;
}

int fill_tree_view_user_group(config_all* all_config, hotebook* widget, char* str_fill ,int flag_gui_add) {
    GtkTreeModel *model = GTK_TREE_MODEL(widget->liststore);
    if (flag_gui_add == 0) {
        g_object_ref(widget->liststore);
        gtk_list_store_clear(widget->liststore);
    }
    int flag_searhc = 0;
    vector* vec_temp = NULL;
    if (strcmp(str_fill, "usrquota")==0) {
        vec_temp = &all_config->v_user;
    }
    else {
        vec_temp = &all_config->v_group;
    }
    if (vec_temp->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);
            if (strcmp(_config->type_arr, str_fill) == 0) {
                GtkTreeIter iter;
                flag_searhc++;
                gtk_list_store_append(widget->liststore,&iter);
                char* deferring_size = wrapper_str_time(_config->deferring_size);
                char* deferring_file = wrapper_str_time(_config->deferring_file);
                gtk_list_store_set(widget->liststore,&iter,
                                0,format_actors(&_config->actors),
                                1,_config->UNIX_file,
                                2,philos_format_cfg_str_size_memory("", check_null(_config->soft_restriction_size),_config->soft_restriction_size_pow),
                                3,philos_format_cfg_str_size_memory("", check_null(_config->severe_limitation_size),_config->severe_limitation_size_pow),
                                4,deferring_size,
                                5,check_null_to_str(_config->soft_restriction_file),
                                6,check_null_to_str(_config->severe_limitation_file),
                                7,deferring_file,
                                -1);
            }
        }
    }
    if (flag_gui_add != 0) {
        gtk_tree_view_set_model(GTK_TREE_VIEW(widget->tree_view), model);
    }
    return flag_searhc;
}

char* wrapper_str_time(size_t seconds) {
    me_time t;
    if (seconds>0) {
        t = time_convert(seconds);
        t.str_time =  yon_char_unite(yon_char_from_int(t.weeks),WEEKS, 
                yon_char_from_int(t.days), DAYS,
                yon_char_from_int(t.hours), HOURS,
                yon_char_from_int(t.minutes), MINUTES, NULL);
    }
    else {
        t.str_time = yon_char_new("-");
    }
    
    return t.str_time;
}

char* quota_type(char* str_quota) {
    if (strcmp(str_quota, "usrquota")==0) {
        return STR_USER;
    }
    else if (strcmp(str_quota, "grpquota")==0) {
        return STR_GROUP;
    }
    else if (strcmp(str_quota, "prjquota")==0) {
        return STR_PROJECT;
    }
}
char quota_fill_generate(char* str_quota, config_u_g_p* _config, GtkListStore* list_store, GtkTreeIter* iter) {
    int index = -1;
    if (strcmp(str_quota, "usrquota")==0) {
        index = 4;
    }
    else if (strcmp(str_quota, "grpquota")==0) {
        index = 5;
    }
    else if (strcmp(str_quota, "prjquota")==0) {
        index = 6;
    }
    if (index != -1) {
        gtk_list_store_set(list_store,iter,index,format_actors(&_config->actors), -1);
    }
    
}

int pars_status_generate(vector* vec_status, config_u_g_p* _config_u_g_p) {
    int flag = 0;
    if (vec_status->vectorList.total > 0) {
        for (int i = 0; i < vec_status->vectorList.total; i++) {
            disk_status* _config = (disk_status*)vec_status->pfVectorGet(vec_status, i);
            if (strstr(_config_u_g_p->type_arr,"prjquota")) {
                if (strstr(_config_u_g_p->UNIX_file, _config->mount_point)) {
                    if (_config->status == 0 ||  _config->status == 1) {
                        flag = 0;
                    }
                    else {
                        flag = 1;
                    }
                }
            }
            else {
                if (strstr(_config_u_g_p->UNIX_file, _config->device)) {
                    if (_config->status == 0 ||  _config->status == 1) {
                        flag = 0;
                    }
                    else {
                        flag = 1;
                    }
                }
            }
            
        }
    }
    return flag;
}

int fill_tree_view_generate(vector* vec_fill, vector* vec_status_disk, char* str_fill, GtkWidget* tree_view, GtkListStore* liststoreGeneral) {
    GtkTreeModel *model = GTK_TREE_MODEL(liststoreGeneral);
    if (vec_fill->vectorList.total > 0) {
        for (int i = 0; i < vec_fill->vectorList.total; i++) {
            config_u_g_p* _config = (config_u_g_p*)vec_fill->pfVectorGet(vec_fill, i);
            GtkTreeIter iter;
            gtk_list_store_append(liststoreGeneral,&iter);
            char* deferring_size = wrapper_str_time(_config->deferring_size);
            char* deferring_file = wrapper_str_time(_config->deferring_file);
            quota_fill_generate(str_fill, _config,liststoreGeneral,&iter);
            gtk_list_store_set(liststoreGeneral,&iter,
                    0,pars_status_generate(vec_status_disk,_config),
                    1,_config->quota,
                    2,quota_type(str_fill),
                    3,_config->UNIX_file,
                    7,philos_format_cfg_str_size_memory("", check_null(_config->soft_restriction_size),_config->soft_restriction_size_pow),
                    8,philos_format_cfg_str_size_memory("", check_null(_config->severe_limitation_size),_config->severe_limitation_size_pow),
                    9,deferring_size,
                    10,check_null_to_str(_config->soft_restriction_file),
                    11,check_null_to_str(_config->severe_limitation_file),
                    12,deferring_file,
                    -1);
        }
    }
    gtk_tree_view_set_model(GTK_TREE_VIEW(tree_view), model);
    return 1;
}

int pars_status_device(disk_status* _config) {
    if (_config->status == 2) {
        return 1;
    }
    return 0;
}

int fill_tree_view_device(vector* vec_device, hotebook* widget, int flag_gui_add) {
    GtkTreeModel *model = GTK_TREE_MODEL(widget->liststore);
    if (flag_gui_add == 0) {
        g_object_ref(widget->liststore);
        gtk_list_store_clear(widget->liststore);
    }
    int flag_searhc = 0;
    if (vec_device->vectorList.total > 0) {
        for (int i = 0; i < vec_device->vectorList.total; i++) {
            disk_status* _config = (disk_status*)vec_device->pfVectorGet(vec_device, i);
            GtkTreeIter iter;
            flag_searhc++;
            gtk_list_store_append(widget->liststore,&iter);
            char* deferring_size = wrapper_str_time(_config->deferring_size);
            char* deferring_file = wrapper_str_time(_config->deferring_file);
            gtk_list_store_set(widget->liststore,&iter,
                            0,pars_status_device(_config),
                            1,_config->device,
                            2,_config->mount_point,
                            3,_config->fs,
                            4,_config->name,
                            5,deferring_size,
                            6,deferring_file,
                            -1);
        }
    }
    if (flag_gui_add != 0) {
        gtk_tree_view_set_model(GTK_TREE_VIEW(widget->tree_view), model);
    }
    return flag_searhc;
}
void del_disk_to_vector_device(vector* vec_temp, char* str_disk) {
    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);
        if (strstr(_config->UNIX_file, str_disk)) {
            vec_temp->pfVectorDelete(vec_temp, i);
        }
    }
}

char* format_str_del_config(config_u_g_p* _config) {
    char* space = g_malloc0(sizeof(char)*2);
    char* actors = yon_char_new("");
    char* cmd = 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);
    }
    for (int i = 0; i < _config->actors.vectorList.total; i++) {
        actors = yon_char_unite(actors, space, _config->actors.pfVectorGet(&_config->actors, i), NULL);
        space[0] = ',';
        space[1] = '\0';
    }
    if (strstr(_config->type_arr, "prjquota")) {
        if (_config->actors.vectorList.total == 0) {
            cmd = yon_char_unite(_config->type_arr, ":", unix_file, ":",fill_tree_view_id(_config->id), NULL);
        }
        else {
            cmd = yon_char_unite(_config->type_arr, ":", unix_file, ":",fill_tree_view_id(_config->id), ",", actors, NULL);
        }
        
    }
    else {
        cmd = yon_char_unite(_config->type_arr, ":", unix_file, ":", actors, NULL);
    }
    free(space);
    free(unix_file);
    return cmd;
}

void model_template_remove(vector* vec_temp, vector* vec_remove, int index) {
    if (index!=-1) {
        config_u_g_p* _config = (config_u_g_p*)vec_temp->pfVectorGet(vec_temp, index);
        vec_remove->pfVectorAdd(vec_remove, format_str_del_config(_config));
        model_clear_cfg(_config);
        vec_temp->pfVectorDelete(vec_temp, index);
    } 
}

void model_search_disk_remove(vector* vec_temp, vector* vec_remove, char* str_disk) {
    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);
        if (strstr(_config->UNIX_file, str_disk)) {
            model_template_remove(vec_temp,vec_remove, i);
        }
    }
}

char* model_find_path_to_disk(vector* vec_temp, char* str_path) {
    for (int i = 0; i < vec_temp->vectorList.total; i++) {
        disk_status* _config = (disk_status*)vec_temp->pfVectorGet(vec_temp, i);
        if (strstr(str_path, _config->mount_point)) {
            return _config->device;
        }
    }
    return "-";
}

void model_search_disk_remove_project(vector* vec_temp,vector* vec_status, vector* vec_remove, char* str_disk) {
    for (int i = 0; i < vec_temp->vectorList.total; i++) {
        config_u_g_p* _config_project = (config_u_g_p*)vec_temp->pfVectorGet(vec_temp, i);
        for (int j = 0; j < vec_status->vectorList.total; j++) {
            disk_status* _config = (disk_status*)vec_status->pfVectorGet(vec_status, j);
            if (strstr(_config_project->UNIX_file, _config->mount_point)) {
                model_template_remove(vec_temp,vec_remove, i);
            }
        }
    }
}

void model_format_del_str(config_u_g_p* _config) {
    char* loaded_keys = yon_char_new("");
    if (strstr(_config->type_arr,"prjquota")) {
        if (strcmp(format_actors(&_config->actors), "-") == 0) {
            loaded_keys = yon_char_unite(_config->type_arr,":" , _config->UNIX_file,":", fill_tree_view_id(_config->id), ",", format_actors(&_config->actors), NULL); 
        }
        else {
            loaded_keys = yon_char_unite(_config->type_arr,":" , _config->UNIX_file,":", fill_tree_view_id(_config->id), NULL); 
        }
    }
    else {
        if (strcmp(format_actors(&_config->actors), "-") == 0) {
            loaded_keys = yon_char_unite(_config->type_arr,":" , _config->UNIX_file, NULL); 
        }
        else {
            loaded_keys = yon_char_unite(_config->type_arr,":" , _config->UNIX_file,":", format_actors(&_config->actors), NULL); 
        }
    }
    _config->loaded_keys = loaded_keys;
}