from __future__ import absolute_import
from __future__ import print_function

import os
import sublime

from .const import AUTO_FORMAT_FILE_EXTENSIONS
from .const import IS_ST3
from .const import PLUGIN_NAME
from .const import PRETTIER_OPTIONS_KEY
from .const import PROJECT_SETTINGS_KEY
from .const import SETTINGS_FILENAME

from .util import ensure_file_has_ext
from .util import generate_dirs
from .util import is_bool_str
from .util import is_str_none_or_empty
from .util import is_windows
from .util import to_str
from .util import which


def st_status_message(msg):
    sublime.set_timeout(lambda: sublime.status_message('{0}: {1}'.format(PLUGIN_NAME, msg)), 0)


def get_setting(view, key, default_value=None):
    settings = view.settings().get(PLUGIN_NAME)
    if settings is None or settings.get(key) is None:
        settings = sublime.load_settings(SETTINGS_FILENAME)
    value = settings.get(key, default_value)
    # check for project-level overrides:
    project_value = _get_project_setting(key)
    if project_value is None:
        return value
    return project_value


def get_sub_setting(view, key=None):
    settings = view.settings().get(PLUGIN_NAME)
    if settings is None or settings.get(PRETTIER_OPTIONS_KEY).get(key) is None:
        settings = sublime.load_settings(SETTINGS_FILENAME)
    value = settings.get(PRETTIER_OPTIONS_KEY).get(key)
    # check for project-level overrides:
    project_value = _get_project_sub_setting(key)
    if project_value is None:
        return value
    return project_value


def _get_project_setting(key):
    """Get a project setting.

    JsPrettier project settings are stored in the sublime project file
    as a dictionary, e.g.:

        "settings":
        {
            "js_prettier": { "key": "value", ... }
        }

    :param key: The project setting key.
    :return: The project setting value.
    :rtype: str
    """
    project_settings = sublime.active_window().active_view().settings()
    if not project_settings:
        return None
    js_prettier_settings = project_settings.get(PROJECT_SETTINGS_KEY)
    if js_prettier_settings and key in js_prettier_settings:
        return js_prettier_settings[key]
    return None


def _get_project_sub_setting(option):
    project_settings = sublime.active_window().active_view().settings()
    js_prettier_settings = project_settings.get(PROJECT_SETTINGS_KEY, None)
    if not js_prettier_settings:
        return None
    prettier_options = js_prettier_settings.get(PRETTIER_OPTIONS_KEY, None)
    if prettier_options and option in prettier_options:
        return prettier_options.get(option, None)
    return None


def is_file_auto_formattable(view):
    filename = view.file_name()
    if not filename:
        return False
    file_ext = filename.rpartition('.')[-1]
    if file_ext == filename:
        return False
    if file_ext in AUTO_FORMAT_FILE_EXTENSIONS:
        return True
    if file_ext in set(get_setting(view, 'custom_file_extensions', [])):
        return True
    return False


def get_st_project_path():
    """Get the active Sublime Text project path.

    Original: https://gist.github.com/astronaughts/9678368

    :rtype: object
    :return: The active Sublime Text project path.
    """
    window = sublime.active_window()
    folders = window.folders()
    if len(folders) == 1:
        return folders[0]
    else:
        active_view = window.active_view()
        if active_view:
            active_file_name = active_view.file_name()
        else:
            active_file_name = None
        if not active_file_name:
            return folders[0] if len(folders) else os.path.expanduser('~')
        for folder in folders:
            if active_file_name.startswith(folder):