import json
from pathlib import Path


def _get_trader_dir(temp_name: str):
    """
    Get path where trader is running in.
    """
    cwd = Path.cwd()
    temp_path = cwd.joinpath(temp_name)

    # If trader folder exists in current working directory,
    # then use it as trader running path.
    if temp_path.exists():
        return cwd, temp_path

    # Create .vntrader folder under home path if not exist.
    if not temp_path.exists():
        temp_path.mkdir()

    return cwd, temp_path


TRADER_DIR, TEMP_DIR = _get_trader_dir("trader")


def get_folder_path(folder_name: str):
    """
    Get path for temp folder with folder name.
    """
    folder_path = TEMP_DIR.joinpath(folder_name)
    if not folder_path.exists():
        folder_path.mkdir()
    return folder_path


def get_file_path(filename: str):
    """
    Get path for temp file with filename.
    """
    return TEMP_DIR.joinpath(filename)


def load_json(filename: str):
    """
    Load data from json file in temp path.
    """
    file_path = get_file_path(filename)

    if file_path.exists():
        with open(file_path, mode="r", encoding="UTF-8") as f:
            data = json.load(f)
        f.close()
        return data
    else:
        save_json(filename, {})
        return {}


def save_json(filename: str, data: dict):
    """
    Save data into json file in temp path.
    """
    file_path = get_file_path(filename)
    with open(file_path, mode="w+", encoding="UTF-8") as f:
        json.dump(
            data,
            f,
            indent=4,
            ensure_ascii=False
        )
    f.close()


def load_file(filename: str):
    file_path = get_file_path(filename)
    lines = None
    if file_path.exists():
        with open(file_path, mode="r", encoding="UTF-8") as f:
            lines = f.readlines()
        f.close()
    return lines
