from django.db import models
from BaseModels.base_models import *
from django.utils.translation import gettext_lazy as _
from GeneralApp.models import Block_Abstract
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.fields import GenericRelation
from django.core.validators import validate_image_file_extension



class BlockPluginPresentation(Block_Abstract):
    from BaseModels.base_models import Manager_Enabled
    objects = models.Manager()
    enabled_objs = Manager_Enabled()

    content_type = models.ForeignKey(ContentType, on_delete=models.SET_NULL, null=True)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey('content_type', 'object_id')

    pre_title = models.CharField(max_length=250, verbose_name=_('Текст перед заголовком'), null=True, blank=True)

    but1_title = models.CharField(max_length=100, verbose_name=_('Текст на кнопке 1'), null=True, blank=True)
    but2_title = models.CharField(max_length=100, verbose_name=_('Текст на кнопке 2'), null=True, blank=True)

    but1_url = models.TextField(verbose_name=_('URL привязанной страницы к кнопке 1'), null=True, blank=True)
    but2_url = models.TextField(verbose_name=_('URL привязанной страницы к кнопке 2'), null=True, blank=True)

    but1_icon = models.FileField(
        upload_to='uploads/', verbose_name=_('Пиктограмма для кнопки 1'), null=True, blank=True,
        validators=[validate_file_extension]
    )
    but2_icon = models.FileField(
        upload_to='uploads/', verbose_name=_('Пиктограмма для кнопки 2'), null=True, blank=True,
        validators=[validate_file_extension]
    )

    class Meta:
        verbose_name = _('Блок презентации плагина')
        verbose_name_plural = _('Блок презентации плагина')


def preSave_BlockPluginPresentation(sender, instance, **kwargs):
    instance.url = get_unique_url_for_new_instance(instance, sender)
pre_save.connect(preSave_BlockPluginPresentation, sender=BlockPluginPresentation, dispatch_uid='pre_save_connect')


page_scheme_choices = (
    ('projecting', _('Проектирование')),
    ('services', _('Разработка')),
    ('plugin', _('Плагин')),
)


class Section(BaseModelViewPage):

    page_scheme = models.CharField(
        max_length=100, verbose_name=_('Схема страницы'), choices=page_scheme_choices, default='projecting')

    plugin_presentation = GenericRelation('ServicesApp.BlockPluginPresentation',
                                          related_query_name='grel_%(class)s_for_block_plugin_presentation')


    def get_projects(self):
        from PortfolioApp.models import Project
        services_ids = self.rel_services_for_section.filter(enable=True).values_list('id', flat=True)
        projects = Project.objects.filter(
            enable=True,
            services__id__in=services_ids
        ).order_by('order')
        return projects


    class Meta:
        verbose_name = _('Раздел сайта')
        verbose_name_plural = _('Разделы сайта')


def preSave_Section(sender, instance, **kwargs):
    instance.url = get_unique_url_for_new_instance(instance, sender)
pre_save.connect(preSave_Section, sender=Section, dispatch_uid='pre_save_connect')


class Service(BaseModelViewPage):

    url = models.TextField(verbose_name=_('URL привязанной страницы'), null=True, blank=True)

    section = models.ForeignKey(
        Section, verbose_name=_('Раздел'),
        on_delete=models.SET_NULL, related_name='rel_services_for_section',
        null=True, blank=True
    )
    parent_service = models.ForeignKey(
        'Service', verbose_name=_('Родитель'),
        on_delete=models.SET_NULL, related_name='rel_children_for_service',
        null=True, blank=True
    )

    def get_subservices(self):
        sub_services = Service.objects.filter(
            enable=True,
            parent_service=self
        ).order_by('order')
        return sub_services



    class Meta:
        verbose_name = _('Услуга')
        verbose_name_plural = _('Услуги')

def preSave_Service(sender, instance, **kwargs):
    instance.url = get_unique_url_for_new_instance(instance, sender)
pre_save.connect(preSave_Service, sender=Service, dispatch_uid='pre_save_connect')