import logging
import os
from typing import Dict

logger = logging.getLogger("waa_cube.vm_backend.getters.gimp")


def get_gimp_config_file(env, config: Dict[str, str]):
    """
    Gets the config setting of GIMP.
    """

    os_type = env.vm_platform
    logger.debug("get_gimp_config_file: os_type=%s", os_type)

    if os_type == "Linux":
        config_path = env.controller.execute_python_command(
            f"import os; print(os.path.expanduser('~/.config/GIMP/2.10/{config['file_name']}'))"
        )["output"].strip()
    # TODO: Add support for macOS and Windows
    else:
        raise Exception("Unsupported operating system", os_type)

    _path = os.path.join(env.cache_dir, config["dest"])
    content = env.controller.get_file(config_path)

    if not content:
        logger.error("Failed to get GIMP config file.")
        return None

    with open(_path, "wb") as f:
        f.write(content)

    return _path
