from django.core.urlresolvers import reverse_lazy
from django.utils.translation import ugettext_lazy as _

from core.views import SimpleUpdateViewBase, SimpleCreateViewBase, DetailedModelViewBase, ModelListViewBase
from .models import ClientOffice, Client, ClientCategory, ContactPerson
from core.models import AddressBase
from rest.serializers import ClientsSerializer, ClientOfficesSerializer, ContactPersonsSerializer
from rest.serializers import ClientCategoriesSerializer
from users.access_checks import LoginRequiredMixin
from users.models import LAB_GROUP_ID


# ---- clients ----
# paginated table overview for all clients, clicking a row
# sends user to detail view for that client
class ClientsListView(LoginRequiredMixin, ModelListViewBase):
    model = Client
    template_name = 'clients/clients_overview.html'
    serializer = ClientsSerializer

    additional_buttons = [
        {'url': reverse_lazy(ClientCategory.overview_url), 'label': _('categories'), 'icon': 'icon-lab'}]

    require_group = LAB_GROUP_ID

    # manually enhance query
    def get_query(self):
        return Client.objects.all().prefetch_related('category')

    def get_context_data(self, **kwargs):
        ctx = super(ClientsListView, self).get_context_data(**kwargs)
        ctx['has_categories'] = ClientCategory.objects.count() > 0
        return ctx


# detailed view for a client, asides from the actual model
# also lists the known branch offices for this client
class ClientDetailView(LoginRequiredMixin, DetailedModelViewBase):
    model = Client
    template_name = 'clients/client_detail.html'

    require_group = LAB_GROUP_ID


class ClientUpdateView(LoginRequiredMixin, SimpleUpdateViewBase):
    model = Client

    require_group = LAB_GROUP_ID


class ClientCreateView(LoginRequiredMixin, SimpleCreateViewBase):
    model = Client

    require_group = LAB_GROUP_ID


# ---- offices -----


# offices list
class ClientOfficesListView(LoginRequiredMixin, ModelListViewBase):
    serializer = ClientOfficesSerializer

    require_group = LAB_GROUP_ID

    def get_query(self):
        return ClientOffice.objects.all().prefetch_related('client', 'contact')


# detailed view onto a branch office of a client
class ClientOfficeDetailView(LoginRequiredMixin, DetailedModelViewBase):
    model = ClientOffice
    template_name = 'clients/clientoffice_detail.html'

    require_group = LAB_GROUP_ID


class ClientOfficeUpdateView(LoginRequiredMixin, SimpleUpdateViewBase):
    model = ClientOffice

    require_group = LAB_GROUP_ID


class ClientOfficeCreateView(LoginRequiredMixin, SimpleCreateViewBase):
    model = ClientOffice

    require_group = LAB_GROUP_ID

    # preselect client if called from the client detail view
    def get_initial(self):
        initial = super(ClientOfficeCreateView, self).get_initial()
        try:
            # note: if the client does not exist or some rubbish is provided
            # in the query string, this will just throw the exception, so no
            # if/else needed
            client_id = self.request.GET.get('forClient', 0)
            client = Client.objects.get(id=client_id)
            initial['client'] = client.id

            # check if we should also reuse the address data
            reuse_addr = self.request.GET.get('reuse_addr', '0')
            if reuse_addr == '1':
                for field in AddressBase._meta.get_all_field_names():
                    initial[field] = getattr(client, field)

        except ValueError:  # silently fail if a hand typed url provides rubbish
            pass
        except Client.DoesNotExist:
            pass  # who t3h hell cares.

        return initial


# ---- categories ----


class ClientCategoryListView(LoginRequiredMixin, ModelListViewBase):
    serializer = ClientCategoriesSerializer

    require_group = LAB_GROUP_ID


class ClientCategoryDetailView(LoginRequiredMixin, DetailedModelViewBase):
    model = ClientCategory
    template_name = 'clients/clientcategory_detail.html'

    require_group = LAB_GROUP_ID


class ClientCategoryCreateView(LoginRequiredMixin, SimpleCreateViewBase):
    model = ClientCategory

    require_group = LAB_GROUP_ID


class ClientCategoryUpdateView(LoginRequiredMixin, SimpleUpdateViewBase):
    model = ClientCategory

    require_group = LAB_GROUP_ID


class ContactPersonsListView(LoginRequiredMixin, ModelListViewBase):
    serializer = ContactPersonsSerializer

    require_group = LAB_GROUP_ID


class ContactPersonDetailView(LoginRequiredMixin, DetailedModelViewBase):
    model = ContactPerson
    template_name = 'clients/contactperson_detail.html'

    require_group = LAB_GROUP_ID


class ContactPersonUpdateView(LoginRequiredMixin, SimpleUpdateViewBase):
    model = ContactPerson

    require_group = LAB_GROUP_ID


class ContactPersonCreateView(LoginRequiredMixin, SimpleCreateViewBase):
    model = ContactPerson

    require_group = LAB_GROUP_ID
