from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.status import HTTP_200_OK, HTTP_400_BAD_REQUEST
from rest_framework_simplejwt.authentication import JWTAuthentication
from rest_framework.permissions import IsAuthenticated

from django.http.response import JsonResponse

from XCMS_PIM.pim_cnsts import *
from app.utils.utils import *
from app.models.label_model import LabelModel
import json


class LabelListView(APIView):
    authencation_classes = (JWTAuthentication,)
    permission_classes = (IsAuthenticated,)

    def get(self, request):
        labels_dict = {label["name"]: label for label in LabelModel.objects.values()}
        response = {"result": "success", "data": labels_dict}
        return JsonResponse(response, safe=False, status=HTTP_200_OK)


class LabelRegisterView(APIView):
    authencation_classes = (JWTAuthentication,)
    permission_classes = (IsAuthenticated,)

    def post(self, request):
        data = json.loads(request.body)
        if not hasAllAttributes(data, ["name", "color"]):
            return Response({"result": "failed", "message": "invalid request"}, status=HTTP_400_BAD_REQUEST)
        name = data["name"]
        color = data["color"]
        if LabelModel.objects.filter(name=name).exists():
            return Response({"result": "failed", "message": "label already exists"}, status=HTTP_400_BAD_REQUEST)
        label = LabelModel.objects.create(name=name, color=color)
        return Response({"result": "success", "id": label.id}, status=HTTP_200_OK)


class LabelListManageView(APIView):
    authencation_classes = (JWTAuthentication,)
    permission_classes = (IsAuthenticated,)

    def post(self, request):
        data = json.loads(request.body)
        to_update = getOrDefault(data, "update", [])
        to_delete = getOrDefault(data, "delete", [])
        to_create = getOrDefault(data, "create", [])
        labels_to_update = []
        labels_to_create = []
        for label in to_update:
            id = label["id"]
            if type(id) != int:
                continue
            label_model = get_or_none(LabelModel, id=id)
            label_model.name = getOrDefault(label, "name", label_model.name)
            label_model.color = getOrDefault(label, "color", label_model.color)
            labels_to_update.append(label_model)
        for label in to_create:
            if not hasAllAttributes(label, ["name", "color"]):
                continue
            name = label["name"]
            color = label["color"]
            if LabelModel.objects.filter(name=name).exists():
                continue
            label_model = LabelModel(name=name, color=color)
            labels_to_create.append(label_model)
        LabelModel.objects.bulk_update(labels_to_update, ["name", "color"])
        LabelModel.objects.bulk_create(labels_to_create)
        LabelModel.objects.filter(id__in=to_delete).delete()
        return Response({"result": "success"}, status=HTTP_200_OK)
