#!/usr/bin/env python3  
# -*- coding: utf-8 -*-  
"""
@Copyright (C) ansjer cop Video Technology Co.,Ltd.All rights reserved.
@AUTHOR: ASJRD018
@NAME: langer
@software: PyCharm
@DATE: 2019/6/4 11:44
@Version: python3.6
@MODIFY DECORD:ansjer dev
@file: LangArea.py
@Contact: chanjunkai@163.com
"""

import json
from django.http import HttpResponse

from django.views.generic import TemplateView
from django.views.decorators.csrf import csrf_exempt
from django.utils.decorators import method_decorator
from object.ResponseObject import ResponseObject
from django.contrib.auth.hashers import make_password, check_password
from model.models import UserModel, LangSetModel, LangAreaModel, LangValModel, LangKeyModel, LangKeyClassModel, \
    SearchToolBlock, SearchToolKeyModel, LangLocationModel
import time
from object.TokenObject import TokenObject
from django.http import StreamingHttpResponse


class LangAreaView(TemplateView):
    def post(self, request, *args, **kwargs):
        request.encoding = 'utf-8'
        operation = kwargs.get('operation', None)
        request_dict = json.loads(request.body.decode('utf-8'))
        return self.validate(request_dict, operation)

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

    def validate(self, request_dict, operation):
        response = ResponseObject()
        token = request_dict.get('token', None)
        tko = TokenObject(token)
        if tko.code == 0:
            userID = tko.userID
            user_qs = UserModel.objects.filter(id=userID, username='admin')
            if not user_qs.exists():
                return response.json(403)
            if operation == 'add':
                return self.do_add(request_dict, response)
            elif operation == 'update':
                return self.do_update(request_dict, response)
            elif operation == 'delete':
                return self.do_delete(request_dict, response)
            elif operation == 'query':
                return self.do_query(request_dict, response, userID)
            elif operation == 'export':
                return self.do_export(request_dict, response, userID)
            else:
                return response.json(414)
        else:
            return response.json(tko.code)

    def do_add(self, request_dict, response):
        lang = request_dict.get('lang', None)
        if lang:
            nowTime = int(time.time())
            create_dict = {
                'lang': lang,
                'addTime': nowTime,
                'updTime': nowTime
            }
            try:
                LangAreaModel.objects.create(**create_dict)
            except Exception as e:
                return response.json(404, repr(e))
            else:
                return response.json(0)
        else:
            return response.json(414)

    def do_update(self, request_dict, response):
        lang = request_dict.get('lang', None)
        id = request_dict.get('id', None)
        nowTime = int(time.time())
        update_dict = {
            'lang': lang,
            'updTime': nowTime
        }
        try:
            LangAreaModel.objects.filter(id=id).update(**update_dict)
        except Exception as e:
            return response.json(404, repr(e))
        else:
            return response.json(0)

    def do_delete(self, request_dict, response):
        id = request_dict.get('id', None)
        try:
            LangAreaModel.objects.filter(id=id).delete()
        except Exception as e:
            return response.json(404, repr(e))
        else:
            return response.json(0)

    def do_query(self, request_dict, response, userID):
        user_qs = UserModel.objects.filter(id=userID, username='admin')
        if not user_qs.exists():
            return response.json(403)
        la_qs = LangAreaModel.objects.filter().values('lang', 'id')
        return response.json(0, list(la_qs))

    def do_export(self, request_dict, response, userID):
        print("进来了")
        id = request_dict.get('id', None)
        type = request_dict.get('type', None)
        # key_list = LangKeyModel.objects.filter().values_list('word_key', flat=True)
        print('id = ' + id)
        langType = (1, 3, 5, 7)
        if type == 'avss':
            langType = (2, 3, 6, 7)
        elif type == 'link_ios' or type == 'link_android':
            langType = (4, 5, 6, 7)

        en_qs = LangKeyModel.objects.filter(langvalmodel__la__id=20, type__in=langType).values('word_key',
                                                                                               'langvalmodel__word_val')
        en_kv = {}
        for e in en_qs:
            en_kv[e['word_key']] = e['langvalmodel__word_val']

        content = ''
        if type == 'ios' or type == 'link_ios' or type == 'flutter':
            res_qs = LangKeyModel.objects.filter(langvalmodel__la__id=id, type__in=langType). \
                values('word_key', 'langvalmodel__word_val')
            res = {}
            for r in res_qs:
                print(r['langvalmodel__word_val'])
                if r['langvalmodel__word_val']:
                    res[r['word_key']] = r['langvalmodel__word_val']
                elif en_kv[r['word_key']]:
                    res[r['word_key']] = en_kv[r['word_key']]
            for l in res:
                content_val = res[l].replace('"', '\'')
                if type == 'ios' or type == 'link_ios':
                    content = content + '"' + l + '"="' + content_val + '";\n'
                else:
                    content = content + '{"' + l + '":"' + content_val + '"},\n'
        elif type == 'android' or type == 'link_android':
            res_qs = LangKeyModel.objects.filter(langvalmodel__la__id=id, type__in=langType). \
                values('word_key', 'langvalmodel__word_val').order_by('addTime')
            res = {}
            for r in res_qs:
                print(r['langvalmodel__word_val'])
                if r['langvalmodel__word_val']:
                    res[r['word_key']] = r['langvalmodel__word_val']
                elif en_kv[r['word_key']]:
                    res[r['word_key']] = en_kv[r['word_key']]
            for l in res:
                lk = l
                lk = lk.replace('.', '_')
                lk = lk.replace('\\n', '_')
                lk = lk.replace('“', '_')
                lk = lk.replace('”', '_')
                lk = lk.replace(' ', '_')
                lk = lk.replace('\\\'', '_')
                lk = lk.replace('\'', '_')
                lk = lk.replace('\\', '_')
                lk = lk.replace('!', '_')
                lk = lk.replace('’', '_')
                lk = lk.replace('...', '_')
                lk = lk.replace('…', '_')
                lk = lk.replace(':', '_')
                lk = lk.replace('：', '_')
                lk = lk.replace('(', '_')
                lk = lk.replace(')', '_')
                lk = lk.replace('?', '_')
                lk = lk.replace(',', '_')
                lk = lk.replace('-', '_')
                lk = lk.replace('%', '_')
                lk = lk.replace(';', '_')
                lk = lk.replace('‘', '_')
                lk = lk.replace('&', '&amp;')
                # 正则表达式
                # re.sub("[a-z]+", "$", "123abc78ab9cdeg00t8")
                content_val = res[l].replace("'", "\\'")
                content_val = content_val.replace('"', '\\"')
                content_val = content_val.replace('&', '&amp;')
                content = content + '<string name="' + lk + '">' + content_val + '</string>\n'
        elif type == 'avss':
            content = self.do_download_avss(id)
        elif type == 'new_searchTool':
            content = self.do_download_SearchTool(id, 0)
        elif type == 'old_searchTool':
            content = self.do_download_SearchTool(id, 1)
        response = StreamingHttpResponse(content)
        response['Content-Type'] = 'application/octet-stream'
        response['Content-Disposition'] = 'attachment;filename="lang.txt"'
        return response

    def do_download_avss(self, id):
        content = ''
        en_result = self.get_area_language(20)
        target_result = self.get_area_language(id)
        if target_result:
            content += '<TS version=\"2.1\" language=\"' + self.get_avss_language(int(id)) + '\">\n'
            # keys = result.keys()
            keys = en_result.keys()
            for key in keys:
                content += '<context>\n<name>'
                content += key
                content += '</name>\n'

                messages = None
                if target_result.__contains__(key):
                    messages = target_result[key]
                else:
                    messages = en_result[key]
                messageKeys = en_result[key].keys()
                for messageKey in messageKeys:
                    message = None
                    if messages.__contains__(messageKey):
                        message = messages[messageKey]
                    else:
                        message = en_result[key][messageKey]

                    # message = messages[messageKey]
                    content += '<message>\n'
                    content += '<source>'
                    content += message['source']
                    content += '</source>\n'
                    content += '<translation>'
                    translation = message['translation']
                    # if translation is None or translation == '':
                    #     content += '23333'
                    # else:
                    content += translation
                    content += '</translation>\n'
                    locations = message['locations']
                    print(message)
                    for location in locations:
                        if location['filename'] != '':
                            content += '<location filename=\"' + location['filename'] + '\" line=\"' + str(
                                location['line']) + '\"/>\n'
                    content += '</message>\n'
                content += '</context>\n'
            content += '</TS>'
            return content
        else:
            print('none')

        return content

    def get_area_language(self, id):
        result = {}
        lkc_qs = LangKeyModel.objects.filter(langvalmodel__la__id=id, type__in=(2, 3, 6, 7)).values('word_key',
                                                                                                    'langkeyclassmodel__clazz__name',
                                                                                                    'langvalmodel__word_val',
                                                                                                    'langlocationmodel__filename',
                                                                                                    'langlocationmodel__line')
        for lkc in lkc_qs:
            # print(lkc)
            name = lkc['langkeyclassmodel__clazz__name']
            hasClass = result.__contains__(name)
            if hasClass is False:
                result[name] = {}

            source = lkc['word_key']
            if not result[name].__contains__(source):
                result[name][source] = {}
            message = result[name][source]

            message['source'] = source
            message['translation'] = lkc['langvalmodel__word_val']

            # location
            locations = []
            locations.append({'filename': lkc['langlocationmodel__filename'], 'line': lkc['langlocationmodel__line']})
            message['locations'] = locations
        print(result)
        return result

    def get_area_language_search(self, id):
        result = {}
        lkc_qs = LangKeyModel.objects.filter(langvalmodel__la__id=id, project__id=4).values('word_key',
                                                                             'searchtoolkeymodel__bk__name',
                                                                             'langvalmodel__word_val',
                                                                             'langlocationmodel__filename',
                                                                             'langlocationmodel__line')
        for lkc in lkc_qs:
            # print(lkc)
            name = lkc['searchtoolkeymodel__bk__name']
            hasClass = result.__contains__(name)
            if hasClass is False:
                result[name] = {}

            source = lkc['word_key']
            if not result[name].__contains__(source):
                result[name][source] = {}
            message = result[name][source]

            message['source'] = source[(len(name) + 1):]
            message['translation'] = lkc['langvalmodel__word_val']

            # location
            locations = []
            locations.append({'filename': lkc['langlocationmodel__filename'], 'line': lkc['langlocationmodel__line']})
            message['locations'] = locations
        print(result)
        return result

    def get_avss_language(self, id):
        if id == 18:  # 简体中文
            return 'zh_CN'
        elif id == 19:  # 繁体中文
            return 'zh_HK'
        elif id == 20:  # 英文
            return 'en_US'
        elif id == 21:  # 俄语
            return 'ru'
        elif id == 22:  # 德语
            return 'de'
        elif id == 23:  # 波兰语
            return 'pl'
        elif id == 24:  # 法语
            return 'fr'
        elif id == 25:  # 西班牙语
            return 'es'
        elif id == 26:  # 日语
            return 'ja'
        elif id == 27:  # 阿拉伯语
            return 'ar'
        elif id == 29:  # 意大利语
            return 'it'
        elif id == 30:  # 葡萄牙语
            return 'pt'
        elif id == 31:  # 荷兰语
            return 'nl'
        elif id == 32:  # 越南语
            return 'vi'
        else:
            return 'en'

    def do_download_SearchTool(self, id, searchtype):
        content = ''
        en_result = self.get_area_language_search(20)
        print(en_result)
        target_result = self.get_area_language_search(id)
        if target_result:
            content += '<TS version=\"2.1\" language=\"' + self.get_avss_language(int(id)) + '\">\n'
            # keys = result.keys()
            keys = en_result.keys()
            for key in keys:
                content += '<context>\n<name>'
                content += key
                content += '</name>\n'

                messages = None
                if target_result.__contains__(key):
                    messages = target_result[key]
                else:
                    messages = en_result[key]
                messageKeys = en_result[key].keys()
                for messageKey in messageKeys:
                    message = None
                    if messages.__contains__(messageKey):
                        message = messages[messageKey]
                    else:
                        message = en_result[key][messageKey]

                    # message = messages[messageKey]
                    content += '<message>\n'
                    content += '<source>'
                    content += message['source']
                    content += '</source>\n'
                    content += '<translation>'
                    translation = message['translation']
                    # if translation is None or translation == '':
                    #     content += '23333'
                    # else:
                    content += translation
                    content += '</translation>\n'
                    locations = message['locations']
                    print(message)
                    for location in locations:
                        if location['filename'] != '':
                            content += '<location filename=\"' + location['filename'] + '\" line=\"' + str(
                                location['line']) + '\"/>\n'
                    content += '</message>\n'
                content += '</context>\n'
            content += '</TS>'
            return content
        else:
            print('none')

        return content


class exportLangView(TemplateView):
    def post(self, request, *args, **kwargs):
        request.encoding = 'utf-8'
        request_dict = json.loads(request.body.decode('utf-8'))
        return self.validate(request_dict)

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

    def validate(self, request_dict):
        # 得到即将下载文件的路径和名称
        response = StreamingHttpResponse('123l4kjlkfjlksadjlfksdajlf')
        response['Content-Type'] = 'application/octet-stream'
        response['Content-Disposition'] = 'attachment;filename="lang.txt"'
        return response
