# Path: vimswitch/ActionResolver.py
# def getActionResolver(app):
#     return app.get('actionResolver', createActionResolver(app))
# 
# Path: vimswitch/Application.py
# class Application:
#     """
#     A container for components that are used application-wide.
# 
#     Examples of how this class can be used:
# 
#       - Set a component
#         `app.foo = myFoo`
# 
#       - Get (or create) a component:
#         `foo = app.get('foo', Foo())`
#         This will try to return `app.foo`. If that does not exist, then it will
#         set `app.foo = Foo()` and return that instead.
# 
#       - Get an already created component
#         `foo = app.foo`
#         Use this when you are sure that app.foo has already been set
#     """
# 
#     def get(self, attr, default):
#         """
#         Returns self.attr. If attr does not exist, then make self.attr = default
#         and then return default.
#         """
#         if hasattr(self, attr):
#             return getattr(self, attr)
#         else:
#             setattr(self, attr, default)
#             return default
# 
# Path: vimswitch/ApplicationDirs.py
# def getApplicationDirs(app):
#     return app.get('applicationDirs', createApplicationDirs(app))
# 
# Path: vimswitch/CommandLineParser.py
# def getCommandLineParser(app):
#     return app.get('commandLineParser', createCommandLineParser(app))
# 
# Path: vimswitch/ConfigFile.py
# def getConfigFile(app):
#     return app.get('configFile', createConfigFile(app))
# 
# Path: vimswitch/Settings.py
# def getSettings(app):
#     return app.get('settings', createSettings(app))


from .ActionResolver import getActionResolver
from .Application import Application
from .ApplicationDirs import getApplicationDirs
from .CommandLineParser import getCommandLineParser
from .ConfigFile import getConfigFile
from .Settings import getSettings


class VimSwitch:
    def __init__(self, app=Application()):
        self.app = app
        self.raiseExceptions = False

    def main(self, argv):
        """
        Runs VimSwitch with the given args. Returns 0 on success, or -1 if an
        error occurred.
        """
        try:
            self.loadSettings()
            commandLineParser = getCommandLineParser(self.app)
            commandLineParser.parse(argv)
            actionResolver = getActionResolver(self.app)
            actionResolver.doActions()
            self.saveSettings()
            return actionResolver.exitCode
        except Exception as e:
            message = 'Error: %s' % str(e)
            print(message)
            return -1

    def loadSettings(self):
        applicationDirs = getApplicationDirs(self.app)
        applicationDirs.createIfNone()

# Path: vimswitch/ActionResolver.py
# def getActionResolver(app):
#     return app.get('actionResolver', createActionResolver(app))
# 
# Path: vimswitch/Application.py
# class Application:
#     """
#     A container for components that are used application-wide.
# 
#     Examples of how this class can be used:
# 
#       - Set a component
#         `app.foo = myFoo`
# 
#       - Get (or create) a component:
#         `foo = app.get('foo', Foo())`
#         This will try to return `app.foo`. If that does not exist, then it will
#         set `app.foo = Foo()` and return that instead.
# 
#       - Get an already created component
#         `foo = app.foo`
#         Use this when you are sure that app.foo has already been set
#     """
# 
#     def get(self, attr, default):
#         """
#         Returns self.attr. If attr does not exist, then make self.attr = default
#         and then return default.
#         """
#         if hasattr(self, attr):
#             return getattr(self, attr)
#         else:
#             setattr(self, attr, default)
#             return default
# 
# Path: vimswitch/ApplicationDirs.py
# def getApplicationDirs(app):
#     return app.get('applicationDirs', createApplicationDirs(app))
# 
# Path: vimswitch/CommandLineParser.py
# def getCommandLineParser(app):
#     return app.get('commandLineParser', createCommandLineParser(app))
# 
# Path: vimswitch/ConfigFile.py
# def getConfigFile(app):
#     return app.get('configFile', createConfigFile(app))
# 
# Path: vimswitch/Settings.py
# def getSettings(app):
#     return app.get('settings', createSettings(app))


# Path: vimswitch/VimSwitch.py
from .ActionResolver import getActionResolver
from .Application import Application
from .ApplicationDirs import getApplicationDirs
from .CommandLineParser import getCommandLineParser
from .ConfigFile import getConfigFile
from .Settings import getSettings



class VimSwitch:
    def __init__(self, app=Application()):
        self.app = app
        self.raiseExceptions = False

    def main(self, argv):
        """
        Runs VimSwitch with the given args. Returns 0 on success, or -1 if an
        error occurred.
        """
        try:
            self.loadSettings()
            commandLineParser = getCommandLineParser(self.app)
            commandLineParser.parse(argv)
            actionResolver = getActionResolver(self.app)
            actionResolver.doActions()
            self.saveSettings()
            return actionResolver.exitCode
        except Exception as e:
            message = 'Error: %s' % str(e)
            print(message)
            return -1

    def loadSettings(self):
        applicationDirs = getApplicationDirs(self.app)
        applicationDirs.createIfNone()

        settings = getSettings(self.app)