from django.views.generic import TemplateView, DetailView, ListView
from main.models import Page, Category, Product, CentralOfficeContact, Office, News, SliderOne, SliderTwo


class HomeView(TemplateView):
    template_name = 'main/index.html'

    def get_context_data(self, **kwargs):
        most_popular_category = Category.objects.all()[:3]
        news = News.objects.all()[:10]
        slider_one = SliderOne.objects.all()
        slider_two = SliderTwo.objects.all()[:4]
        return {
            "most_popular_category": most_popular_category,
            "news": news,
            'slider_one': slider_one,
            'slider_two': slider_two,
        }


class AboutHistory(DetailView):
    template_name = 'about.html'
    model = Page
    context_object_name = 'page'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        obj = self.model.objects.get(pk=self.kwargs['pk'])
        if obj.parent:
            context['menu_pages'] = Page.objects.filter(parent=obj.parent)
        else:
            context['menu_pages'] = Page.objects.filter(parent=obj)
        return context


class PageView(DetailView):
    template_name = 'about.html'
    model = Page
    context_object_name = 'page'


class CategoryDetail(DetailView):
    template_name = 'main/product.html'
    model = Category
    context_object_name = 'category'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        category = self.request.GET.getlist('category', None)
        search = self.request.GET.get('search', None)
        q = self.request.GET.get('q', None)
        if q:
            context['products'] = Product.objects.filter(title__icontains=q)
            return context
        if not category:
            category = Category.objects.get(id=self.kwargs['pk'])
            categories_id = [category.id]
            categories_id += [x.id for x in category.children.all()]
            context['products'] = Product.objects.filter(category_id__in=categories_id)
        else:
            categories = []
            for category in category:
                ct = Category.objects.get(id=category)
                categories += category
                categories += [x.id for x in ct.children.all()]
            context['products'] = Product.objects.filter(category_id__in=categories)

        if search:
            if search == 'abc':
                context['products'] = context['products'].order_by('-title')
        else:
            context['products'].order_by('-id')
        context['search'] = search
        return context


class Products(ListView):
    template_name = 'main/products.html'
    model = Category
    context_object_name = 'categories'


class DetailProduct(DetailView):
    model = Product
    template_name = 'main/preparation.html'
    context_object_name = 'product'


class CentralOffice(TemplateView):
    template_name = 'main/contacts.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['contacts'] = CentralOfficeContact.objects.all()
        return context


class RegionalOffices(TemplateView):
    template_name = 'main/regional_offices.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['offices'] = Office.objects.all()
        return context


class RegionalOfficesDetailView(DetailView):
    template_name = 'main/detail_regional_offices.html'
    model = Office
    context_object_name = 'office'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['offices'] = Office.objects.all()
        context['id'] = self.kwargs['pk']
        return context


class NewsView(ListView):
    template_name = 'main/news.html'
    model = News
    context_object_name = 'news'
    paginate_by = 10

class NewsDetailView(DetailView):
    template_name = 'main/detail_news.html'
    model = News
    context_object_name = 'one_news'
