# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
# Copyright (c) 2016-2017 Anaconda, Inc.
#
# May be copied and distributed freely only as part of an Anaconda or
# Miniconda installation.
# -----------------------------------------------------------------------------
"""Test launch applications utilities."""

# Standard library imports
import os

# Third party imports
import pytest

# Local imports
from anaconda_navigator.api.conda_api import _CondaAPI
from anaconda_navigator.config import WIN
from anaconda_navigator.utils import launch
from anaconda_navigator.utils.fixtures import tmpfolder


# --- Fixtures
# -----------------------------------------------------------------------------
@pytest.fixture(scope="module")
def conda_api():
    api = _CondaAPI()
    return api


# --- Tests
# -----------------------------------------------------------------------------
@pytest.mark.skipif(WIN, reason="Skip on windows as used on unix only")
def test_is_program_installed():
    assert launch.is_program_installed('python')
    assert not launch.is_program_installed('bloop')
    assert not launch.is_program_installed('bleep')


@pytest.mark.skipif(not WIN, reason="Skip on unix as used on win only")
def test_get_command_on_win_spaces(tmpfolder):
    tmp_folder = os.path.join(
        tmpfolder,
        'dieter oms',
    )
    root_prefix = 'C:\\prefix with spaces'
    prefix = root_prefix + '\\envs\\someenv'
    command = 'boo'
    package_name = 'foobar'
    environment = None
    popen_dict = launch.get_command_on_win(
        prefix, command, package_name, root_prefix, environment=environment, default_scripts_path=tmp_folder
    )
    cmd = popen_dict.pop('cmd')
    print(popen_dict)

    assert 'call "C:\\prefix with spaces\\Scripts\\activate" "C:\\prefix with spaces\\envs\\someenv"' in cmd
    assert '"' + tmp_folder in cmd


@pytest.mark.skipif(not WIN, reason="Skip on unix as used on win only")
def test_get_command_on_win_no_spaces(tmpfolder):
    tmp_folder = os.path.join(
        tmpfolder,
        'dieter oms',
    )
    root_prefix = 'C:\\prefix-with-no-spaces'
    prefix = root_prefix + '\\envs\\someenv'
    command = 'boo'
    package_name = 'foobar'
    environment = None
    popen_dict = launch.get_command_on_win(
        prefix, command, package_name, root_prefix, environment=environment, default_scripts_path=tmp_folder
    )
    cmd = popen_dict.pop('cmd')
    print(popen_dict)

    assert 'call C:\\prefix-with-no-spaces\\Scripts\\activate C:\\prefix-with-no-spaces\\envs\\someenv' in cmd
    assert '"' + tmp_folder in cmd


@pytest.mark.skipif(not WIN, reason="Skip on unix as used on win only")
def test_get_command_on_win_no_spaces_2(tmpfolder):
    tmp_folder = os.path.join(
        tmpfolder,
        'dieter-oms',
    )
    root_prefix = 'C:\\prefix-with-no-spaces'
    prefix = root_prefix + '\\envs\\someenv'
    command = 'boo'
    package_name = 'foobar'
    environment = None
    popen_dict = launch.get_command_on_win(
        prefix, command, package_name, root_prefix, environment=environment, default_scripts_path=tmp_folder
    )
    cmd = popen_dict.pop('cmd')
    print(popen_dict)

    assert 'call C:\\prefix-with-no-spaces\\Scripts\\activate C:\\prefix-with-no-spaces\\envs\\someenv' in cmd
    assert '"' + tmp_folder not in cmd


@pytest.mark.skipif(WIN, reason="Skip on windows as used on unix only")
def test_get_command_on_unix_spaces(tmpfolder):
    tmp_folder = os.path.join(
        tmpfolder,
        'dieter oms',
    )
    root_prefix = '/prefix with spaces'
    prefix = root_prefix + '/envs/someenv'
    command = 'boo'
    package_name = 'foobar'
    environment = None
    popen_dict = launch.get_command_on_unix(
        prefix, command, package_name, root_prefix, environment=environment, default_scripts_path=tmp_folder
    )
    print(popen_dict)
    cmd = popen_dict.pop('cmd')

    assert '. "/prefix with spaces/bin/activate" "/prefix with spaces/envs/someenv"' in cmd
    assert '"' + tmp_folder in cmd


@pytest.mark.skipif(WIN, reason="Skip on windows as used on unix only")
def test_get_command_on_unix_no_spaces(tmpfolder):
    tmp_folder = os.path.join(
        tmpfolder,
        'dieter oms',
    )
    root_prefix = '/prefix-with-no-spaces'
    prefix = root_prefix + '/envs/someenv'
    command = 'boo'
    package_name = 'foobar'
    environment = None
    popen_dict = launch.get_command_on_unix(
        prefix, command, package_name, root_prefix, environment=environment, default_scripts_path=tmp_folder
    )
    print(popen_dict)
    cmd = popen_dict.pop('cmd')

    assert '. /prefix-with-no-spaces/bin/activate /prefix-with-no-spaces/envs/someenv' in cmd
    assert '"' + tmp_folder in cmd


@pytest.mark.skipif(WIN, reason="Skip on windows as used on unix only")
def test_get_command_on_unix_no_spaces_2(tmpfolder):
    tmp_folder = os.path.join(
        tmpfolder,
        'dieter-oms',
    )
    root_prefix = '/prefix-with-no-spaces'
    prefix = root_prefix + '/envs/someenv'
    command = 'boo'
    package_name = 'foobar'
    environment = None
    popen_dict = launch.get_command_on_unix(
        prefix, command, package_name, root_prefix, environment=environment, default_scripts_path=tmp_folder
    )
    print(popen_dict)
    cmd = popen_dict.pop('cmd')

    assert '. /prefix-with-no-spaces/bin/activate /prefix-with-no-spaces/envs/someenv' in cmd
    assert '"' + tmp_folder not in cmd


@pytest.mark.xfail
def test_create_app_run_script():
    # command, package_name, prefix, root_prefix, suffix
    raise NotImplementedError


@pytest.mark.xfail
def test_launch():
    # prefix, command, leave_path_alone, working_directory=HOME_PATH,
    # package_name=None, root_prefix=None
    raise NotImplementedError


@pytest.mark.xfail
def test_console(activate=None):
    raise NotImplementedError


@pytest.mark.xfail
def test_check_prog(api):
    # prog, prefix
    # 'notebook', 'jupyter notebook'
    raise NotImplementedError


@pytest.mark.xfail
def test_py_in_console():
    # activate=None, prog='python'
    raise NotImplementedError


@pytest.mark.xfail
def test_run_ex():
    # *args
    raise NotImplementedError


@pytest.mark.xfail
def test_run_python_console():
    # project_path, project=None, filename=""
    raise NotImplementedError
