import json
import threading
import time

import jwt

from Ansjer.config import SERVER_TYPE, OAUTH_ACCESS_TOKEN_SECRET
from Model.models import CloudLogModel
from Object.RedisObject import RedisObject
from Object.UidTokenObject import UidTokenObject
from Service.CommonService import CommonService
from Service.MiscellService import MiscellService
from Service.MiscellService import get_access_name

def batch_add_access_log(request, status_code):
    asy = threading.Thread(target=batch_add_log_ctr, args=(request, status_code))
    asy.start()


def batch_add_log_ctr(request, status_code):
    request.encoding = 'utf-8'
    if request.method == 'GET':
        request_dict = request.GET
    elif request.method == 'POST':
        request_dict = request.POST
    else:
        return
    request_path = request.path.strip().strip('/')
    if 'storeplaylist' in request_path or 'signplaym3u8' in request_path:   # 只记录上传和播放视频的操作
        user = get_access_name(request_dict)
        uidToken = request_dict.get('uidToken', None)
        utko = UidTokenObject(uidToken)
        uid = utko.UID
        if not uid:
            uid = request_dict.get('uid', None)
            if len(uid) > 50:
                uid = jwt.decode(uid, OAUTH_ACCESS_TOKEN_SECRET, algorithms='HS256').get('uid', '')
        clientIP = CommonService.get_ip_address(request)
        now_time = time.time()
        password = request_dict.get('userPwd', None)
        if password is not None:
            request_dict = dict(request_dict)
            request_dict.pop('userPwd')
        content = json.dumps(request_dict)
        add_data = {
            'user': user,
            'ip': clientIP,
            'uid': uid,
            'url': request.META['SERVER_PROTOCOL'] + '-' + request.method + '-' + request.path,
            'operation': request_path,
            'time': now_time,
            'content': content
        }
        print('add_data', add_data)
        redisObj = RedisObject()
        loggerData = json.dumps(add_data)
        if SERVER_TYPE == 'Ansjer.formal_settings':
            logKey = 'logger'
        else:
            logKey = 'test_logger'
        redisObj.rpush(name=logKey, val=loggerData)
        # 判断redis列表长度
        if redisObj.llen(name=logKey) > 100:
            data_list = redisObj.lrange(logKey, 0, -1)
            redisObj.del_data(key=logKey)
            add_batch_log(data_list)


# 访问日志批量添加
def add_batch_log(data_list):
    try:
        if data_list:
            querysetlist = []
            for i in data_list:
                data = json.loads(i.decode('utf-8'))
                querysetlist.append(CloudLogModel(user=data['user'],
                                                  ip=data['ip'],
                                                  uid=data['uid'],
                                                  url=data['url'],
                                                  operation=data['operation'],
                                                  time=data['time'],
                                                  content=''))
            CloudLogModel.objects.bulk_create(querysetlist)
        else:
            return
    except Exception as e:
        print(repr(e))
        return False
    else:
        return True
