from django.db import models
from BaseModels.base_models import BaseModelViewPage, BaseModel, validate_file_extension
from django.utils.translation import gettext_lazy as _
# from ckeditor.fields import RichTextField
from ckeditor_uploader.fields import RichTextUploadingField
# from colorfield.fields import ColorField
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.fields import GenericRelation
from colorfield.fields import ColorField

class StaticPage(BaseModelViewPage):
    promo_header = models.BooleanField(verbose_name='Промо-хэдер', default=False)

    class Meta:
        verbose_name = _('Статическая страница')
        verbose_name_plural = _('Статические страницы')


block_type_choices = (
    ('photo_n_text', _('Фото с одной стороны + Текст с другой стороны')),
    ('present_technology_block', _('описание и НАЗВАНИЕ слева + Картинка справа')),
    ('photo150_n_title', _('Фото 150x150 сверху + заголовок под фото')),
    ('how_work', _('Как с нами работать')),
    ('how_dev', _('Как ведется разработка')),
    ('feedback_form', _('Форма обратной связи')),
    # ('slider', _('Галерея слайдов')),
)


class Block_Abstract(BaseModelViewPage):

    class Meta:
        verbose_name = _('Блок')
        verbose_name_plural = _('Блоки')
        abstract = True
        ordering = ['order']


class Block(Block_Abstract):

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

    block_type = models.CharField(max_length=100, choices=block_type_choices, verbose_name=_('Тип блока'),
                                  default='photo_n_text')

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

    bg_color = ColorField(verbose_name=_('Цвет фона'), default=None, null=True, blank=True)
    bg_image = models.FileField(
        upload_to='uploads/', verbose_name=_('Фоновое изображение блока'), null=True, blank=True,
        validators=[validate_file_extension]
    )

    but_title = models.CharField(max_length=100, verbose_name=_('Текст на кнопке'), null=True, blank=True)
    but_color = ColorField(verbose_name=_('Цвет кнопки'), default='#000000')

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

    child_in_row_count = models.PositiveSmallIntegerField(
        verbose_name=_('Количество дочерних элементов в строке'), default=3)

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







class WidgetForBlock(BaseModel):

    block = models.ForeignKey(
        Block, verbose_name=_('Родительский блок'), on_delete=models.CASCADE,
        related_name='rel_widgets_for_block'
    )

    title = models.TextField(verbose_name=_('Заголовок'), null=True, blank=True)
    description = RichTextUploadingField(verbose_name=_('Краткое описание'), null=True, blank=True)
    text = RichTextUploadingField(verbose_name=_('Дополнительное описание'), null=True, blank=True)

    picture = models.FileField(upload_to='uploads/', verbose_name=_('Картинка'), null=True, blank=True,
                                validators=[validate_file_extension])

    # block_type = models.CharField(max_length=100, choices=block_type_choices, verbose_name=_('Тип блока'), default='photo_n_text')

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

    bg_color = ColorField(verbose_name=_('Цвет фона'), default=None, null=True, blank=True)

    but_title = models.CharField(max_length=100, verbose_name=_('Текст на кнопке'), null=True, blank=True)
    but_color = ColorField(verbose_name=_('Цвет кнопки'), default='#000000')

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


    class Meta:
        verbose_name = _('Виджет в блоке')
        verbose_name_plural = _('Виджеты в блоке')



class Option(BaseModel):
    opt_type = models.CharField(max_length=250, verbose_name=_('Тип'), blank=True, null=True)
    prefix = models.CharField(max_length=250, verbose_name=_('Префикс'), blank=True, null=True)
    value = models.CharField(max_length=250, verbose_name=_('Значение'))
    picture = models.FileField(upload_to='uploads/', verbose_name=_('Миниатюра'), null=True, blank=True,
                                validators=[validate_file_extension])

    class Meta:
        verbose_name = _('Параметр')
        verbose_name_plural = _('Параметры')

class FAQitem(BaseModel):

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

    question = models.TextField(verbose_name=_('Вопрос'))
    answer = RichTextUploadingField(verbose_name=_('Ответ'))

    def __str__(self):
        if self.question:
            return self.question
        else:
            return self.id

    class Meta:
        verbose_name = 'FAQ'
        verbose_name_plural = 'FAQs'