import re
import pytz
from datetime import datetime, time, timedelta
from django.db.models import Sum, Max
from django.core.cache import cache
from rest_framework.generics import (
    ListAPIView,
    RetrieveAPIView,
    CreateAPIView
)
from rest_framework.views import APIView
from rest_framework.response import Response

from rest_framework.permissions import IsAuthenticated
from rest_framework.filters import SearchFilter, OrderingFilter
from mosquito.api.pagination import (
    LatestDailyPagination,
    DeviceInfoListPagination,
    DeviceLogListPagination,
    DeviceLogHistoryPagination
)
from .serializers import (
    DeviceCountSerializer,
    DeviceCountWithInfoSerializer,
    DeviceInfoSerializer,
    LatestDailySerializer,
    DeviceLogHistorySerializer,
)
from ..mixins.role import RoleMixin, DeviceListMixin
from ..models import DeviceCount, DeviceInfo
from mosquito.models import MosqPostStatistic, DevicePostStatistic
from mosquito.models import DeviceInfo as CelexDeviceInfo


class DeviceLogListAPIView(ListAPIView, RoleMixin, DeviceListMixin):
    # serializer_class = DeviceCountSerializer
    permission_classes = [IsAuthenticated]
    filter_backends = [SearchFilter, OrderingFilter]
    pagination_class = DeviceLogListPagination
    search_fields = ['device_id']

    def get_serializer_class(self):
        with_device = self.request.GET.get('with_device')
        if with_device == 'true':
            return DeviceCountWithInfoSerializer
        return DeviceCountSerializer

    def get_queryset(self, *args, **kwargs):
        user_roles = self.get_role()
        device = self.request.GET.get('device')
        start = self.request.GET.get('start')
        end = self.request.GET.get('end')
        queryset = DeviceCount.objects.get_queryset().order_by('-data_time')
        if 'staff' in user_roles or 'manager' in user_roles:
            device_list = self.get_device_list()
            child = self.request.user.get_child()
            device_ids = [query.device_id
                          for query in device_list
                          if query.org == self.request.user.org or query.org.id in child]
            queryset = queryset.filter(device_id__in=device_ids)
        if device:
            # device_id or device_name
            qs = queryset.filter(device_id__icontains=device)
            if qs.count() > 0:
                queryset = qs
            else:
                qs = CelexDeviceInfo.objects.filter(device_name__contains=device)
                device_ids = [device.device_id for device in qs]
                queryset = queryset.filter(device_id__in=device_ids)
        if start and end:
            t_pattern = re.compile(r'^2\d{3}-\d{1,2}-\d{1,2}$')
            start_m = t_pattern.match(start)
            end_m = t_pattern.match(end)
            if not start_m or not end_m:
                return Response({'detail': 'start or end must be format "yyyy-mm-dd"'})
            start = datetime.strptime(start, '%Y-%m-%d')
            end = datetime.strptime(end, '%Y-%m-%d')
            queryset = queryset.filter(data_time__gte=start, data_time__lt=end + timedelta(days=1))
        return queryset


class DeviceInfoListAPIView(ListAPIView, RoleMixin):
    serializer_class = DeviceInfoSerializer
    permission_classes = [IsAuthenticated]
    filter_backends = [SearchFilter, OrderingFilter]
    pagination_class = DeviceInfoListPagination
    search_fields = ['device_id', 'online']

    def get_queryset(self, *args, **kwargs):
        user_roles = self.get_role()
        queryset = DeviceInfo.objects.all().order_by('-last_offline_time')
        if 'staff' in user_roles or 'manager' in user_roles:
            child = self.request.user.get_child()
            device_ids = [query.device_id
                          for query in queryset
                          if query.org == self.request.user.org or query.org.id in child]
            queryset = queryset.filter(device_id__in=device_ids)
        device_id = self.request.GET.get('device_id')
        status = self.request.GET.get('status')

        if device_id:
            queryset = queryset.filter(device_id__icontains=device_id)

        if status:
            queryset = queryset.filter(online=int(status))
        return queryset


class LatestStatisticAPIView(APIView, RoleMixin):
    permission_classes = [IsAuthenticated]

    def get(self, request, *args, **kwargs):
        username = request.user.username
        cur_month_count_key = 'cur_month_count_{}'.format(username)
        daily_count_key = 'daily_count_{}'.format(username)
        cur_month_count = cache.get(cur_month_count_key)
        daily_count = cache.get(daily_count_key)

        if cur_month_count and daily_count:
            data = {'cur_month_count': cur_month_count, 'daily_count': daily_count}
            return Response(data)

        # no cache
        devices = self.get_devices()
        total_count_queryset = [DeviceCount.objects.filter(device_id=x['device_id'],
                                                           data_time=x['max_time']).order_by('-count')[0]
                                for x in DeviceCount.objects.filter(
                device_id__in=devices).values('device_id').annotate(max_time=Max('data_time'))]
        if total_count_queryset:
            total_count = sum(int(device.count) for device in total_count_queryset)
        else:
            total_count = 0

        tz = pytz.timezone("UTC")
        today = datetime.now(tz).date()

        cur_month = tz.localize(datetime.combine(datetime(today.year, today.month, 1), time(0, 0)), is_dst=None)
        one_month_ago_queryset = [
            DeviceCount.objects.filter(device_id=x['device_id'], data_time=x['max_time']).order_by('-count')[0]
            for x in DeviceCount.objects.filter(
                data_time__lt=cur_month, device_id__in=devices).values('device_id').annotate(
                max_time=Max('data_time'))]
        if one_month_ago_queryset:
            one_month_ago_count = sum(int(device.count) for device in one_month_ago_queryset)
            cur_month_count = total_count - one_month_ago_count
        else:
            cur_month_count = total_count

        # set month cache
        cache.set(cur_month_count_key, cur_month_count, 60 * 10)
        midnight = tz.localize(datetime.combine(today, time(0, 0)), is_dst=None)
        one_day_ago_queryset = [DeviceCount.objects.filter(device_id=x['device_id'], data_time=x['max_time']).order_by('-count')[0]
                                for x in DeviceCount.objects.filter(
                                    data_time__lt=midnight, device_id__in=devices).values('device_id').annotate(max_time=Max('data_time'))]
        if one_day_ago_queryset:
            one_day_ago_count = sum(int(device.count) for device in one_day_ago_queryset)
            daily_count = total_count - one_day_ago_count
        else:
            daily_count = 0

        # set daily cache
        cache.set(daily_count_key, daily_count, 60 * 10)

        data = {'cur_month_count': cur_month_count, 'daily_count': daily_count}
        return Response(data)

    def get_devices(self):
        user_roles = self.get_role()
        queryset = DeviceInfo.objects.get_queryset().order_by('-online')
        if 'staff' in user_roles or 'manager' in user_roles:
            #device_ids = [query.device_id for query in queryset if query.org == self.request.user.org]
            #queryset = queryset.filter(device_id__in=device_ids)
            child = self.request.user.get_child()
            device_ids = [query.device_id
                          for query in queryset
                          if query.org == self.request.user.org or query.org.id in child]
            queryset = queryset.filter(device_id__in=device_ids)

        return queryset


class DeviceStatusAPIView(APIView, RoleMixin):
    permission_classes = [IsAuthenticated]

    def get(self, request, *args, **kwargs):
        user_roles = self.get_role()
        online_devices = DeviceInfo.objects.filter(online=1)
        offline_devices = DeviceInfo.objects.filter(online=0)
        if 'staff' in user_roles or 'manager' in user_roles:
            child = self.request.user.get_child()
            online_ids = [query.device_id for query in online_devices
                          if query.org == self.request.user.org or query.org.id in child]
            offline_ids = [query.device_id for query in offline_devices
                           if query.org == self.request.user.org or query.org.id in child]
            online_devices = online_devices.filter(device_id__in=online_ids)
            offline_devices = offline_devices.filter(device_id__in=offline_ids)
        online_count = online_devices.count()
        offline_count = offline_devices.count()

        data = {'online_count': online_count, 'offline_count': offline_count}
        return Response(data)


class LatestDailyListAPIView(ListAPIView, RoleMixin, DeviceListMixin):
    """
    用户下所有设备总数统计
    """
    serializer_class = LatestDailySerializer
    permission_classes = [IsAuthenticated]
    filter_backends = [SearchFilter, OrderingFilter]
    pagination_class = LatestDailyPagination
    search_fields = ['date']
    queryset = MosqPostStatistic.objects.all().order_by('-date')

    def list(self, request, *args, **kwargs):
        user_roles = self.get_role()
        resp = super().list(request, *args, **kwargs)
        results = resp.data['results']
        if 'staff' in user_roles or 'manager' in user_roles:
            devices = self.get_device_list()
            #device_ids = [device.device_id for device in devices if device.org == self.request.user.org]
            child = self.request.user.get_child()
            device_ids = [query.device_id
                          for query in devices
                          if query.org == self.request.user.org or query.org.id in child]
            device_daily_qs = DevicePostStatistic.objects.filter(device_id__in=device_ids)
            for item in results:
                date = item['date']
                data = device_daily_qs.filter(date=date)
                total = sum(i.total for i in data)
                increment = sum(i.increment for i in data)
                item['total'] = total
                item['increment'] = increment

        return resp


class DeviceDailyListAPIView(ListAPIView, RoleMixin, DeviceListMixin):
    """
    设备每天日志
    """
    serializer_class = DeviceLogHistorySerializer
    permission_classes = [IsAuthenticated]
    filter_backends = [SearchFilter, OrderingFilter]
    pagination_class = DeviceLogHistoryPagination
    search_fields = ['device_id', 'date']

    def get_queryset(self, *args, **kwargs):
        user_roles = self.get_role()
        device_id = self.request.GET.get('device_id')
        start = self.request.GET.get('start')
        end = self.request.GET.get('end')
        queryset = DevicePostStatistic.objects.get_queryset().order_by('-date')

        if 'staff' in user_roles or 'manager' in user_roles:
            devices = self.get_device_list()
            #device_ids = [device.device_id for device in devices if device.org == self.request.user.org]
            child = self.request.user.get_child()
            device_ids = [query.device_id
                          for query in devices
                          if query.org == self.request.user.org or query.org.id in child]
            queryset = queryset.filter(device_id__in=device_ids)

        if device_id:
            queryset = queryset.filter(device_id__icontains=device_id)

        if start and end:
            t_pattern = re.compile(r'^2\d{3}-\d{1,2}-\d{1,2}$')
            start_m = t_pattern.match(start)
            end_m = t_pattern.match(end)
            if not start_m or not end_m:
                return Response({'detail': 'start or end must be format "yyyy-mm-dd"'})
            start = datetime.strptime(start, '%Y-%m-%d')
            end = datetime.strptime(end, '%Y-%m-%d')
            queryset = queryset.filter(date__gte=start, date__lt=end + timedelta(days=1))

        return queryset
