<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the Closure to execute when that URI is requested.
|
*/

Route::get('/', function()
{
	$gesehen = Film::zuletztGesehen()->get();
	$vorgeschlagen = Film::neuesteVorschlage()->get();
	return View::make('index')
		->with('gesehen', $gesehen)
		->with('vorgeschlagen', $vorgeschlagen);
});

Route::get('film/{id}', array('as' => 'film', function($id) {
	$film = Film::findOrFail($id);

	$tmdb = new TMDb('b187f8d9c5e72b1faecb741d5d04239a', 'de', TRUE);
	$tmovie = $tmdb->getMovie($film->tvdbid);
	$tcast = $tmdb->getMovieCast($film->tvdbid);
	$ttrail = $tmdb->getMovieTrailers($film->tvdbid);
	$image = $tmdb->getImageUrl($tmovie['poster_path'], TMDb::IMAGE_POSTER, 'w342');

	$votes = $film->votes()->count();
	$vposi = $film->votes()->where('stimme', true)->count();

	$comments = $film->comments()->orderBy('id', 'DESC')->get();

	return View::make('film')
		->with('film', $film)
		->with('tfilm', $tmovie)
		->with('poster', $image)
		->with('comments', $comments)
		->with('cast', $tcast)
		->with('trail', $ttrail)
		->with('votes', $votes)
		->with('vposi', $vposi)
		->with('tmdb', $tmdb);
}));

Route::get('login', array('as' => 'login', function() {
	return View::make('login');
}));

Route::post('login', function() {
	$userdata = array(
		'name' => Input::get('iSpieler'),
		'password' => Input::get('iPassword'));
	if(Auth::attempt($userdata)) {
		return Redirect::intended('/');
	} else {
		echo "Login gescheitert.";
		var_dump($userdata);
		return Redirect::to('login')
			->with('login_errors', true);
	}
});

Route::get('logout', array('as' => 'logout', function() {
	Auth::logout();
	return Redirect::to('/');
}));