from django.shortcuts import render, redirect
from django.http import Http404, HttpResponse
from django.core import serializers
from football import points, stats, schedule, predictions, convert
import nflgame # https://github.com/BurntSushi/nflgame/
from datetime import date
from collections import OrderedDict
from string import capwords
import operator

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

def search(request):
    return render(request, 'search.html')
    
def results(request):
    if 'name' not in request.GET or 'scoring' not in request.GET:
        return render(request, 'results.html', {'error':True, 'title':"Error"})
    name = capwords(request.GET['name'])
    scoring = request.GET['scoring']
    year = date.today().year
    week = schedule.current_week()
    weeks = []
    for x in range(1,week+1):
        weeks.append(x)
    position = stats.player_position(name)
    team = stats.player_team(name)
    if position == "QB" or position == "RB" or position == "WR" or position == "TE":
        functionname = scoring+"_player_points"
        getpoints = getattr(points, functionname)
        results = getpoints(name, year, weeks)
    elif position == "K":
        results = points.k_points(name, year, weeks)
    else:
        results = False
    if results == False:
        return render(request, 'results.html', {'error':True, 'title':"Error"})
    position = stats.player_position(name)
    qb = False
    flex = False
    k = False
    if position == "QB":
        qb = True
    elif position == "K":
        k = True
    elif position == "WR" or position == "RB" or position == "TE":
        flex = True
    else:
        return render(request, 'results.html', {'error':True, 'title':"Error"})
    bye_week = schedule.bye_week_player(name, year)
    total_points = points.total_points(results);
    graph_total_points = points.total_points_no_bye(results)
    ordered_total_points = OrderedDict(sorted(total_points.items()))
    graph_ordered_total_points = OrderedDict(sorted(graph_total_points.items()))
    total_stats = stats.total_stats(name, year, week)
    if position == "QB" or position == "RB" or position == "WR" or position == "TE":
        output_total_stats = convert.main_stats(total_stats)
        sorted_output_total_stats = OrderedDict(sorted(output_total_stats.items(), key=operator.itemgetter(1), reverse=True))
    elif position == "K":
        output_total_stats = convert.k_stats(total_stats)
        sorted_output_total_stats = OrderedDict(sorted(output_total_stats.items(), key=operator.itemgetter(1), reverse=True))
    else:
        return render(request, 'results.html', {'error':True, 'title':"Error"})
    average_points = predictions.average(name, scoring)
    prediction_stats = predictions.prediction(name, scoring)
    if prediction_stats == "Bye Week":
        prediction_points = "Bye Week"
        sorted_output_prediction_stats = {'Bye Week': "Bye Week"}
    else:
        if position == "QB":
            prediction_points = prediction_stats['points']
            output_prediction_stats = convert.qb_prediction(prediction_stats)
            sorted_output_prediction_stats = OrderedDict(sorted(output_prediction_stats.items(), key=operator.itemgetter(1), reverse=True))
        elif position == "RB":
            prediction_points = prediction_stats['points']
            output_prediction_stats = convert.rb_prediction(prediction_stats)
            sorted_output_prediction_stats = OrderedDict(sorted(output_prediction_stats.items(), key=operator.itemgetter(1), reverse=True))
        elif position == "WR" or position == "TE":
            prediction_points = prediction_stats['points']
            output_prediction_stats = convert.rec_prediction(prediction_stats)
            sorted_output_prediction_stats = OrderedDict(sorted(output_prediction_stats.items(), key=operator.itemgetter(1), reverse=True))
        elif position == "K":
            prediction_points = prediction_stats
            sorted_output_prediction_stats = {'Points':prediction_points}
        else:
            return render(request, 'results.html', {'error':True, 'title':"Error"})
    return render(request, 'results.html', {'title':name, 'name':name, 'total_stats':sorted_output_total_stats, 'results':results, 'scoring':scoring, 'graph_ordered_total_points':graph_ordered_total_points, 'bye_week':bye_week, 'position':position, 'team':team, 'qb':qb, 'flex':flex, 'k':k, 'average':average_points, 'predictions':sorted_output_prediction_stats, 'prediction':prediction_points})

def scoreboard(request):
    current_week = schedule.current_week()
    year = date.today().year
    games = nflgame.games(year, week=current_week)
    scores = []
    for x in games:
        scores.append(x)
    weeks = []
    for x in range(0,current_week):
        weeks.append(x+1)
    return render(request, 'scoreboard.html', {'scores':scores,'week':current_week, 'past_weeks':weeks, 'current_week':True})

def previous_scoreboard(request, offset):
    try:
        wk = int(offset)
    except ValueError: