import os
import sys
from pathlib import Path
from shutil import copyfile, copytree, ignore_patterns
from typing import List
from urllib.parse import urlparse


def console_log(message):
    print(f"[emr-cli]: {message}")


def find_files(directory, excluded_dirs=[], search=None) -> List[str]:
    files = []
    for root, dirs, filenames in os.walk(directory):
        dirs[:] = [d for d in dirs if d not in excluded_dirs]
        for filename in filenames:
            if search is None or filename == search or filename.endswith(search):
                files.append(os.path.join(root, filename))
    return files


def parse_bucket_uri(uri: str) -> List[str]:
    result = urlparse(uri, allow_fragments=False)
    return [result.netloc, result.path.strip("/")]


def mkdir(path: str):
    try:
        os.mkdir(path)
    except FileExistsError:
        pass


def copy_template(source: str, target_dir: str):
    """
    Copies the entire `source` directory to `target_dir`.
    """
    source = os.path.abspath(Path(__file__).parent.parent / "templates" / source)
    if sys.version_info.major == 3 and sys.version_info.minor == 7:
        py37_copytree(source, target_dir, ignore=ignore_patterns("__pycache__"))
    else:
        copytree(
            source,
            target_dir,
            dirs_exist_ok=True,
            ignore=ignore_patterns("__pycache__"),
        )


def py37_copytree(src, dest, ignore=None):
    """
    A Python3 3.7 version of shutils.copytree since `dirs_exist_ok` was introduced in 3.8
    """
    if os.path.isdir(src):
        if not os.path.isdir(dest):
            os.makedirs(dest)
        files = os.listdir(src)
        if ignore is not None:
            ignored = ignore(src, files)
        else:
            ignored = set()
        for f in files:
            if f not in ignored:
                py37_copytree(os.path.join(src, f), os.path.join(dest, f), ignore)
    else:
        copyfile(src, dest)
