#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from django.utils.decorators import method_decorator
from django.views import View
from django.views.decorators.csrf import csrf_exempt

from Model.models import LogModel
from Object.uidManageResponseObject import uidManageResponseObject
from Object.TokenObject import TokenObject


class LogView(View):

    @method_decorator(csrf_exempt)
    def dispatch(self, request, *args, **kwargs):
        return super(LogView, self).dispatch(request, *args, **kwargs)

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

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

    def validate(self, request_dict, operation):
        token = TokenObject(request_dict.get('token', None))
        response = uidManageResponseObject()

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

        if operation == 'query':
            return self.do_query(request_dict, token, response)
        elif operation == 'queryAll':
            return self.do_query_all(request_dict, token, response)
        elif operation == 'delete':
            return self.do_delete(request_dict, response)
        elif operation == 'adminQuery':
            return self.do_admin_query(request_dict, token, response)
        else:
            return response.json(404)

    def do_query(self, request_dict, token: TokenObject, response):
        page = request_dict.get('page', None)
        line = request_dict.get('line', None)

        if page and line:
            log_qs = LogModel.objects.filter(user__id=token.userID).values('id', 'operation', 'time', 'ip', 'user__username')
            if log_qs.exists():
                page = int(page)
                line = int(line)
                start = (page - 1) * line
                count = log_qs.count()
                data = log_qs[start:(start + line)]
                return response.json(0, {'count': count, 'data': list(data)})
            else:
                return response.json(0, {'count': 0, 'data': []})
        else:
            return response.json(444)

    def do_query_all(self, request_dict, token: TokenObject, response: uidManageResponseObject):
        log_qs = LogModel.objects.filter(user__id=token.userID).values('id', 'operation', 'time', 'ip')
        if log_qs.exists():
            count = log_qs.count()
            return response.json(0, {'count': count, 'data': list(log_qs)})
        else:
            return response.json(0, {'count': 0, 'data': []})

    def do_delete(self, request_dict, response):
        id = request_dict.get('id', None)

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

    def do_admin_query(self, request_dict, token: TokenObject, response):
        page = request_dict.get('page', None)
        line = request_dict.get('limit', None)

        if page and line:
            log_qs = LogModel.objects.filter().values('id', 'operation', 'time', 'ip', 'user__username')
            if log_qs.exists():
                page = int(page)
                line = int(line)
                start = (page - 1) * line
                count = log_qs.count()
                data = log_qs[start:(start + line)]
                return response.json(0, {'count': count, 'data': list(data)})
            else:
                return response.json(0, {'count': 0, 'data': []})
        else:
            return response.json(444)