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 EmailConfigModel, EmailLangConfig, EmailSortModel, EmailProjectConfigModel


class EmailUserView(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_addorupdate_index':
                return self.do_addorupdate_index(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_addorupdate_index(self, request_dict, response):
        lang = EmailLangConfig.objects.values('emaillangid', 'lang')
        sort = EmailSortModel.objects.values('emailsortid', 'sort')
        project = EmailProjectConfigModel.objects.values('emailprojectid', 'projecname')
        a = []
        a.append(list(lang))
        a.append(list(sort))
        a.append(list(project))
        return response.json(0, a)

    def do_add(self, request_dict, response):
        fromaddr = request_dict.get('fromaddr', None)
        password = request_dict.get('password', None)
        emailservername = request_dict.get('emailservername', None)
        emailserver = request_dict.get('emailserver', None)
        emailserverport = request_dict.get('emailserverport', None)
        emailserverportagreement = request_dict.get('emailserverportagreement', None)
        emailtag = request_dict.get('emailtag', None)
        emailproject_id = request_dict.get('emailproject_id', None)
        emailsort_id = request_dict.get('emailsort_id', None)
        emaillangconfig = request_dict.get('emaillangconfig', None)
        a = EmailConfigModel.objects.create(fromaddr=fromaddr, password=password, emailservername=emailservername,
                                            emailserver=emailserver, emailserverport=emailserverport,
                                            emailserverportagreement=emailserverportagreement,
                                            emailtag=emailtag, emailproject_id=emailproject_id,
                                            emailsort_id=emailsort_id)
        emaillangconfig = emaillangconfig.split(',')
        if len(emaillangconfig) > 0:
            a.emaillangconfig.set(emaillangconfig)
        return response.json(0)

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

    def do_update(self, request_dict, response):
        emailid = request_dict.get('emailid', None)
        fromaddr = request_dict.get('fromaddr', None)
        password = request_dict.get('password', None)
        emailservername = request_dict.get('emailservername', None)
        emailserver = request_dict.get('emailserver', None)
        emailserverport = request_dict.get('emailserverport', None)
        emailserverportagreement = request_dict.get('emailserverportagreement', None)
        emailtag = request_dict.get('emailtag', None)
        emailproject_id = request_dict.get('emailproject_id', None)
        emailsort_id = request_dict.get('emailsort_id', None)
        emaillangconfig = request_dict.get('emaillangconfig', None)
        EmailConfigModel.objects.filter(emailid=emailid).update(
            fromaddr=fromaddr, password=password,
            emailservername=emailservername,
            emailserver=emailserver, emailserverport=emailserverport,
            emailserverportagreement=emailserverportagreement,
            emailtag=emailtag, emailproject_id=emailproject_id,
            emailsort_id=emailsort_id)
        emaillangconfig = emaillangconfig.split(',')
        if len(emaillangconfig) > 0:
            EmailConfigModel.objects.get(emailid=emailid).emaillangconfig.set(emaillangconfig)
        return response.json(0)

    def do_query(self, request_dict, response):
        a = EmailConfigModel.objects.values('fromaddr', 'emailservername', 'emailtag', 'emailsort_id__sort',
                                            'emailproject_id__projecname', 'emaillangconfig__lang')
        return response.json(0, list(a))
