# encoding: UTF-8
__author__ = 'a@toukmanov.ru'

from .exceptions import Error

class Application(object):
    """ TML application """
    def __init__(self, client, id, languages, default_locale):
        """ .ctor
            Args:
                client (api.client.Client): API client
                id (int): application id
                languages (list): list of languages
        """
        self.client = client
        self.id = id
        self.languages = languages
        self.default_locale = default_locale


    @classmethod
    def from_dict(cls, client, data):
        """ Build from api response
            Args:
                client (api.client.Client): API client
                data (dict): API response
            Returns:
                Application
        """
        return Application(client, data['id'], data['languages'], data['default_locale'])


    @classmethod
    def load_default(cls, client):
        """ Load default application
            Args:
                client (api.client.Client): API client
            Returns:
                Application
        """
        return cls.from_dict(client, client.get('applications/current', {'definition': 1}))

    @property
    def supported_locales(self):
        """ List of locales supported by app
            Returns:
                list
        """
        return [lang['locale'] for lang in self.languages]

    @classmethod
    def load_by_id(cls, client, id):
        """ Load application by id
            Args:
                client (api.client.Client): API client
                id (int): application
            Returns:
                Application
        """
        return cls.from_dict(client, client.get('applications/%d' % id, {'definition': 1}))

    def get_language_url(self, locale):
        """ Language URL for locale
            Args:
                locale (string): locale code
            Throws:
                LanguageNotSupported
            Returns:
                string: url
        """
        for language in self.languages:
            if language['locale'] == locale:
                return 'languages/%s' % locale
        raise LanguageNotSupported(locale, self)


class LanguageNotSupported(Error):
    """ Raised if somebody try to load not supported language """
    def __init__(self, locale, application):
        """ .ctor
            Args:
                locale (string): given locale code
                application (Application): application instance
        """
        self.locale = locale
        self.application = application

    def __str__(self):
        return 'Locale %s is not suppored by application %d' % (self.locale, self.application.id)

@@ -41,17 +41,16 @@
 nov.ru'%0A
-%0A
 from .ex
@@ -123,24 +123,336 @@
 ication %22%22%22%0A
+    client = None # API client%0A    id = None # application id%0A    languages = None # supported language%0A    default_locale = None%0A    features = %7B%7D # key - feature code, value - boolean supported)%0A    key = None # client key%0A    name = None # human readable name%0A    shortcuts = %7B%7D%0A    tools = %7B%7D # tools URLS %0A%0A
     def __in
@@ -495,24 +495,34 @@
 fault_locale
+, **kwargs
 ):%0A        %22
@@ -838,16 +838,87 @@
 _locale%0A
+        for key in kwargs:%0A            setattr(self, key, kwargs%5Bkey%5D)%0A
 %0A%0A    @c
@@ -1217,61 +1217,14 @@
 nt, 
+**
 data
-%5B'id'%5D, data%5B'languages'%5D, data%5B'default_locale'%5D
 )%0A%0A%0A
@@ -3038,8 +3038,9 @@
 ion.id)%0A
+%0A

Store all application data as properties