"""Manage a vault in ways that are specific to `trouver`"""

# AUTOGENERATED! DO NOT EDIT! File to edit: ../../../../nbs/27_markdown.obsidian.personal.vault.ipynb.

# %% auto 0
__all__ = ['setup_obsidian_vault_for_trouver']

# %% ../../../../nbs/27_markdown.obsidian.personal.vault.ipynb 2
from pathlib import Path
import os
from os import PathLike

# %% ../../../../nbs/27_markdown.obsidian.personal.vault.ipynb 6
def setup_obsidian_vault_for_trouver(
        vault: PathLike, # The path to the vault to setup.
        verbose: bool = True, # If true, print messages explaining what is created
        prompt: bool = True, # If true, prompt user input for certain decisions, such as whether to create an Obsidian vault folder if `vault` does not exist.
    ):
    # TODO: use this method for vault construction for a reference
    """Setup an `Obsidian.md` vault for `trouver`.
    
    Currently, the following subdirectories are created in `vault` if they do not
    already exist:

    - `_references`, `_templates`
        - Each of these directories are then populated by subdirectories 
          `A-E`, `F-J`, `K-O`, `P-T`, `U-Z`, and those subdirectories are
          further populated by subdirectories whose names are single-letter
          alphabets in the ranges of these names, e.g. `A-E` has subdirectories
          `A`, `B`, `C`, `D`, and `E`.
    """
    _setup_trouver_vault_essentials(Path(vault), verbose, prompt)


def _setup_trouver_vault_essentials(
        vault: PathLike, # The path to the vault to setup
        verbose: bool, # If true, print messages explaining what is created
        prompt: bool, # If true, prompt user input for certain decisions, such as whether to create an Obsidian vault folder if `vault` does not exist.
    ):
    """
    """
    # TODO: setup missing subfolders in the `_references` or `_templates` subfolders even
    for name in ['_references', '_templates']:
        if os.path.exists(vault / name) and verbose:
            print(f"The vault already has a `{name}` folder.")
        elif not os.path.exists(vault / name) and prompt:
            # TODO do input things
            command = input(
                f"""The vault does not have a `{name}` folder.
                Create it and subdirectories?\n[Y/(n)]""")
        # if not os.path.exists(vault / name) and ()
        #     print('')
        if not os.path.exists(vault / name) and (not prompt or command == 'Y'):
            if verbose:
                print(f"Creating `{name}` folder in the vault.")
            _setup_alphabetical_subdirectories(vault, name)



def _setup_alphabetical_subdirectories(
        dir: PathLike, # The path to the directory in which to make the subdirectory with alphabetical subdirectories
        name: str, # The name of the subdirecctory to make in which the alphabetical subdirectories will be made
    ):
    """Create a subdirectory with name ``name`` in the directory ``dir`` and create
    alphabetical subdirectories.
    
    Assumes that ``dir`` exists, but the subdirectory with name ``name`` does not.
    """
    os.mkdir(dir / name)
    sub_names = ['A-E', 'F-J', 'K-O', 'P-T', 'U-Z']
    for sub_name in sub_names:
        os.mkdir(dir / name / sub_name)
        first_letter, last_letter = ord(sub_name[0]), ord(sub_name[-1])
        for i in range(first_letter, last_letter+1):
            os.mkdir(dir / name / sub_name / chr(i))



