# -*- coding: utf-8 -*-
"""
Guarded access to the Earth Engine runtime.

The plugin depends on the "Google Earth Engine" QGIS plugin (module ``ee_plugin``)
and on the ``earthengine-api`` package. Importing them lazily keeps this plugin
loadable when they are missing, so the user gets a readable message instead of a
traceback during QGIS start-up.
"""

try:
    import ee
except ImportError as error:
    ee = None
    _EE_ERROR = str(error)
else:
    _EE_ERROR = None

try:
    from ee_plugin import Map
except ImportError as error:
    Map = None
    _EE_PLUGIN_ERROR = str(error)
else:
    _EE_PLUGIN_ERROR = None


def is_ee_ready():
    return ee is not None and Map is not None


def ee_missing_message():
    """Human readable hint about what is missing."""
    missing = []
    if ee is None:
        missing.append(u'earthengine-api ({})'.format(_EE_ERROR))
    if Map is None:
        missing.append(u'plugin "Google Earth Engine" ({})'.format(_EE_PLUGIN_ERROR))

    return (
        u'Missing Earth Engine component: {}. Install the "Google Earth Engine" plugin '
        u'in this QGIS profile and sign in. / Thiếu thành phần Earth Engine: hãy cài plugin '
        u'"Google Earth Engine" cho profile QGIS đang dùng và đăng nhập tài khoản GEE.'
    ).format('; '.join(missing))
