# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
# Copyright 2016 Continuum Analytics, Inc.
#
# May be copied and distributed freely only as part of an Anaconda or
# Miniconda installation.
# -----------------------------------------------------------------------------
"""Launch applications utilities."""

# Standard library imports
import json
import os
import shutil
import subprocess
import sys
import uuid

# Third party imports
from qtpy.QtCore import QProcess

# Local imports
from anaconda_navigator.api.anaconda_api import AnacondaAPI
from anaconda_navigator.config import HOME_PATH
from anaconda_navigator.config.base import get_conf_path, get_home_dir
from anaconda_navigator.utils.logs import logger


def is_program_installed(basename):
    """
    Return program absolute path if installed in PATH.

    Otherwise, return None
    """
    for path in os.environ["PATH"].split(os.pathsep):
        abspath = os.path.join(path, basename)
        if os.path.isfile(abspath):
            return abspath


def launch(project_path, command):
    """Handle launching commands from projects."""
    logger.debug(str((project_path, command)))
    command = command.format(PREFIX=project_path)
    command = command.replace('\\', '/')
    if os.name == 'nt':
        command = command.replace('/bin', '/Scripts')

    if '.ipynb' in command:
        filename = command.replace('ipython notebook ', '')
        filename = filename.replace('jupyter notebook ', '')
        run_notebook(project_path=project_path, filename=filename)
    elif command.startswith('python '):
        filename = command.replace('python ', '')
        run_python_file(project_path, filename=filename)
    else:
        proc = QProcess()
        proc.setWorkingDirectory(HOME_PATH)
        logger.debug(command)
        if sys.platform == 'darwin' and command.startswith('open '):
            com = 'open'
            args = [command.replace('open ', '')]
            proc.startDetached(com, args, HOME_PATH)
        else:
            proc.startDetached(command, [], HOME_PATH)


def console(activate=None):
    """Open command prompt console and optionally activate the environment."""
    if os.name == 'nt':
        if activate:
            cmd = 'start cmd.exe /k activate ' + activate
        else:
            cmd = 'start cmd.exe'
        logger.debug(cmd)
        subprocess.Popen(cmd, shell=True)
    elif sys.platform == 'darwin':
        if activate:
            cmd = ('bash --init-file '
                   '<(echo "source activate {};")'.format(activate))
        else:
            cmd = 'bash'
        fname = os.path.join(get_conf_path(), 'a.tool')

        with open(fname, 'w') as f:
            f.write(cmd)
        os.chmod(fname, 0o777)

        logger.debug('open ' + fname)
        subprocess.call(['open ' + fname], shell=True)
    else:  # Linux, solaris, etc
        if is_program_installed('gnome-terminal'):
            if activate:
                cmd = ['gnome-terminal', '-x', 'bash', '-c', 'bash --init-file'
                       ' <(echo "source activate {0};")'.format(activate)]
            else:
                cmd = ['gnome-terminal', '-e', 'bash']
            logger.debug(' '.join(cmd))
            subprocess.Popen(cmd)
        elif is_program_installed('xterm'):
            if activate:
                cmd = ['xterm', '-e', 'bash --init-file'
                       ' <(echo "source activate {0};")'.format(activate)]
            else:
                cmd = ['xterm']
            logger.debug(' '.join(cmd))
            subprocess.Popen(cmd)


def check_prog(prog, prefix=None):
    """Check if program exists in prefix."""
    api = AnacondaAPI()
    prefix = prefix or api.conda_get_prefix_envname(name='root')

    if prog in ['notebook', 'jupyter notebook']:
        pkgs = ['notebook', 'ipython-notebook', 'jupyter-notebook']
    elif prog in ['ipython', 'jupyter console']:
        pkgs = ['ipython', 'jupyter']
    else:
        pkgs = [prog]

    return any(api.conda_package_version(prefix=prefix, pkg=p) is not None
               for p in pkgs)


def py_in_console(activate=None, prog='python'):
    """
    Run (i)python in a new console.

    It optionally run activate first on the given env name/path.
    """
    logger.debug("%s, %s", activate, prog)
    if not check_prog(prog, activate):
        raise RuntimeError('Program not available in environment: %s, %s',
                           prog, activate)
    if prog == 'python':
        cmd = 'python -i'
    elif prog == 'ipython':
        cmd = 'ipython -i'
    elif 'notebook' in prog:
        cmd = 'jupyter notebook'
    if os.name == 'nt':
        if activate:
            cmd = '"activate {} & {}"'.format(activate, cmd)
        else:
            cmd = '"{}"'.format(cmd)
        logger.debug('start cmd.exe /k ' + cmd)
        subprocess.Popen('start cmd.exe /k {}'.format(cmd),
                         shell=True, cwd=HOME_PATH)
    elif sys.platform == 'darwin':
        if activate:
            if '/' not in activate:
                out = subprocess.check_output(['conda', 'env', 'list'])
                for line in out.decode().split('\n'):
                    # This is too fragile...!
                    print('\t' + line)
                    if line and line.split()[0] == activate:
                        activate = line.split()[-1]
                        print(activate)
                        break
            cmd = activate + '/bin/' + cmd
        else:
            cmd = cmd
        fname = os.path.join(get_conf_path(), 'a.tool')

        command = "source activate {0};{1}".format(activate, cmd)
        with open(fname, 'w') as f:
            f.write(command)

        os.chmod(fname, 0o777)
        logger.debug('open ' + fname)
        subprocess.call(['open ' + fname], shell=True, cwd=HOME_PATH)
    else:  # linux, solaris, etc
        if activate:
            cmd = "source activate " + activate + "; " + cmd
        if is_program_installed('gnome-terminal'):
            cmd = ['gnome-terminal', '-x', 'bash', '-c', cmd]
            logger.debug(' '.join(cmd))
            subprocess.Popen(cmd, cwd=HOME_PATH)
            return
        elif is_program_installed('xterm'):
            cmd = ['xterm', '-e', cmd]
            logger.debug(' '.join(cmd))
            subprocess.Popen(cmd, cwd=HOME_PATH)
            return


def run_ex(*args):
    """Start new console window, run command (given as string list)."""
    logger.debug(str(args))
    cmd = '{0}'.format(" ".join(args))

    if os.name == 'nt':
        subprocess.Popen(['start', 'cmd.exe', '/K', cmd])

    elif sys.platform == 'darwin':
        fname = 'a.tool'
        open(fname, 'w').write(cmd)
        os.chmod(fname, 0o777)
        subprocess.call(['open ' + fname], shell=True)
        os.unlink(fname)

    else:
        terms = ['gnome-terminal', 'konsole', 'xterm']
        for term in terms:
            if is_program_installed(term):
                subprocess.Popen([term, '-e', '"{}"'.format(cmd)])
                break


def run_notebook(project_path, project=None, filename=""):
    """Start notebook server."""
    from anaconda_navigator.api import AnacondaAPI
    api = AnacondaAPI()

    if project is None:
        project = api.load_project(project_path)

    kernels_folder = os.sep.join([get_home_dir(), ".ipython",
                                  "kernels"])
    display_name = '{0} ({1})'.format(project.name, project_path)
    kernel_uuid = uuid.uuid1()
    kernel_path = os.sep.join([kernels_folder, "{name}", "kernel.json"])
    pyexec = os.sep.join([project.env_prefix(project_path),
                          'bin', 'python'])
    spec = {"argv": [pyexec,
                     "-m", "IPython.kernel",
                     "-f", "{connection_file}"],
            "display_name": display_name,
            "language": "python",
            }

    # Delete any other kernel sec mathching this name!
    kernels = os.listdir(kernels_folder)
    for kernel in kernels:
        path = os.sep.join([kernels_folder, kernel])
        file_path = os.sep.join([path, 'kernel.json'])

        if os.path.isfile(file_path):
            with open(file_path, 'r') as f:
                data = json.loads(f.read())

            name = data.get('display_name')
            if name is not None and project_path in name:
                shutil.rmtree(path)

    os.makedirs(os.path.split(kernel_path.format(name=kernel_uuid))[0])

    with open(kernel_path.format(name=kernel_uuid), 'w') as f:
        f.write(json.dumps(spec))

    # This is not working!
    cmd = ('jupyter notebook '
           '--KernelSpecManager.whitelist={0}'.format(kernel_uuid))

    cmd = ('jupyter notebook')
    command = (cmd + ' ' + filename)
    logger.debug(",".join([command, project_path]))
    subprocess.Popen(command.split(), cwd=project_path)


def run_python_file(project_path, project=None, filename=""):
    """Execute python in environment."""
    from anaconda_navigator.api import AnacondaAPI
    api = AnacondaAPI()

    if project is None:
        project = api.load_project(project_path)

    cmd = os.sep.join([project.env_prefix(project_path),
                       'bin', 'python'])
    logger.debug(",".join([cmd, filename]))
    subprocess.Popen([cmd, filename])


def run_python_console(project_path, project=None, filename=""):
    """Execute python in env with console."""
    from anaconda_navigator.api import AnacondaAPI
    api = AnacondaAPI()

    if project is None:
        project = api.load_project(project_path)

    cmd = os.sep.join([project.env_prefix(project_path),
                       'bin', 'python'])
    run_ex(cmd, filename)
