# Path: kano_profile/paths.py
# TRACKER_UUIDS_PATH = os.path.join(kanoprofile_dir, 'tracker', 'uuids')
# PAUSED_SESSIONS_FILE = os.path.join(kanoprofile_dir, '.paused_sessions')


import json
import os
from kano.utils import read_json, write_json, get_date_now, ensure_dir, \
    chown_path, run_print_output_error, run_bg, run_cmd
from kano.logging import logger
from kano_profile.paths import apps_dir, xp_file, kanoprofile_dir, \
    app_profiles_file
    from kano.utils.misc import is_installed
    from kano.utils.shell import run_cmd


def load_app_state_variable_encode(app_name, variable):
    try:
        data = load_app_state_variable(app_name, variable)
        if data:
            encoded_data = json.dumps(data)
            return encoded_data

    except Exception as e:
        logger.error("Could not encode and load app state var:\n{}".format(e))


def save_app_state(app_name, data):
    """ Save a state of an application to the user's Kano profile.

        :param app_name: The application that this data are associated with.
        :type app_name: str

        :param data: The data to be stored.
        :type data: dict
    """

    logger.debug("save_app_state {}".format(app_name))

    app_state_file = get_app_state_file(app_name)
    data['save_date'] = get_date_now()
    ensure_dir(get_app_dir(app_name))
    write_json(app_state_file, data)
    if 'SUDO_USER' in os.environ:

# Path: kano_profile/paths.py
# TRACKER_UUIDS_PATH = os.path.join(kanoprofile_dir, 'tracker', 'uuids')
# PAUSED_SESSIONS_FILE = os.path.join(kanoprofile_dir, '.paused_sessions')


# Path: kano_profile/apps.py
import json
import os
from kano.utils import read_json, write_json, get_date_now, ensure_dir, \
    chown_path, run_print_output_error, run_bg, run_cmd
from kano.logging import logger
from kano_profile.paths import apps_dir, xp_file, kanoprofile_dir, \
    app_profiles_file
    from kano.utils.misc import is_installed
    from kano.utils.shell import run_cmd



def load_app_state_variable_encode(app_name, variable):
    try:
        data = load_app_state_variable(app_name, variable)
        if data:
            encoded_data = json.dumps(data)
            return encoded_data

    except Exception as e:
        logger.error("Could not encode and load app state var:\n{}".format(e))


def save_app_state(app_name, data):
    """ Save a state of an application to the user's Kano profile.

        :param app_name: The application that this data are associated with.
        :type app_name: str

        :param data: The data to be stored.
        :type data: dict
    """

    logger.debug("save_app_state {}".format(app_name))

    app_state_file = get_app_state_file(app_name)
    data['save_date'] = get_date_now()
    ensure_dir(get_app_dir(app_name))
    write_json(app_state_file, data)
    if 'SUDO_USER' in os.environ:

        chown_path(kanoprofile_dir)