# coding: utf-8
from __future__ import unicode_literals, division, absolute_import, print_function

import os
import sys
import shutil
import locale
import stat

import golangconfig

if sys.version_info < (3,):
    from cStringIO import StringIO
    str_cls = unicode  # noqa
else:
    from io import StringIO
    str_cls = str


class SublimeViewMock():

    _settings = None
    _context = None

    def __init__(self, settings, context):
        self._settings = settings
        self._context = context

    def settings(self):
        if self.window():
            # In Sublime Text, View objects inherit settings from the window/project
            # unless they are explicitly set on the view, so we replicate that here
            merged_golang_settings = {}
            project_data = self.window().project_data()
            if project_data:
                merged_golang_settings.update(project_data.get('settings', {}).get('golang', {}).copy())
            merged_golang_settings.update(self._settings)
        elif self._settings:
            merged_golang_settings = self._settings.copy()
        else:
            merged_golang_settings = {}
        return {'golang': merged_golang_settings}

    def window(self):
        return self._context.window


class SublimeWindowMock():

    _settings = None
    _context = None

    def __init__(self, settings, context):
        self._settings = settings
        self._context = context

    def project_data(self):
        if self._settings is None:
            return None
        return {'settings': {'golang': self._settings}}

    def active_view(self):
        if self._context.view:
            return self._context.view
        return SublimeViewMock({}, self._context)


class ShellenvMock():

    _env_encoding = locale.getpreferredencoding() if sys.platform == 'win32' else 'utf-8'
    _fs_encoding = 'mbcs' if sys.platform == 'win32' else 'utf-8'

    _shell = None