#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json

from django.views import View

from Model.models import OrderTaskModel
from Object.ResponseObject import ResponseObject
from Object.TokenObject import TokenObject
from Service.ModelService import ModelService


class OrderTaskView(View):

    def get(self, request, *args, **kwargs):
        request.encoding = 'utf-8'
        operation = kwargs.get('operation', None)
        request_dict = request.GET
        return self.validate(request_dict, operation)

    def post(self, request, *args, **kwargs):
        request.encoding = 'utf-8'
        operation = kwargs.get('operation', None)
        request_dict = request.POST
        return self.validate(request_dict, operation)

    def validate(self, request_dict, operation):

        token = request_dict.get('token', None)

        response = ResponseObject()
        token = TokenObject(token)

        if token.code != 0:
            return response.json(0)

        if operation == 'query':
            return self.do_query(token.userID, request_dict, response)
        elif operation == 'update':
            return self.do_update(token.userID, request_dict, response)
        elif operation == 'delete':
            return self.do_delete(token.userID, request_dict, response)
        else:
            return response.json(404)

    def do_query(self, userID, request_dict, response):
        perm = ModelService.check_perm(userID, 0)
        if not perm:
            return response.json(309)

        page = request_dict.get('page', None)
        line = request_dict.get('limit', None)

        if page and line:

            order_task_qs = OrderTaskModel.objects.filter()
            res = {
                'count': 0,
                'data': []
            }
            if order_task_qs.exists():
                page = int(page)
                line = int(line)

                res['count'] = order_task_qs.count()
                start = (page - 1) * line
                end = start + line

                order_task_qs = order_task_qs[start:end].values()
                res['data'] = list(order_task_qs)

            return response.json(0, res)
        else:
            return response.json(444)

    def do_update(self, userID, request_dict, response):
        perm = ModelService.check_perm(userID, 0)
        if not perm:
            return response.json(309)

        id = request_dict.get('id', None)
        content = request_dict.get('content', None)
        print(content)

        if id and content:
            content = json.loads(content)
            print(content)
            OrderTaskModel.objects.filter(id=id).update(**content)
            return response.json(0)
        else:
            return response.json(444)

    def do_delete(self, userID, request_dict, response):
        perm = ModelService.check_perm(userID, 0)
        if not perm:
            return response.json(309)

        id = request_dict.get('id', None)

        if id:
            OrderTaskModel.objects.filter(id=id).delete()
            return response.json(0)
        else:
            return response.json(444)