# -*- coding: UTF-8 -*-
# Copyright 2010-2018 Rumma & Ko Ltd
# License: BSD (see file COPYING for details)


from builtins import object
from django.contrib.contenttypes.models import *

from django.conf import settings

from django.utils.translation import ugettext_lazy as _
from django.utils.text import format_lazy

from lino.api import dd
from lino.core.gfks import gfk2lookup

from .fields import GenericForeignKey, GenericForeignKeyIdField


class Controllable(dd.Model):

    # Translators: will also be concatenated with '(type)' '(object)'
    owner_label = _('Controlled by')

    controller_is_optional = True

    class Meta(object):
        abstract = True

    owner_type = dd.ForeignKey(
        ContentType,
        editable=True,
        blank=True, null=True,
        verbose_name=format_lazy(u"{} {}", owner_label, _('(type)')))

    owner_id = GenericForeignKeyIdField(
        owner_type,
        editable=True,
        blank=True, null=True,
        verbose_name=format_lazy(u"{} {}", owner_label, _('(object)')))

    owner = GenericForeignKey(
        'owner_type', 'owner_id',
        verbose_name=owner_label)

    @classmethod
    def update_controller_field(cls, verbose_name=None, **kwargs):

        if verbose_name is not None:
            dd.update_field(cls, 'owner', verbose_name=verbose_name)
            kwargs.update(
                verbose_name=format_lazy(u"{} {}",
                    verbose_name, _('(object)')))
        dd.update_field(cls, 'owner_id', **kwargs)
        if verbose_name is not None:
            kwargs.update(
                verbose_name=format_lazy(u"{} {}",
                    verbose_name, _('(type)')))
        dd.update_field(cls, 'owner_type', **kwargs)

    def update_owned_instance(self, controllable):
        if self.owner:
            self.owner.update_owned_instance(controllable)
        super(Controllable, self).update_owned_instance(controllable)

    def save(self, *args, **kw):
        if settings.SITE.loading_from_dump:
            super(Controllable, self).save(*args, **kw)
        else:
            if self.owner:
                self.owner.update_owned_instance(self)
            super(Controllable, self).save(*args, **kw)
            if self.owner:
                self.owner.after_update_owned_instance(self)

    def controlled_rows(self, model, **kwargs):
        gfk = self._meta.get_field('owner')
        kwargs = gfk2lookup(gfk, self, **kwargs)
        return model.objects.filter(**kwargs)


[((23, 18, 23, 36), 'django.utils.translation.ugettext_lazy', '_', ({(23, 20, 23, 35): '"""Controlled by"""'}, {}), "('Controlled by')", True, 'from django.utils.translation import ugettext_lazy as _\n'), ((54, 8, 54, 50), 'lino.api.dd.update_field', 'dd.update_field', ({(54, 24, 54, 27): 'cls', (54, 29, 54, 39): '"""owner_id"""'}, {}), "(cls, 'owner_id', **kwargs)", False, 'from lino.api import dd\n'), ((59, 8, 59, 52), 'lino.api.dd.update_field', 'dd.update_field', ({(59, 24, 59, 27): 'cls', (59, 29, 59, 41): '"""owner_type"""'}, {}), "(cls, 'owner_type', **kwargs)", False, 'from lino.api import dd\n'), ((78, 17, 78, 48), 'lino.core.gfks.gfk2lookup', 'gfk2lookup', ({(78, 28, 78, 31): 'gfk', (78, 33, 78, 37): 'self'}, {}), '(gfk, self, **kwargs)', False, 'from lino.core.gfks import gfk2lookup\n'), ((50, 12, 50, 68), 'lino.api.dd.update_field', 'dd.update_field', (), '', False, 'from lino.api import dd\n'), ((34, 56, 34, 67), 'django.utils.translation.ugettext_lazy', '_', ({(34, 58, 34, 66): '"""(type)"""'}, {}), "('(type)')", True, 'from django.utils.translation import ugettext_lazy as _\n'), ((40, 56, 40, 69), 'django.utils.translation.ugettext_lazy', '_', ({(40, 58, 40, 68): '"""(object)"""'}, {}), "('(object)')", True, 'from django.utils.translation import ugettext_lazy as _\n'), ((53, 34, 53, 47), 'django.utils.translation.ugettext_lazy', '_', ({(53, 36, 53, 46): '"""(object)"""'}, {}), "('(object)')", True, 'from django.utils.translation import ugettext_lazy as _\n'), ((58, 34, 58, 45), 'django.utils.translation.ugettext_lazy', '_', ({(58, 36, 58, 44): '"""(type)"""'}, {}), "('(type)')", True, 'from django.utils.translation import ugettext_lazy as _\n')]