#include "libublsettings.h"



char *yon_file_path_proceed_spaces(char *path){
    int size;
    config_str parsed = yon_char_parse(path,&size,"/");
    for (int i=0;i<size;i++){
        if (!yon_char_is_empty(parsed[i])){
            if (strstr(parsed[i]," ")){
                char *temp = yon_char_unite("/",parsed[i],"/",NULL);;
                free(parsed[i]);
                parsed[i]=temp;
            }
        }
    }
    char *final = yon_char_parsed_to_string(parsed,size,"/");
    yon_char_parsed_free(parsed,size);
    char *temp = NULL;
    if (path[0]=='/') temp = yon_char_append("/",final);
    free(final);
    final=temp;
    return final;
}

config_str yon_file_open(char *file_path, int *size){
    *size=0;
    FILE *file = fopen(file_path,"r");
        if (file){
        char str_loaded[4098];
        config_str final_string = NULL;
        while (fgets(str_loaded,4098,file)){
            yon_char_parsed_add_or_create_if_exists(final_string,size,str_loaded);
        }
        fclose(file);
        return final_string;
    }
}

int yon_file_save(char *file_path, char *text){
    FILE *file = fopen(file_path,"w");
    struct passwd *user = getpwnam(yon_ubl_root_user_get());
    if (chown(file_path,user->pw_uid,user->pw_gid)){};
    if (file){
        fputs(text,file);
        fclose(file);
        return 1;
    }
    return 0;
}

int yon_file_create(char *path, char *name, int rules){
    if (path&&name){
        char *full_path = yon_char_unite(path,"/",name,NULL);
        if (access(full_path,F_OK)){
            FILE *fl = fopen(full_path,"w");
            if (fl){
                struct passwd *user = getpwnam(yon_ubl_root_user_get());
                if (chown(path,user->pw_uid,user->pw_gid)){};
                fclose(fl);
                return 1;
            } else {
                return 0;
            }
        } else {
            return -1;
        }
    } else {
        return 0;
    }
}

int yon_file_create_full_path(char *path, mode_t rules){
    if (path){
        if (access(path,F_OK)){
            int size;
            config_str parsed = yon_char_parse(path,&size,"/");
            char *current = "";
            for (int i=0;i<size;i++){
                char *temp = yon_char_unite(current,strcmp(current,"/")?"/":"",parsed[i],NULL);
                if (!yon_char_is_empty(temp)){
                    if (access(temp,F_OK)){
                        if (i!=size-1){
                            mkdir(temp,0755);
                            struct passwd *user = getpwnam(yon_ubl_root_user_get());
                            if (chown(temp,user->pw_uid,user->pw_gid)){};
                        } else {
                            FILE *fl = fopen(temp,"w");
                            struct passwd *user = getpwnam(yon_ubl_root_user_get());
                            if (chown(temp,user->pw_uid,user->pw_gid)){};
                            if (!fl) return 0;
                            fclose(fl);
                        }
                    
                    }
                }
                if (!yon_char_is_empty(current)) free(current);
                current = temp;
            }

            return 1;
        } else {
            return 0;
        }
    }
    return 0;
}

config_str yon_file_list_dirs (char *path, int *size){
    (*size)=0;
    if (!yon_char_is_empty(path)&&!access(path,F_OK)){
        config_str final = NULL;
        DIR *current_dir = opendir(path);
        struct dirent* cur_dir;
        while ((cur_dir=readdir(current_dir))){
            if (cur_dir->d_type==DT_DIR &&(strcmp(cur_dir->d_name, ".") && strcmp(cur_dir->d_name, ".."))){
                yon_char_parsed_add_or_create_if_exists(final,size,cur_dir->d_name);
            }
        }
        closedir(current_dir);
        return final;
    }
    return NULL;
}

config_str yon_file_ls(char *path, int *size){
    (*size)=0;
    if (!yon_char_is_empty(path)&&!access(path,F_OK)){
        config_str final = NULL;
        DIR *current_dir = opendir(path);
        struct dirent* cur_dir;
        while ((cur_dir=readdir(current_dir))){
            if ((strcmp(cur_dir->d_name, ".") && strcmp(cur_dir->d_name, ".."))){
                yon_char_parsed_add_or_create_if_exists(final,size,cur_dir->d_name);
            }
        }
        closedir(current_dir);
        return final;
    }
    return NULL;
}

char *yon_file_get_parent(const char *path){
    if (yon_char_is_empty(path)|| !strstr(path,"/")) return NULL;
    char *parent_path = NULL;
    int size;
    config_str parsed = yon_char_parse(path,&size,"/");
    free(parsed[size-1]);
    parsed[size-1] = NULL;
    parent_path = yon_char_parsed_to_string_full(parsed,size,"/");
    return parent_path;
}

int yon_file_is_directory(const char *path)
{
    struct stat path_stat;
    stat(path, &path_stat);
    return S_ISDIR(path_stat.st_mode);
}

void yon_dir_remove(const char *path){
    DIR *d = opendir(path);
    struct dirent *dir;

    if (d) {
        while ((dir = readdir(d)) != NULL) {
            if (strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0) {
                continue;
            }
            char full_path[1024];
            snprintf(full_path, sizeof(full_path), "%s/%s", path, dir->d_name);

            if (dir->d_type == DT_DIR) {
                yon_dir_remove(full_path);
            } else {
                unlink(full_path);
            }
        }
        closedir(d);
        rmdir(path);
    }
}