from django.utils.translation import gettext_lazy as _
from django.shortcuts import get_object_or_404
from django.urls import reverse_lazy

from django.views.generic.base import RedirectView, TemplateView
from django.views.generic.detail import DetailView
from django.views.generic.list import ListView
from django.views.generic.edit import (CreateView, DeleteView, FormView,
                                       UpdateView)

from orchestra.contrib.saas.models import SaaS
from orchestra.contrib.resources.models import Resource, ResourceData

from orchestra.contrib.musician.settings import ALLOWED_RESOURCES
from orchestra.contrib.musician.utils import get_bootstraped_percent_exact
from orchestra.contrib.musician.mixins import (CustomContextMixin, ExtendedPaginationMixin,
                     UserTokenRequiredMixin)

from .forms import ( NextcloudChangePasswordForm, SaasNextcloudUpdateForm,
                    SaasWordpressUpdateForm, NextcloudCreateForm)


class SaasNextcloudListView(CustomContextMixin, UserTokenRequiredMixin, ListView):
    model = SaaS
    template_name = "musician/saas_nextcloud_list.html"
    extra_context = {
        # Translators: This message appears on the page title
        'title': _('Software as a Service'),
        'verbose_name': _('Nextcloud Service'),
        'description': _("Members can manage their Nextcloud users in this section."),
    }

    def get_queryset(self):
        qs =  self.model.objects.filter(account=self.request.user, service='nextcloud')
        disk_resource = Resource.objects.get(name='nextcloud-disk')

        name = self.request.GET.get('name')
        if name:
            qs = qs.filter(name__icontains=name)

        for Nuser in qs:
            try:
                Nuser.usage = Nuser.resource_set.get(resource=disk_resource)
            except ResourceData.DoesNotExist:
                Nuser.usage = ResourceData(resource=disk_resource)
            Nuser.percent = get_bootstraped_percent_exact(Nuser.usage.used, Nuser.usage.allocated)
        return qs
        

class SaasWordpressListView(CustomContextMixin, UserTokenRequiredMixin, ListView):
    model = SaaS
    template_name = "musician/saas_wordpress_list.html"
    extra_context = {
        # Translators: This message appears on the page title
        'title': _('Software as a Service'),
        'verbose_name': _('Community WordPress'),
        'description': _("Pangea's multisite WordPress service. Members can have their WordPress on this community WordPress hosted and maintained by Pangea."),
    }

    def get_queryset(self):
        qs = self.model.objects.filter(account=self.request.user, service='wordpress')
        name = self.request.GET.get('name')
        if name:
            qs = qs.filter(name__icontains=name)
        return qs


class SaasWordpressUpdateView(CustomContextMixin, UserTokenRequiredMixin, UpdateView):
    model = SaaS
    form_class = SaasWordpressUpdateForm
    template_name = "musician/saas_wordpress_form.html"

    def get_queryset(self):
        qs = SaaS.objects.filter(account=self.request.user)
        return qs

    def get_success_url(self):
        return reverse_lazy("musician:saas-wordpress-list")
    
    def get_form_kwargs(self):
        kwargs = super().get_form_kwargs()
        kwargs["user"] = self.request.user
        return kwargs


class SaasNextcloudUpdateView(CustomContextMixin, UserTokenRequiredMixin, UpdateView):
    model = SaaS
    form_class = SaasNextcloudUpdateForm
    template_name = "musician/saas_nextcloud_form.html"

    def get_queryset(self):
        qs = SaaS.objects.filter(account=self.request.user)
        return qs

    def get_success_url(self):
        return reverse_lazy("musician:saas-nextcloud-list")
    
    def get_form_kwargs(self):
        kwargs = super().get_form_kwargs()
        kwargs["user"] = self.request.user
        return kwargs

class NextcloudChangePasswordView(CustomContextMixin, UserTokenRequiredMixin, UpdateView):
    template_name = "musician/nextcloud_change_password.html"
    model = SaaS
    form_class = NextcloudChangePasswordForm
    success_url = reverse_lazy("musician:saas-nextcloud-list")

    def get_queryset(self):
        return self.model.objects.filter(account=self.request.user)


class SaasDeleteView(CustomContextMixin, UserTokenRequiredMixin, DeleteView):
    template_name = "musician/saas_check_delete.html"
    model = SaaS
    success_url = reverse_lazy("musician:saas-nextcloud-list")

    def get_queryset(self):
        return self.model.objects.filter(account=self.request.user)


class NextcloudCreateView(CustomContextMixin, UserTokenRequiredMixin, CreateView):
    model = SaaS
    template_name = "musician/saas_nextcloud_form.html"
    form_class = NextcloudCreateForm
    success_url = reverse_lazy("musician:saas-nextcloud-list")

    def get_form_kwargs(self):
        kwargs = super().get_form_kwargs()
        kwargs['user'] = self.request.user
        return kwargs

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context.update({
            'extra_user': self.is_extra_user(context['profile']),
        })
        return context
    
    def is_extra_user(self, profile):
        qs = SaaS.objects.filter(account=profile, service="nextcloud")
        number_of_users = len(qs)
        allowed_users = ALLOWED_RESOURCES[profile.type]['nextcloud']
        return number_of_users >= allowed_users