from django.shortcuts import render
from tutorials.models import Game
import requests
from bs4 import BeautifulSoup
from django.http import HttpResponse
from django.core.files.base import ContentFile
from io import StringIO
from datetime import datetime

def home(request):
	return render(request, 'base.html')

def games(request):
	games = Game.objects.all()
	context = {'games': games}
	return render(request, 'games.html', context)

def detail(request, game_id):
	game = Game.objects.get(pk=game_id)
	context = {'game': game}
	return render(request, 'game.html', context)

def get_game_image(game_url):
	r = requests.get('http://en.wikipedia.org' + game_url)
	raw = r.content
	soup = BeautifulSoup(str(raw))
	image = soup.find_all('img')[0]
	src = image['src']
	if 'CentralAutoLogin' in src:
		# don't create image - it is a special Wikipedia image
		return None
	image_data = requests.get('http:' + src, stream=True)
	binary_image_data = image_data.content
	image_filename = src.split('/')[-1]
	return image_filename, binary_image_data

def get_games(request):
	WIKIPEDIA_URL = 'http://en.wikipedia.org/wiki/List_of_Xbox_One_games'
	r = requests.get(WIKIPEDIA_URL)
	raw = r.content
	soup = BeautifulSoup(str(raw))
	table = soup.find_all('table', attrs={'class', 'wikitable'})

	# there should be only one table on the page
	for item in table:
		t = item
		rows = t.find_all('tr')
		# TODO - FINISH THIS - CURRENTLY JUST GETTING ROW 3 (THIRD GAME IN THE LIST)
		for i, row in enumerate(rows):
			if i > 0:
				name = row.find_all('a')[0].text
				game_url = row.find_all('a')[0]['href']
				if game_url.startswith('/wiki'):
					game_image_and_filename = get_game_image(game_url)
					if game_image_and_filename:
						image_filename, game_image = game_image_and_filename
					else:
						image_filename, game_image = None, None
				try:
					release_date = row.find_all('td')[7]
				except IndexError:
					import pdb;pdb.set_trace()
				try:
					publisher = row.find_all('td')[3].find_all('a')[0].text
				except IndexError:
					publsher = None
				unreleased = False
				try:
					release_date = release_date.find_all('span')[1].text
					# Convert release date to a Python datetime
					try:
						release_date = datetime.strptime(release_date, "%B %d, %Y")
					except ValueError:
						# Guess we don't have the day (just month and year e.g. December 2014)
						# don't save in the database...
						continue
				except IndexError:
					# The game must not have a release date yet (either TBA or e.g. 2015)
					unreleased = True
					release_date = release_date.text
				# Only try to create the game in the database if it has been released
				if not unreleased:
					if game_image and image_filename:
						image_field = ContentFile(game_image, image_filename)
						game, created = Game.objects.get_or_create(name=name, release_date=release_date, 
							publisher=publisher, front_cover=image_field)
					else:
						game, created = Game.objects.get_or_create(name=name, release_date=release_date,
							publisher=publisher)
	return HttpResponse('OK')

