#include "my_device.h"

vector vec_disk;

vector *get_device_vec()
{
    return &vec_disk;
}

void device_disk_parsed() {
    char *cmd = "findmnt -lo source,target,fstype,label,size -t ext4";
    device_device_config_free(get_device_vec());
    vector_init(get_device_vec());
    int size = 0;
    char **responce = yon_config_load(cmd, &size);
    char *split_simvol = "    ";
    for (int index = 1; index < size; index++)
    {
        char *disk = yon_char_divide_search(responce[index], "\n", -1);
        if (yon_char_find_count(disk, " ") != 0 && !strstr(disk, "zram") && !strstr(disk, "["))
        {
            int size_str = 0;
            char** name_disk = yon_char_parse(disk, &size_str, " ");
            int flag = 0;
            device_config* obj_device_config = malloc(sizeof(device_config));
            for (int index_1 = 0; index_1 < size_str; index_1++) {
                if (strlen(name_disk[index_1])) {
                    if (flag == 0) {
                        obj_device_config->name_disk = yon_char_new(name_disk[index_1]);
                    }
                    else if (flag == 1) {
                        obj_device_config->mounted = yon_char_new(name_disk[index_1]); 
                    }
                    else if (flag == 2) {
                        obj_device_config->file_system = yon_char_new(name_disk[index_1]);
                    }
                    else if (flag == 3) {
                        obj_device_config->type_dick = yon_char_new(name_disk[index_1]);
                    }
                    else if (flag == 4) {
                        obj_device_config->size_disk = philos_convert_memory(yon_char_new(name_disk[index_1]));
                    }
                    flag+=1;
                } 
            }
            obj_device_config->description_disk = yon_char_unite(obj_device_config->name_disk,split_simvol,
                                    obj_device_config->file_system,
                                    split_simvol,
                                    obj_device_config->mounted,
                                    split_simvol,
                                    obj_device_config->type_dick, NULL);
            get_device_vec()->pfVectorAdd(get_device_vec(), obj_device_config);
            philos_free_string_array(&name_disk, size_str);
        }
        free(disk);
    }
    philos_free_string_array(&responce, size);
}


void device_fill_disk(GtkWidget *combo_box_text, vector* vec_filt)
{   
    vector vec;
    vector_init(&vec);
    vec.pfVectorCopy(&vec, get_device_vec());
    for (int i = 0; i < vec_filt->vectorList.total; i++) {
        disk_status* _config_disk_remove = (disk_status*)vec_filt->pfVectorGet(vec_filt, i);
        for (int index = 0; index < vec.vectorList.total; index++) {
            device_config* _config_disk = (device_config*)vec.pfVectorGet(&vec, index);
            if (strcmp(_config_disk->name_disk, _config_disk_remove->device) == 0) {
                vec.pfVectorDelete(&vec, index);
            }
            
        }
    }
    
    if (vec.vectorList.total > 0) {
        for (int index = 0; index < vec.vectorList.total; index++)
        {
            device_config* _config_disk_remove = (device_config*)vec.pfVectorGet(&vec, index);
            gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo_box_text), yon_char_new(_config_disk_remove->description_disk));
        }
    }
}

void device_device_config_free() {
    if (get_device_vec()) {
        for (int i = 0; i < get_device_vec()->vectorList.total; i++) {
            device_config* _config_disk_remove = (device_config*)get_device_vec()->pfVectorGet(get_device_vec(), i);
            free(_config_disk_remove->description_disk);
            free(_config_disk_remove->type_dick);
            free(_config_disk_remove->mounted);
            free(_config_disk_remove->file_system);
            free(_config_disk_remove->name_disk);
            get_device_vec()->pfVectorDelete(get_device_vec(), i);
        }
    }
    
}