# =============================================================================
# C O P Y R I G H T
# -----------------------------------------------------------------------------
# Copyright (c) 2019-2022 by Helmut Konrad Fahrendholz. All rights reserved.
# This file is property of Helmut Konrad Fahrendholz. Any unauthorized copy,
# use or distribution is an offensive act against international law and may
# be prosecuted under federal law. Its content is company confidential.
# =============================================================================
"""Access path's of global data share. These locations are stored in
global environment variables:

+ SHARED_READY
+ SHARED_SPACE
+ SHARED_TMP
+ SHARED_TODO

Use `configos.export()` to change these variables.
"""

import os
import sys

import utilo

TODO = 'SHARED_TODO'
READY = 'SHARED_READY'
COMMON = 'SHARED_SPACE'
TMP = 'SHARED_TMP'


def ready(check: bool = False) -> str:
    """Return path to finished jobs.

    Args:
        check(bool): check existence of path
    Returns:
        path from global variable
    Raises:
        SystemExit if check is True and path does not exists
        SystemExit if global env var does not exist
    """
    return _path_from_env(READY, check=check)


def todo(check: bool = False) -> str:
    """Return path todo with jobs to work.

    Args:
        check(bool): check existence of path
    Returns:
        path from global variable
    Raises:
        SystemExit if check is True and path does not exists
        SystemExit if global env var does not exist
    """
    return _path_from_env(TODO, check=check)


def share(check: bool = False) -> str:
    """Return path to shared folder.

    Args:
        check(bool): check existence of path
    Returns:
        path from global variable
    Raises:
        SystemExit if check is True and path does not exists
        SystemExit if global env var does not exist
    """
    return _path_from_env(COMMON, check=check)


def tmp(check: bool = False) -> str:
    """Return path to tmp folder.

    Args:
        check(bool): check existence of path
    Returns:
        path from global variable
    Raises:
        SystemExit if check is True and path does not exists
        SystemExit if global env var does not exist
    """
    return _path_from_env(TMP, check=check)


def export(common: str, todo: str, ready: str):  # pylint:disable=W0621
    """Set environment variables"""
    os.environ[COMMON] = str(common)
    os.environ[TODO] = str(todo)
    os.environ[READY] = str(ready)


def environment(check: bool = False):
    """Return `SHARED_TODO` and `SHARED_READY` folder."""
    todo_ = todo(check=check)
    ready_ = ready(check=check)
    common = share(check=check)

    return common, todo_, ready_


def _path_from_env(env: str, check: bool = False):  # pylint:disable=R1710
    """Access global env variable."""
    assert env
    try:
        path = os.environ[env]
        if check and not os.path.exists(path):
            utilo.error('Path does not exists: %s' % path)
            sys.exit(utilo.FAILURE)
        return path
    except KeyError:
        utilo.error('Missing environment var `%s`' % env)
        sys.exit(utilo.FAILURE)


def makedirs():
    """Ensure that all working directories are created."""
    os.makedirs(ready(), exist_ok=True)
    os.makedirs(share(), exist_ok=True)
    os.makedirs(tmp(), exist_ok=True)
    os.makedirs(todo(), exist_ok=True)


def check_startup():
    """Check that global environments are set."""
    ready(check=True)
    share(check=True)
    tmp(check=True)
    todo(check=True)
