# coding=utf-8
from BaseModels.api.base_api_views import *
from AuthApp.models import *
from .serializers import *
from .api_permissions import *
from rest_framework.decorators import action



def get_buttons_states_Dict(user):

    Dict = {
        'managers_menu': user.has_perm('AuthApp.UI_managers_show'),
        'managers_add_but': user.has_perm('AuthApp.UI_managers_create'),
        'managers_change_but': user.has_perm('AuthApp.UI_managers_modify'),
        'managers_delete_but': user.has_perm('AuthApp.UI_managers_delete'),

        'admin_group_menu': user.has_perm('AuthApp.UI_adminGroup_show'), #показываем в меню группу Администрирование
        'personal_show': user.has_perm('AuthApp.UI_personal_show'), #показываем в группе Администрирование пункт Персонал
    }

    return Dict



class Client_Person_ViewSet(APIViewSet_ModelClass):
    queryset = UserProfile.objects.filter(user__is_staff=False)
    serializer_class = UserProfile_Serializer
    permission_classes = (Auth_API_perms,)

    def get_serializer_class(self):

        try:
            if self.action == 'retrieve':
                return UserProfile_Serializer

        except (KeyError, AttributeError):
            pass

        return super(Client_Person_ViewSet, self).get_serializer_class()




class Staff_Person_ViewSet(APIViewSet_ModelClass):
    queryset = UserProfile.objects.filter(user__is_staff=True)
    serializer_class = UserProfile_Serializer
    permission_classes = (Auth_API_perms,)

    def get_serializer_class(self):

        try:
            if self.action == 'retrieve':
                return UserProfile_Serializer

        except (KeyError, AttributeError):
            pass

        return super(Staff_Person_ViewSet, self).get_serializer_class()



class UserProfile_ViewSet(APIViewSet_ModelClass):
    queryset = UserProfile.objects.all()
    serializer_class = UserProfile_Serializer
    permission_classes = (Auth_API_perms,)

    def get_serializer_class(self):

        try:
            if self.action == 'retrieve':
                return UserProfile_Serializer

        except (KeyError, AttributeError):
            pass

        return super(UserProfile_ViewSet, self).get_serializer_class()

    # @action(methods=['GET'], detail=True)
    # def get_current_order(self, request, pk):
    #     from B2BApp.models import Order
    #     from B2BApp.api.serializers import B2B_Order_serializer
    #
    #     try:
    #         from B2BApp.views import get_waiting_order_or_create_new
    #         order = get_waiting_order_or_create_new(request, pk)
    #     except Order.DoesNotExist:
    #         raise serializers.ValidationError(
    #             u'Ошибка, функция недоступна'
    #         )
    #
    #     order_data = B2B_Order_serializer(order)
    #
    #     return Response(order_data.data)