#
# Copyright (C) 2023, Inria
# GRAPHDECO research group, https://team.inria.fr/graphdeco
# All rights reserved.
#
# This software is free for non-commercial, research and evaluation use 
# under the terms of the LICENSE.md file.
#
# For inquiries contact  george.drettakis@inria.fr
#

from errno import EEXIST
from os import makedirs, path
import os

def mkdir_p(folder_path):
    # Creates a directory. equivalent to using mkdir -p on the command line
    try:
        makedirs(folder_path)
    except OSError as exc: # Python >2.5
        if exc.errno == EEXIST and path.isdir(folder_path):
            pass
        else:
            raise

def searchForMaxIteration(folder):
    saved_iters = [int(fname.split("_")[-1]) for fname in os.listdir(folder)]
    return max(saved_iters)

def searchForMaxLod(folder):
    saved_lods = [int(fname.split("_")[1]) for fname in os.listdir(folder)]
    return max(saved_lods)

def searchForMaxIterationGivenLod(folder, lod):
    filtered_names = [fname for fname in os.listdir(folder) if fname.startswith(f'lod_{lod}')]
    saved_iters = [int(fname.split("_")[-1]) for fname in filtered_names]    
    return max(saved_iters)


def checkFileSize(file_path, unit='MB'):
    # Get file size in bytes
    size_bytes = os.path.getsize(file_path)

    # Convert to MB or GB
    if unit == 'MB':
        size_mb = size_bytes / (1024 * 1024)
        return size_mb
    elif unit == 'GB':
        size_gb = size_bytes / (1024 * 1024 * 1024)
        return size_gb
    else:
        raise ValueError("Invalid unit. Please use 'MB' or 'GB'.")
    