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

from django.views import View

from Model.models import LanguageModel
from Object.uidManageResponseObject import uidManageResponseObject
from Object.TokenObject import TokenObject
from Service.ModelService import ModelService


class LanguageView(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 = TokenObject(request_dict.get('token', None))

        response = uidManageResponseObject()

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

        if operation == 'add':
            return self.do_add(token.userID, request_dict, response)
        elif operation == 'update':
            return self.do_update(token.userID, request_dict, response)
        elif operation == 'getLanguage':
            return self.do_get(request_dict, response)
        else:
            return response.json(404)

    def do_add(self, userID, request_dict, response):
        # perm = ModelService.check_perm_uid_manage(userID, 0)
        # if not perm:
        #     return response.json(309)

        lang = request_dict.get('lang', None)
        lang_youdao = request_dict.get('lang_youdao', None)

        if lang:
            now_time = int(time.time())

            language = LanguageModel(lang=lang, lang_youdao=lang_youdao, add_time=now_time, update_time=now_time)
            language.save()
            return response.json(0)
        else:
            return response.json(444)

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

        id = request_dict.get('id', None)
        if id:
            lang = request_dict.get('lang', None)
            lang_youdao = request_dict.get('lang_youdao', None)

            update = {

            }
            if lang:
                update['lang'] = lang

            if lang_youdao:
                update['lang_youdao'] = lang_youdao

            LanguageModel.objects.filter(id=id).update(**update)
            return response.json(0)
        else:
            return response.json(444)

    def do_get(self, request_dict, response):
        type = request_dict.get('type', None)

        if type:
            lang_qs = LanguageModel.objects.filter(lang=type)
            if lang_qs.exists():
                lang = lang_qs.values('id', 'lang', 'lang_youdao')

                return response.json(0, lang)
            else:
                return response.json(173)
        else:
            return response.json(444)