#!/usr/bin/env python3  
# -*- coding: utf-8 -*-  
"""
@Copyright (C) ansjer cop Video Technology Co.,Ltd.All rights reserved.
@AUTHOR: ASJRD018
@NAME: Ansjer
@software: PyCharm
@DATE: 2018/6/8 9:10
@Version: python3.6
@MODIFY DECORD:ansjer dev
@file: SysManage.py
@Contact: chanjunkai@163.com
"""
import os

from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.views.generic.base import View

from Ansjer.config import BASE_DIR
from Object.ResponseObject import ResponseObject
from Object.TokenObject import TokenObject
from Service.ModelService import ModelService
from Model.models import SysMsgModel, Equipment_Info, Device_Info


@csrf_exempt
def updateLog(request):
    response = ResponseObject()
    request.encoding = 'utf-8'
    if request.method == 'GET':
        request_dict = request.GET
    if request.method == 'POST':
        request_dict = request.POST
    else:
        response.json(404)
    token = request_dict.get('token', None)
    content = request_dict.get('content', None)
    if not content:
        return response.json(444, 'content')
    tko = TokenObject(token)
    response.lang = tko.lang
    if tko.code != 0:
        return response.json(tko.code)
    userID = tko.userID
    own_permission = ModelService.check_perm(userID=userID, permID=50)
    if own_permission is not True:
        return response.json(404)
    file_path = os.path.join(BASE_DIR, 'static/Help/version.txt')
    try:
        with open(file_path, "w", encoding="utf-8") as f:
            f.write(content)
    except Exception as e:
        pass
    return response.json(0)


class getStatView(View):
    def post(self, request, *args, **kwargs):
        request.encoding = 'utf-8'
        filePath = kwargs.get('filePath', None)
        filePath.encode(encoding='utf-8', errors='strict')
        response = ResponseObject()
        return self.getFile(filePath, response)

    def get(self, request, *args, **kwargs):
        request.encoding = 'gb2312'
        filePath = kwargs.get('filePath', None)
        response = ResponseObject()
        filePath.encode(encoding='gb2312', errors='strict')
        return self.getFile(filePath, response)

    def getFile(self, filePath, response):
        if filePath:
            pass
        else:
            return response.json(800)
        fullPath = os.path.join(BASE_DIR, filePath).replace('\\', '/')
        from var_dump import var_dump
        var_dump(fullPath)
        if os.path.isfile(fullPath):
            try:
                Imagedata = open(fullPath, 'rb').read()
            except Exception as e:
                return response.json(906, repr(e))
            else:
                return HttpResponse(Imagedata, content_type="image/jpeg")
        else:
            return response.json(907)


def initMsgFunc(request):
    response = ResponseObject()
    request.encoding = 'utf-8'
    if request.method == 'GET':
        request_dict = request.GET
    if request.method == 'POST':
        request_dict = request.POST
    else:
        response.json(404)
    token = request_dict.get('token', None)
    tko = TokenObject(token)
    response.lang = tko.lang
    if tko.code == 0:
        userID = tko.userID
        sm_count = SysMsgModel.objects.filter(userID_id=userID, status=0).count()
        eq_count = Equipment_Info.objects.filter(userID_id=userID).filter(status=False).count()
        rq_count = Equipment_Info.objects.filter(userID_id=userID).filter(eventType=57, status=False,).count()
        uid_reset_count = Device_Info.objects.filter(userID_id=userID, isExist=2).count()
        res = {
            'sm_count': sm_count,  # 系统消息未读数量
            'eq_count': eq_count,  # 未读消息总数
            'rq_count': rq_count,  # 人形检测总数
            'uid_reset_count': uid_reset_count,# 复位的设备数量
        }
        return response.json(0, res)
    else:
        return response.json(tko.code)
