import requests
import http
import json
import redis
import requests

from django.http import JsonResponse
from django.shortcuts import render
from datacenter import settings
from smartdevice.hikTools import HikTools
from .models import Device
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt

# 获取智慧设备汇总数据
@csrf_exempt
def getSmartDeviceAnalysisData(request, what):
    if what == 'yearRiderShip':
        return getSmartDeviceYearRiderShip(request)
    elif what == 'dayRiderShip':
        return getSmartDeviceDayRiderShip(request)
    elif what == 'areaRiderShip':
        return getSmartDeviceAreaRiderShip(request)
    elif what == 'timeSlotRiderShip':
        return getSmartDeviceTimeSlotRiderShip(request)
    elif what == 'productDisplayCnt':
        return getSmartDeviceProductDisplayCnt(request)
    elif what == 'productDisplayMonthCnt':
        return getSmartDeviceProductDisplayMonthCnt(request)
    elif what == 'mapData':
        return getSmartDeviceMapData(request)

#获取智慧设备年客流量
def getSmartDeviceYearRiderShip(request):
    data = {
        'cnt': 9000
    }
    return JsonResponse(data)

#获取智慧设备日客流量
def getSmartDeviceDayRiderShip(request):
    data = {
        'cnt': 9000
    }
    return JsonResponse(data)

#获取智慧设备大区客流量
def getSmartDeviceAreaRiderShip(request):
    data = {
        'x': ['北方大区','南方大区','华东大区','西部大区'],
        'y': [100,200,300,400]
    }
    return JsonResponse(data)

#获取智慧设备时段客流量
def getSmartDeviceTimeSlotRiderShip(request):
    data = {
    "x": [
        "00:00",
        "00:30",
        "01:00",
        "01:30",
        "02:00",
        "02:30",
        "03:00",
        "03:30",
        "04:00",
        "04:30",
        "05:00",
        "05:30",
        "06:00",
        "06:30",
        "07:00",
        "07:30",
        "08:00",
        "08:30",
        "09:00",
        "09:30",
        "10:00",
        "10:30",
        "11:00",
        "11:30",
        "12:00",
        "12:30",
        "13:00",
        "13:30",
        "14:00",
        "14:30",
        "15:00",
        "15:30",
        "16:00",
        "16:30",
        "17:00"
    ],
    "y": [
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        71,
        301,
        450,
        531,
        399,
        412,
        478,
        540,
        287,
        280,
        176,
        146,
        196,
        228,
        220,
        350,
        568,
        294,
        132
    ]
}
    return JsonResponse(data)

   
#获取智慧设备产品比率
def getSmartDeviceProductDisplayCnt(request):
    data = {
        'x':
        [
            {
                'name': '测试产品1', 'value': 0.66
            },
            {
                'name': '测试产品2', 'value': 0.11
            },
            {
                'name': '测试产品3', 'value': 0.08
            },
            {
                'name': '测试产品4', 'value': 0.08
            },
            {
                'name': '测试产品5', 'value': 0.05
            },
            {
                'name': '测试产品5', 'value': 0.01
            }
        ]
    }
    return JsonResponse(data, safe=False)

#获取智慧设备产品比率
def getSmartDeviceProductDisplayMonthCnt(request):
    data = {
        "legend": [
            "测试产品1",
            "测试产品2",
            "测试产品3",
            "测试产品4",
            "测试产品5",
            "测试产品6"
        ],
        "yvl": [
            "2023-07",
            "2023-08",
            "2023-09",
            "2023-10",
            "2023-11",
            "2023-12"
        ],
        "series": [
            {
                "name": "测试系列1",
                "type": "bar",
                "stack": "total",
                "label": {
                    "show": False
                },
                "emphasis": {
                    "focus": "series"
                },
                "data": [
                    1874,
                    2354,
                    5750,
                    4264,
                    4842,
                    2886
                ]
            },
            {
                "name": "测试系列2",
                "type": "bar",
                "stack": "total",
                "label": {
                    "show": False
                },
                "emphasis": {
                    "focus": "series"
                },
                "data": [
                    628,
                    194,
                    417,
                    800,
                    827,
                    366
                ]
            },
            {
                "name": "测试系列3",
                "type": "bar",
                "stack": "total",
                "label": {
                    "show": False
                },
                "emphasis": {
                    "focus": "series"
                },
                "data": [
                    3620,
                    4370,
                    7884,
                    8140,
                    8354,
                    3910
                ]
            },
            {
                "name": "测试系列4",
                "type": "bar",
                "stack": "total",
                "label": {
                    "show": False
                },
                "emphasis": {
                    "focus": "series"
                },
                "data": [
                    1561,
                    600,
                    941,
                    1576,
                    3073,
                    1891
                ]
            },
            {
                "name": "测试系列5",
                "type": "bar",
                "stack": "total",
                "label": {
                    "show": False
                },
                "emphasis": {
                    "focus": "series"
                },
                "data": [
                    31679,
                    24867,
                    49322,
                    34738,
                    34801,
                    23637
                ]
            },
            {
                "name": "测试系列6",
                "type": "bar",
                "stack": "total",
                "label": {
                    "show": False
                },
                "emphasis": {
                    "focus": "series"
                },
                "data": [
                    1392,
                    4755,
                    6928,
                    7238,
                    6724,
                    2865
                ]
            }
        ]
    }
    return JsonResponse(data, safe=False)
    

#获取智慧设备产品比率
def getSmartDeviceMapData(request):
    data = [
        {
            "name": "内蒙古",
            "value": [
                111.76522,
                40.81733,
                20
            ]
        },
        {
            "name": "北京",
            "value": [
                116.40717,
                39.90469,
                21
            ]
        },
        {
            "name": "天津",
            "value": [
                117.19937,
                39.0851,
                11
            ]
        },
        {
            "name": "河北",
            "value": [
                114.46979,
                38.03599,
                41
            ]
        },
        {
            "name": "山西",
            "value": [
                112.56272,
                37.87343,
                8
            ]
        },
        {
            "name": "内蒙古",
            "value": [
                111.76522,
                40.81733,
                6
            ]
        },
        {
            "name": "辽宁",
            "value": [
                123.42925,
                41.83571,
                2
            ]
        },
        {
            "name": "吉林",
            "value": [
                125.3268,
                43.89616,
                3
            ]
        },
        {
            "name": "黑龙江",
            "value": [
                126.66285,
                45.74208,
                14
            ]
        },
        {
            "name": "上海",
            "value": [
                121.4737,
                31.23037,
                30
            ]
        },
        {
            "name": "江苏",
            "value": [
                118.76295,
                32.06071,
                14
            ]
        },
        {
            "name": "浙江",
            "value": [
                120.1536,
                30.26555,
                0
            ]
        },
        {
            "name": "安徽",
            "value": [
                117.28565,
                31.86157,
                6
            ]
        },
        {
            "name": "福建",
            "value": [
                119.29659,
                26.09982,
                2
            ]
        },
        {
            "name": "江西",
            "value": [
                115.91004,
                28.67417,
                5
            ]
        },
        {
            "name": "山东",
            "value": [
                117.02076,
                36.66826,
                10
            ]
        },
        {
            "name": "河南",
            "value": [
                113.75322,
                34.76571,
                19
            ]
        },
        {
            "name": "湖北",
            "value": [
                114.34234,
                30.54539,
                51
            ]
        },
        {
            "name": "湖南",
            "value": [
                112.9834,
                28.11266,
                20
            ]
        },
        {
            "name": "广东",
            "value": [
                113.26627,
                23.13171,
                1
            ]
        },
        {
            "name": "广西",
            "value": [
                108.32754,
                22.81521,
                2
            ]
        },
        {
            "name": "海南",
            "value": [
                110.34863,
                20.01997,
                12
            ]
        },
        {
            "name": "重庆",
            "value": [
                106.55073,
                29.56471,
                5
            ]
        },
        {
            "name": "四川",
            "value": [
                104.07572,
                30.65089,
                3
            ]
        },
        {
            "name": "贵州",
            "value": [
                106.70722,
                26.5982,
                2
            ]
        },
        {
            "name": "云南",
            "value": [
                102.70973,
                25.0453,
                8
            ]
        },
        {
            "name": "西藏",
            "value": [
                91.11748,
                29.64725,
                14
            ]
        },
        {
            "name": "陕西",
            "value": [
                108.95424,
                34.26486,
                56
            ]
        },
        {
            "name": "甘肃",
            "value": [
                103.82634,
                36.05942,
                5
            ]
        },
        {
            "name": "青海",
            "value": [
                101.78011,
                36.62087,
                7
            ]
        },
        {
            "name": "宁夏",
            "value": [
                106.25867,
                38.47117,
                1
            ]
        },
        {
            "name": "新疆",
            "value": [
                87.6271,
                43.79343,
                1
            ]
        },
        {
            "name": "台湾",
            "value": [
                121.520076,
                25.030724,
                0
            ]
        },
        {
            "name": "香港",
            "value": [
                114.16546,
                22.27534,
                0
            ]
        },
        {
            "name": "澳门",
            "value": [
                113.54913,
                22.19875,
                0
            ]
        }
    ]
    return JsonResponse(data, safe=False)

@csrf_exempt
def getCameraInfo(request,what,company_id,deviceSerial=None):

    hikTools = HikTools()
    access_token = hikTools.get_hik_stoken()
    if access_token:
        if what == 'getLiveAddress':
            # 查询当前公司设备是否存在设备并开启视频流
            device = Device.objects.filter(company_id=company_id, is_delete=False, is_active=True,category_id=1)
            if device is not None:
                deviceSerial = device[0].sn
                if device[0].is_open_live_video  == False:
                    is_open = hikTools.act_liveVideo(deviceSerial, 'open')
                    if is_open['code'] == 200:
                        device[0].is_open_live_video = True
                        device[0].save()

                hik_url = "https://api2.hik-cloud.com/v1/customization/liveStudio/actions/getLiveAddress"
                headers = {"Content-Type": "application/json", "Authorization": "Bearer " + access_token['access_token']}
                data = json.dumps({
                    "deviceSerial": deviceSerial,
                    "channelNo": 1,
                    "protocol": 2,
                    "expireTime": 3600,
                    "type": 1,
                    "quality": 2
                })
                res = requests.post(hik_url, headers=headers, data=data);
                if res.status_code == 200:
                    res = res.json()
                    return JsonResponse(res, safe=False)
                else:
                    return JsonResponse({'code': 200, "message": "获取预览播放地址失败"}, safe=False)
        elif what == 'getPassengerHourFlow':
            device = Device.objects.filter(company_id=company_id, is_delete=False, is_active=True, category_id=2)
            if device is not None:
                deviceSerial = device[0].sn
                hik_store_id = device[0].hik_store_id
                if device[0].is_open_live_video == False:
                    is_open = hikTools.act_liveVideo(deviceSerial, 'open')
                    if is_open['code'] == 200:
                        device[0].is_open_live_video = True
                        device[0].save()
                dt =  request.GET.get("dateTime")
                hik_url = "https://api2.hik-cloud.com/v1/customization/passenger_hour_flow?dateTime="+dt+"&storeId="+hik_store_id
                headers = {"Authorization": "Bearer " + access_token['access_token']}
                res = requests.get(hik_url, headers=headers,);
                if res.status_code == 200:
                    res = res.json()
                    return JsonResponse(res, safe=False)
                else:
                    return JsonResponse({'code': 200, "message": "门店的客流统计数据查询失败"}, safe=False)
            return JsonResponse({'code': 200, "message": "ok"}, safe=False)

    else:
        return JsonResponse({'code': 200,"message":"获取token校验错误"}, safe=False)