from backend.models.categories import SubCategoryModel
from django.views.generic.detail import DetailView
from django.shortcuts import get_object_or_404
from backend.models import User, CategoryModel


class DashboardView(DetailView):
    model = User
    template_name = 'dashboard.html'
    context_object_name = 'user'

    def get_object(self):
        # user = get_object_or_404(User, username=self.kwargs['username'])
        # print(user)
        # public_categories = CategoryModel.objects.public_categories()
        # print(public_categories)
        # private_categories = CategoryModel.objects.private_categories(
        #     user=user)
        # print(private_categories)
        return get_object_or_404(User, username=self.kwargs['username'])

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        user = get_object_or_404(User, username=self.kwargs['username'])
        for category in CategoryModel.objects.public_categories():
            print(category)
            if (category.children):
                for child in category.children.all():
                    print(f'\t{child} {child.ispublic}')
        print()
        for category in CategoryModel.objects.private_categories(user=user):
            print(category)
            if (category.children):
                for child in category.children.all():
                    print(f'\t{child} {child.ispublic}')
        print()
        for category in CategoryModel.objects.user_categories(user=user):
            print(category)
            if (category.children):
                for child in category.children.all():
                    print(f'\t{child} {child.ispublic}')

        context['public_categories'] = CategoryModel.objects.public_categories()
        context['private_categories'] = CategoryModel.objects.private_categories(user=user)
        # context['public_subcategories'] = SubCategoryModel.objects.public_categories()
        # context['private_subcategories'] = SubCategoryModel.objects.private_categories(user=user)
        return context