#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@Copyright (C) ansjer cop Video Technology Co.,Ltd.All rights reserved.
@AUTHOR: ASJRD018
@NAME: AnsjerFormal
@software: PyCharm
@DATE: 2018/12/5 9:30
@Version: python3.6
@MODIFY DECORD:ansjer dev
@file: cloudstorage.py
@Contact: chanjunkai@163.com
"""
import json
import time
import urllib
import uuid
import hashlib

import boto3
import oss2
import paypalrestsdk
import threading
from aliyunsdkcore import client
from aliyunsdksts.request.v20150401 import AssumeRoleRequest
from boto3.session import Session
from django.http import JsonResponse, HttpResponseRedirect, HttpResponse
from django.views.generic.base import View

from Ansjer.config import OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET, OSS_ROLE_ARN, SERVER_DOMAIN, PAYPAL_CRD, \
    SERVER_DOMAIN_SSL
from Model.models import Device_Info, Order_Model, Store_Meal, VodHlsModel, OssCrdModel, UID_Bucket, StsCrdModel, \
    ExperienceContextModel, CDKcontextModel
from Object.AliPayObject import AliPayObject
from Object.ResponseObject import ResponseObject
from Object.TokenObject import TokenObject
from Object.UidTokenObject import UidTokenObject
from Service.CommonService import CommonService
from Object.m3u8generate import PlaylistGenerator
from Object.WechatPayObject import WechatPayObject
from django.db.models import Q, F
from django.http import StreamingHttpResponse

SERVER_DOMAIN = 'http://test.dvema.com/'


# 设备信息添加
class CDKView(View):

    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:
            token = request_dict.get('token', None)
            # 设备主键uid
            tko = TokenObject(token)
            response.lang = tko.lang
            if tko.code != 0:
                return response.json(tko.code)
            userID = tko.userID
            if operation == 'createCDK':
                return self.createCDK(request_dict, response)
            elif operation == 'deleteCDK':
                return self.deleteCDK(request_dict, response)
            elif operation == 'queryCDK':
                return self.queryCDK(request_dict, response)
            elif operation == 'saveOrEditCDKform':
                return self.saveOrEditCDKform(request_dict, response)
            elif operation == 'saveOrEditCDK':
                return self.saveOrEditCDK(request_dict, response)
            elif operation == 'downloadCDK':
                return self.downloadCDK(request_dict, response)

    def createCDK(self, request_dict, response):
        cdk_num = request_dict.get("cdknum", None)
        rank = request_dict.get('rank', None)
        order = request_dict.get('order', None)
        cdk_list = []
        for i in range(int(cdk_num)):
            nowTime = int(time.time())
            cdk = hashlib.md5((str(uuid.uuid1()) + str(nowTime)).encode('utf-8')).hexdigest()
            cdk_model = CDKcontextModel(
                cdk=cdk,
                create_time=nowTime,
                valid_time=0,
                is_activate=0,
                rank_id=rank,
                order=order,
            )
            cdk_list.append(cdk_model)
        try:
            CDKcontextModel.objects.bulk_create(cdk_list)
        except Exception as e:
            print(repr(e))
            return response.json(404, repr(e))
        else:
            return response.json(0)

    def queryCDK(self, request_dict, response):
        page = int(request_dict.get('page', None))
        line = int(request_dict.get('line', None))
        # channel = request_dict.get('channel', None)
        cdk = request_dict.get('cdk', None)
        order = request_dict.get('order', None)
        is_activate = request_dict.get('is_activate', None)
        lang = request_dict.get('lang', 'cn')
        searchVal = ''
        if cdk:
            searchVal = cdk.strip()
        elif order:
            searchVal = order.strip()
        elif is_activate:
            searchVal = is_activate.strip()

        if page and line:
            cdk_qs = CDKcontextModel.objects.filter().all()  # values('cdk','create_time','valid_time','is_activate','rank__id','order__id')
            if searchVal:
                if cdk:
                    cdk_qs = cdk_qs.filter(cdk__contains=searchVal)
                elif order:
                    cdk_qs = cdk_qs.filter(order=searchVal)
                elif is_activate:
                    cdk_qs = cdk_qs.filter(is_activate=searchVal)


            cdk_qs = cdk_qs.filter(rank__lang__lang=lang)
            cdk_qs = cdk_qs.annotate(rank__title=F('rank__lang__title'))
            cdk_qs = cdk_qs.values('id', 'cdk', 'create_time', 'valid_time', 'is_activate', 'rank__id', 'rank__title', 'order',
                                   'create_time')
            cdk_qs = cdk_qs.order_by('-create_time')  # 根据CDK创建时间降序排序
            count = cdk_qs.count()
            cdk_qs = cdk_qs[(page - 1) * line:page * line]

            # cdk_dict = {}
            # for cdk in cdk_qs:
            #     cdk_dict[cdk['id']] = {'id': cdk['id'], 'cdk': cdk['cdk'], 'create_time': cdk['create_time'],
            #                            'valid_time': cdk['valid_time'], 'is_activate': cdk['is_activate'],
            #                            'rank': cdk['rank__id'], 'order': cdk['order']}

            res = {
                'datas': list(cdk_qs),
                'count': count
            }
            return response.json(0, res)
        else:
            return response.json(444, 'page,line')

    def deleteCDK(self, request_dict, response):
        cdk_id = request_dict.get("id", None)
        lang = request_dict.get("lang", 'cn')
        try:
            CDKcontextModel.objects.get(cdk=cdk_id).delete()
        except Exception as e:
            return response.json(500, repr(e))
        else:
            page = int(request_dict.get('page', None))
            line = int(request_dict.get('line', None))
            # channel = request_dict.get('channel', None)
            cdk = request_dict.get('cdk', None)
            order = request_dict.get('order', None)
            is_activate = request_dict.get('is_activate', None)

            searchVal = ''
            if cdk:
                searchVal = cdk.strip()
            elif order:
                searchVal = order.strip()
            elif is_activate:
                searchVal = is_activate.strip()

            if page and line:
                cdk_qs = CDKcontextModel.objects.filter().all()  # values('cdk','create_time','valid_time','is_activate','rank__id','order__id')
                if searchVal:
                    if cdk:
                        cdk_qs = cdk_qs.filter(cdk__contains=searchVal)
                    elif order:
                        cdk_qs = cdk_qs.filter(order=searchVal)
                    elif is_activate:
                        cdk_qs = cdk_qs.filter(is_activate=searchVal)


                cdk_qs = cdk_qs.filter(rank__lang__lang=lang)
                cdk_qs = cdk_qs.annotate(rank__title=F('rank__lang__title'))
                cdk_qs = cdk_qs.values('id', 'cdk', 'create_time', 'valid_time', 'is_activate', 'rank__id',
                                       'rank__title', 'order',
                                       'create_time')
                cdk_qs = cdk_qs.order_by('-create_time')  # 根据CDK创建时间降序排序
                count = cdk_qs.count()
                cdk_qs = cdk_qs[(page - 1) * line:page * line]
                res = {
                    'datas': list(cdk_qs),
                    'count': count
                }
                return response.json(0, res)



    def saveOrEditCDK(self, request_dict, response):
        cdk_id = request_dict.get("id", None)
        cdk = request_dict.get('cdk', None)
        valid_time = request_dict.get('valid_time', None)
        is_activate = request_dict.get('is_activate', None)
        rank = request_dict.get('rank', None)

        try:
            if cdk_id:
                if cdk or valid_time or is_activate or rank:
                    update_dict = {}
                    if cdk:
                        update_dict['cdk'] = cdk
                    if valid_time:
                        update_dict['valid_time'] = valid_time
                    if is_activate:
                        update_dict['is_activate'] = is_activate
                    if rank:
                        update_dict['rank'] = rank
                    CDKcontextModel.objects.filter(id=cdk_id).update(**update_dict)
        except Exception as e:
            return response.json(500, repr(e))
        else:
            return response.json(0)

    def downloadCDK(self,request_dict, response):
        region = request_dict.get('region', None)
        content = ''
        if region == 'cn':
            # 下载国内未使用激活码
            content += '激活码(国内)\n'
            cdk_inactivate_qs = CDKcontextModel.objects.filter(is_activate=0, rank__bucket__mold=0).values('cdk')
        else:
            # 下载国外未使用激活码
            content += '激活码(国外)\n'
            cdk_inactivate_qs = CDKcontextModel.objects.filter(is_activate=0, rank__bucket__mold=1).values('cdk')
        for cdk_inactivate in cdk_inactivate_qs:
            content += cdk_inactivate['cdk'] + '\n'
        # print(content)

        response = StreamingHttpResponse(content)
        response['Content-Type'] = 'application/octet-stream'
        response['Content-Disposition'] = 'attachment;filename="CDK.txt"'
        return response





