# -*- coding: utf-8 -*-

import logging

from django.conf.urls import url, include
from django.conf import settings

import plugins.plugin as plugin


logger = logging.getLogger("inventree")


# region mixins
class SettingsMixin:
    """Mixin that enables settings for the plugin"""
    def __init__(self):
        super().__init__()
        self.add_mixin('settings', 'has_settings')
        self.settings = self.setup_settings()

    def setup_settings(self):
        """
        setup settings for this plugin
        """
        return getattr(self, 'SETTINGS', None)

    @property
    def has_settings(self):
        """
        does this plugin use custom settings
        """
        return bool(self.settings)

    @property
    def settingspatterns(self):
        if self.has_settings:
            return {f'PLUGIN_{self.plugin_name().upper()}_{key}': value for key, value in self.settings.items()}
        return None


class UrlsMixin:
    """Mixin that enables urls for the plugin"""
    def __init__(self):
        super().__init__()
        self.add_mixin('urls', 'has_urls')
        self.urls = self.setup_urls()

    def setup_urls(self):
        """
        setup url endpoints for this plugin
        """
        return getattr(self, 'URLS', None)

    @property
    def base_url(self):
        return f'{settings.PLUGIN_URL}/{self.plugin_name()}/'

    @property
    def urlpatterns(self):
        """
        retruns the urlpatterns for this plugin
        """
        if self.has_urls:
            return url(f'^{self.plugin_name()}/', include((self.urls, self.plugin_name())), name=self.plugin_name())
        return None

    @property
    def has_urls(self):
        """
        does this plugin use custom urls
        """
        return bool(self.urls)


class NavigationMixin:
    """Mixin that enables adding navigation links with the plugin"""
    def __init__(self):
        super().__init__()
        self.add_mixin('navigation', 'has_naviation')
        self.navigation = self.setup_navigation()

    def setup_navigation(self):
        """
        setup navigation links for this plugin
        """
        nav_links = getattr(self, 'NAVIGATION', None)
        if nav_links:
            # check if needed values are configured
            for link in nav_links:
                if False in [a in link for a in ('link', 'name', )]:
                    raise NotImplementedError('Wrong Link definition', link)
        return nav_links

    @property
    def has_naviation(self):
        """
        does this plugin define navigation elements
        """
        return bool(self.navigation)
# endregion


class IntegrationPlugin(plugin.InvenTreePlugin):
    """
    The IntegrationPlugin class is used to integrate with 3rd party software
    """

    def __init__(self):
        self.add_mixin('base')

    def add_mixin(self, key: str, fnc_enabled=True):
        if not hasattr(self, '_mixins'):
            self._mixins = {}
        self._mixins[key] = fnc_enabled

    def mixin(self, key):
        return key in self._mixins

    def mixin_enabled(self, key):
        if self.mixin(key):
            fnc_name = self._mixins.get(key)
            return getattr(self, fnc_name, True)
        return False

@@ -186,16 +186,244 @@
 ree%22)%0A%0A%0A
+class MixinBase:%0A    %22%22%22general base for mixins%22%22%22%0A%0A    def add_mixin(self, key: str, fnc_enabled=True, cls=None):%0A        if not hasattr(self, '_mixins'):%0A            self._mixins = %7B%7D%0A        self._mixins%5Bkey%5D = fnc_enabled%0A%0A%0A
 # region
@@ -2883,16 +2883,27 @@
 nPlugin(
+MixinBase, 
 plugin.I
@@ -3073,173 +3073,8 @@
 ')%0A%0A
-    def add_mixin(self, key: str, fnc_enabled=True):%0A        if not hasattr(self, '_mixins'):%0A            self._mixins = %7B%7D%0A        self._mixins%5Bkey%5D = fnc_enabled%0A%0A
     

move mixin registry stuff into own class