""" Handles all AJAX requests """

from datetime import datetime, timedelta
import json
import pytz
import requests

from decouple import config
from django.core.cache import cache
from django.contrib.auth.decorators import login_required
from django.http import JsonResponse

from functions import get_start_and_end_date, what_is_the_weather, get_averages


def json_icon(request):
    """ Returns the relevant weather icon """
    icon = what_is_the_weather(config("SITE_ID"), config("API_KEY"), config("LAT"), config("LNG"))
    return JsonResponse({'icon': icon})


@login_required
def json_power_day(request):
    """ returns a json object with the power data of the last 24 hours """
    date_range = int(request.GET.get('range'))
    content = cache.get(f'power_day_{date_range}')
    if content is not None:
        return JsonResponse(content)
    start_time = (datetime.now(pytz.timezone(config("TIME_ZONE"))) - timedelta(days=date_range)).strftime('%Y-%m-%d%%20%H:%M:%S')
    end_time = datetime.now(pytz.timezone(config("TIME_ZONE"))).strftime('%Y-%m-%d%%20%H:%M:%S')
    time = f'startTime={start_time}&endTime={end_time}'
    url = f'https://monitoringapi.solaredge.com/site/{config("SITE_ID")}/power?{time}&api_key={config("API_KEY")}'
    response = requests.get(url)
    if response.status_code == 200:
        data = json.loads(response.content)['power']['values']
        power_list = []
        for datum in data:
            date = datetime.strptime(datum['date'], '%Y-%m-%d %H:%M:%S')
            timestamp = int(date.strftime('%s')) * 1000
            power = round(datum['value'], 1) if datum['value'] is not None else 0.0
            power_list.append([timestamp, power])
        content = {
            'data': {
                'raw': {
                    'name': 'Power',
                    'data': power_list
                },
                'small_avg': {
                    'name': 'Hourly average',
                    'data': get_averages(power_list, 5)
                },
                'large_avg': {
                    'name': 'Three hour average',
                    'data': get_averages(power_list, 13)
                }
            },
            'success': True
        }
        cache.set(f'power_day_{date_range}', content, 5 * 60)
        return JsonResponse(content)
    return JsonResponse({'power': [], 'success': False})


@login_required
def json_energy_year_view(request):
    """ Returns a json object with all the energy data """
    content = cache.get('energy_year')
    if content is not None:
        return JsonResponse(content)
    start_date, end_date = get_start_and_end_date(config("SITE_ID"), config("API_KEY"))
    time = f'startDate={start_date}&endDate={end_date}'
    url = f'https://monitoringapi.solaredge.com/site/{config("SITE_ID")}/energy?timeUnit=DAY&{time}&api_key={config("API_KEY")}'
    response = requests.get(url)
    if response.status_code == 200:
        data = json.loads(response.content)['energy']['values']
        energy_list = []
        for datum in data:
            date = datetime.strptime(datum['date'], '%Y-%m-%d %H:%M:%S').date()
            timestamp = int(date.strftime('%s')) * 1000
            energy = round(datum['value'] / 1000, 1) if datum['value'] is not None else 0.0
            energy_list.append([timestamp, energy])
        content = {
            'data': {
                'raw': {
                    'name': 'Daily generation',
                    'data': energy_list
                },
                'small_avg': {
                    'name': 'Weekly average',
                    'data': get_averages(energy_list, 7)
                },
                'large_avg': {
                    'name': 'Monthly average',
                    'data': get_averages(energy_list, 30)
                }
            },
            'success': True
        }
        cache.set('energy_year', content, 5 * 60)
        return JsonResponse(content)
    return JsonResponse({'energy': [], 'success': False})
