#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import datetime
import json
import random

from django.utils.decorators import method_decorator
from django.views import View
from django.views.decorators.csrf import csrf_exempt

from Ansjer.config import SALES, ONLINE_DEVICE
from Object.uidManageResponseObject import uidManageResponseObject


class SalesView(View):

    @method_decorator(csrf_exempt)
    def dispatch(self, request, *args, **kwargs):
        return super(SalesView, self).dispatch(request, *args, **kwargs)

    def get(self, request, *args, **kwargs):
        request_dict = request.GET
        return self.validate(request_dict)

    def post(self, request, *args, **kwargs):
        request_dict = request.POST
        return self.validate(request_dict)

    def validate(self, request_dict):
        year = request_dict.get('year', 0)
        response = uidManageResponseObject()
        if year:
            year = int(year)
            if year == 0:
                data = SALES.copy()
                data[2020] = self.get_last_year_data(SALES[2020])
                return response.json(0, {'data': self.format_data(data)})
            elif 2015 < year < 2020:
                data = {year: SALES[year]}
                return response.json(0, {'data': self.format_data(data)})
            elif year == 2020:
                data = {year: self.get_last_year_data(SALES[year])}
                return response.json(0, {'data': self.format_data(data)})
            else:
                return response.json(444)
        else:
            return response.json(444)

    def get_last_year_data(self, data):
        result = []
        month = datetime.datetime.now().month
        print(month)
        sum = 0
        for item in data:
            value = item['value']
            sale = int(value[0] / 12 * month)
            sum += sale

        for item in data:
            tmp = {}
            tmp['name'] = item['name']
            value = item['value']
            sale = int(value[0] / 12 * month)
            percent = round(sale * 100 / sum, 3)

            tmpValue = []
            tmpValue.append(sale)
            tmpValue.append(percent)
            tmpValue.append(value[2])
            tmp['value'] = tmpValue
            result.append(tmp)
        print(result)
        return result

    def format_data(self, data: dict):
        result = []
        keys = data.keys()
        for key in keys:
            tmp = {
                'time': key,
                'data': data[key]
            }
            result.append(tmp)
        return result


class DeviceOnlineView(View):

    @method_decorator(csrf_exempt)
    def dispatch(self, request, *args, **kwargs):
        return super(DeviceOnlineView, self).dispatch(request, *args, **kwargs)

    def get(self, request, *args, **kwargs):
        request_dict = request.GET
        return self.validate(request_dict)

    def post(self, request, *args, **kwargs):
        request_dict = request.POST
        return self.validate(request_dict)

    def validate(self, request_dict):
        response = uidManageResponseObject()
        return response.json(0,{'online': random.randint((0.9 * ONLINE_DEVICE), ONLINE_DEVICE)})




