from django.shortcuts import render
from django.http import HttpResponse
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required

import math

from .models import Meal, DiaryDay, DiaryEntry, UserProfile

def index(request):
    return render(request, 'tracker/index.html')

def meals(request):
    return render(request, 'tracker/meals.html', {'meals': Meal.objects.all()})

def entries(request):
    return render(request, 'tracker/diary-entries.html', {'entries': DiaryEntry.objects.all()})

def day(request, pk):
    date = DiaryDay.objects.get(pk=pk)
    entries = date.diaryentry_set.all()
    totals = date.total()
    return render(request, 'tracker/day.html', {'entries': entries, 'totals': totals})

def days(request):
    return render(request, 'tracker/days.html', {'days': DiaryDay.objects.all()})


'''The user shouldn't need to create an account to calculate macros, so none of the calc-related views will be under the @login_required decorator.'''
def calc(request):
    return render(request, 'tracker/calc.html')

def calculate(request):
    data = request.POST
    name = data['name']
    meas_sys = data['measurement']
    weight_in = int(data['weight'])
    bfp = int(data['bfp'])#body fat percentage
    act_lvl = float(data['act_lvl'])
    goal = data['goal']

    if meas_sys == 'imperial':
        weight = round(weight_in * .45)
        meas_sys_bool = True
    else:
        weight = weight_in
        meas_sys_bool = False

    lbm = round(weight - (weight * (bfp / 100)))#lean body mass
    bmr = round(370 + 21.6 * lbm)#basal metabolic rate
    tdee = round(bmr * act_lvl)#total daily energy expenditure

    if goal == 'gain':
        adj = 1.2#adjustment
        fat_mult = .25#fat intake as a percentage of total calories
        goal_bool = False
    else:
        bfr = bfp // 10 #body fat range
        if bfr >= 3:
            adj = .7
        else:
            adj_list = [.85, .8, .75]
            adj = adj_list[bfr]
        fat_mult = 1.1#fat intake g / kg lbm
        goal_bool = True
    
    tadci = round(tdee * adj)#target average daily calorie intake
    tdci = round(tadci * 1.2)#training day calorie intake
    rdci = round(tadci * .8)#rest day calorie intake
    protein = round(lbm * 2.5)#protein g / day

    if goal == 'gain':
        train_fat = round(((fat_mult * tadci) / 9) * .7)
        rest_fat = round(((fat_mult * tadci) / 9) * 1.3)
    else:
        train_fat = round(fat_mult * lbm * .7)
        rest_fat = round(fat_mult * lbm * 1.3)
    
    train_carb = round((tdci - (protein * 4) - (train_fat * 9)) / 4)
    rest_carb = round((rdci - (protein * 4) - (rest_fat * 9)) / 4)

    new_user = UserProfile(name=name, meas_sys=meas_sys_bool, weight=weight_in, bfp=bfp, act_lvl=act_lvl, goal=goal_bool, lbm=lbm, bmr=bmr, protein=protein, train_kcal=tdci, rest_kcal=rdci, train_fat=train_fat, rest_fat=rest_fat, train_carb=train_carb, rest_carb=rest_carb)
    new_user.save()

    return render(request, 'tracker/macros.html', {'user': UserProfile.objects.get(pk=new_user.pk)})