from .. import settings
from django.views import View as DjangoView
from django.views.generic import FormView as DjangoFormView
from typing import Any

class Base:
    base_template_name = settings.TINYWIKI_BASE_TEMPLATE

    @property
    def user_can_edit_pages(self)->bool:
        if (self.request.user.is_staff 
            or self.request.user.has_perm('page.tiynwiki-edit')
            or self.request.user.has_perm('page.tinywiki-edit-all')):
            return True
        return False
    
    @property
    def user_can_edit_all_pages(self)->bool:
        if (self.request.user.is_staff 
            or self.request.user.has_perm('page.tinywiki-edit-all')):
            return True
        return False
    
    @property
    def user_can_edit_system_pages(self)->bool:
        if (self.request.user.is_staff 
            or self.request.user.has_perm('page.tiynwiki-edit-system')):
            return True
        return False
    
    @property
    def user_can_create_pages(self)->bool:
        if (self.request.user.is_staff 
            or self.request.user.has_perm('page.tiynwiki-create')):
            return True
        return False
    
    @property
    def user_can_create_system_pages(self)->bool:
        if (self.request.user.is_staff 
            or self.request.user.has_perm('page.tiynwiki-create-system')):
            return True
        return False
    
    def page_is_creatable(self,slug:str)->bool:
        if not slug:
            return False
        if slug.startswith('tw-'):
            return self.user_can_create_system_pages
        return self.user_can_create_pages
        
    def page_is_editable(self,page)->bool:
        if page.slug.startswith('tw-'):
            return self.user_can_edit_system_pages
        elif (self.user_can_edit_all_pages 
              or (self.request.user.pk == page.author.pk and self.user_can_edit_pages)):
            return True
        return False
        
    @classmethod
    def get_base_template_name(cls)->str:
        return cls.base_template_name
    
    @classmethod
    def get_template_name(cls)->str:
        return cls.template_name
    
    def get_tinywiki_context_data(self):
        create_pages = False
        if self.request.user.is_authenticated:
            if self.request.user.is_staff or self.request.user.has_perm('tinywiki.tinywiki-create'):
                create_pages = True
        return {
            'brand_logo': settings.TINYWIKI_BRAND_LOGO,
            'brand_name': settings.TINYWIKI_BRAND_NAME,
            'base_template': self.get_base_template_name(),
            'use_bootstrap': settings.USE_BOOTSTRAP,
            'user_can_create_wiki_pages':create_pages
        }
        
class View(Base,DjangoView):
    template_name = "tinywiki"
    
    def get_context_data(self,**kwargs):
        context = self.get_tinywiki_context_data()
        context.update(kwargs)
        return context
    
class FormView(Base,DjangoFormView):
    def get_context_data(self, **kwargs) -> dict[str, Any]:
        context = self.get_tinywiki_context_data()
        context.update(kwargs)
        return super().get_context_data(**context)
    