#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@Copyright (C) ansjer cop Video Technology Co.,Ltd.All rights reserved.
@software: PyCharm
@Version: python3.6
"""
import datetime
import traceback
import time
import logging
import jwt
import simplejson
import simplejson as json
import requests
from django.contrib.auth.hashers import make_password, check_password  # 对密码加密模块
from django.db.models import Q
from django.http import HttpResponseRedirect
from django.utils.decorators import method_decorator
from django.utils.timezone import utc
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import TemplateView
from jwt.algorithms import RSAAlgorithm
from ratelimit.decorators import ratelimit

from Ansjer.config import AuthCode_Expire, SERVER_DOMAIN, APNS_CONFIG, JPUSH_CONFIG, FCM_CONFIG, TUTK_PUSH_DOMAIN
from Controller.CheckUserData import DataValid, date_handler, RandomStr
from Model.models import Device_User, Role, UidPushModel, UserOauth2Model, UserExModel, Device_Info, UidSetModel, \
    UserAppFrequencyModel, CountryIPModel, CountryModel, RegionModel
from Object.AWS.SesClassObject import SesClassObject
from Object.AliSmsObject import AliSmsObject
from Object.RedisObject import RedisObject
from Object.ResponseObject import ResponseObject
from Object.TokenObject import TokenObject
from Service.CommonService import CommonService
from Service.ModelService import ModelService
from Service.TemplateService import TemplateService
from django.views.generic import View
import base64
import random
from io import BytesIO
from PIL import Image, ImageDraw, ImageFont
from django.shortcuts import HttpResponse
from Ansjer.config import BASE_DIR

#确认设备所在地区
class ConfirmRegion(TemplateView):
    @method_decorator(csrf_exempt)
    def dispatch(self, *args, **kwargs):
        return super(ConfirmRegion, self).dispatch(*args, **kwargs)

    def get(self, request, *args, **kwargs):
        response = ResponseObject()
        request.encoding = 'utf-8'
        # number = request.POST.get('number', None)
        # selectRegion = CountryModel.objects.filter(number=number).values('region__api_url')
        # if not selectRegion.exists():
        #     return response.json(0,{"api_url":"https://test.dvema.com"})
        # else:
        #     return response.json(0,{"api_url":selectRegion[0]['region__api_url']})

        # serial_number = request.POST.get('serial_number', None)
        ip = CommonService.get_ip_address(request)
        # ip = '14.102.176.0'
        ipInfo = CommonService.getIpIpInfo(ip,"CN")
        if ipInfo['country_code']:
            device_request_url = CountryModel.objects.filter(country_code=ipInfo['country_code']).values("region__api")
            if device_request_url.exists():
                return response.json(0,{"request_api_url":device_request_url[0]['region__api']})

        # 不存在默认返回美洲地区api。
        api = RegionModel.objects.filter(continent_code='NA').values("api")
        return response.json(0,{"request_api_url":api[0]['api']})

#确认设备所在地区
class Device_Region(object):

    def get_device_region(self, ip):

        ipInfo = CommonService.getIpIpInfo(ip, "CN")
        if ipInfo['country_code']:
            device_request_url = CountryModel.objects.filter(country_code=ipInfo['country_code']).values("region__api","region__id")
            if device_request_url.exists():
                return device_request_url[0]['region__id']
        # 不存在默认返回美洲地区api
        api = RegionModel.objects.filter(continent_code='NA').values("id")
        return api[0]['id']


