import boto3
from boto3 import Session
from django.views import View

from model.models import EmailSortModel, EmailConfigModel
from Utils.NeteaseMail import NeteaseMail
from django.views.generic import TemplateView

from object.ResponseObject import ResponseObject
from model.models import EmailSortModel


class EmailSorView(TemplateView):

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

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

    def validation(self, request_dict, request, operation):
        response = ResponseObject()
        if operation is None:
            return response.json(444, 'error path')
        else:
            if operation == 'do_add':
                return self.do_add(request_dict, response)
            elif operation == 'do_delete':
                return self.do_delete(request_dict, response)
            elif operation == 'do_update':
                return self.do_update(request_dict, response)
            elif operation == 'do_query':
                return self.do_query(request_dict, response)

    def do_add(self, request_dict, response):
        sort = request_dict.get('sort', None)
        sortcode = request_dict.get('sortcode', None)
        parentsortid = request_dict.get('parentsortid', None)
        isautoreply = request_dict.get('isautoreply', None)
        autoreplymodel = request_dict.get('autoreplymodel', None)
        EmailSortModel.objects.create(sort=sort, sortcode=sortcode, parentsortid=parentsortid,
                                      isautoreply=isautoreply, autoreplymodel=autoreplymodel)
        return response.json(0)

    def do_delete(self, request_dict, response):
        emailsortid = request_dict.get('emailsortid', None)
        if emailsortid:
            EmailSortModel.objects.filter(emailsortid=emailsortid).delete()
            return response.json(0)
        else:
            return response.json(444, 'id does not exist')

    def do_update(self, request_dict, response):
        emailsortid = request_dict.get('emailsortid', None)
        sort = request_dict.get('sort', None)
        sortcode = request_dict.get('sortcode', None)
        parentsortid = request_dict.get('parentsortid', None)
        isautoreply = request_dict.get('isautoreply', None)
        autoreplymodel = request_dict.get('autoreplymodel', None)
        EmailSortModel.objects.filter(emailsortid=emailsortid).update(sort=sort, sortcode=sortcode,
                                                                      parentsortid=parentsortid,
                                                                      isautoreply=isautoreply,
                                                                      autoreplymodel=autoreplymodel)
        return response.json(0)

    def do_query(self, request_dict, response):
        a = EmailSortModel.objects.values('emailsortid', 'sort', 'sortcode', 'parentsortid',
                                          'isautoreply', 'autoreplymodel')
        return response.json(0, list(a))

