<?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 controller to call when that URI is requested.
|
*/

Route::get('/', ['as' => 'home', function () {
    Session::reflash();
    // Dummy query to calculate rows
    $video = \App\Models\Video::getRandom()->first();

    return redirect($video->id);

}])/*->middleware('auth')*/;

Route::get('nojs', function() { return view('nojs'); });

Route::post('filter', 'UserController@filter');

// /api
Route::group(['prefix' => 'api'], function() {

    // /api/messages
    Route::group(['prefix' => 'messages'], function() {
        Route::get('', 'MessageController@index');
        Route::post('read', 'MessageController@read');
        Route::get('readall', 'MessageController@readall');
    });

    // /api/comments
    Route::group(['prefix' => 'comments'], function() {
        Route::get('/', 'CommentController@index');
        Route::get('/{id}', 'CommentController@show')->where('id', '[0-9]+');
        Route::post('{id}/edit', 'CommentController@update')->where('id', '[0-9]+');
        Route::post('{id}/delete', 'CommentController@destroy')->where('id', '[0-9]+');
        Route::post('{id}/restore', 'CommentController@restore')->where('id', '[0-9]+');
    });

    // /api/user
    Route::group(['prefix' => 'user'], function() {
        Route::post('{username}/ban', 'UserController@ban');
        Route::get('/layout', 'UserController@setLayout');
    });

    // /api/video
    Route::group(['prefix' => 'video'], function() {
        Route::get('random', function() {
            return \App\Models\Video::getRandom()->with(['category', 'user' => function($query) {
                $query->addSelect('username', 'id');
            }])->first();
        });




	Route::get('latest', function(\Illuminate\Http\Request $req) {
            if ($req->has('filtered') && $req->get('filtered')) {
                return \App\Models\Video::filtered()->orderBy('id', 'DESC')->first();
            }
            return \App\Models\Video::orderBy('id', 'DESC')->first();
	});

        Route::get('{id}', function($id) {
            $res = \App\Models\Video::with(['category', 'user' => function($query) {
                $query->addSelect('username', 'id');
            }])->find($id);
            if(!$res) {
                return response(['message' => 'Video not found'], 404);
            }
            return $res;
        })->where('id', '[0-9]+')->middleware('auth');
        Route::post('{id}/delete', 'VideoController@destroy')->where('id', '[0-9]+');
        Route::post('{id}/tag', 'VideoController@tag')->where('id', '[0-9]+');
        Route::post('{id}/untag', 'VideoController@untag')->where('id', '[0-9]+');
        Route::post('upload', 'VideoController@store')->middleware('auth.basic');
    });

    Route::post('upload', 'VideoController@store');

});



Route::group(["middleware" => "theme"], function() {
	Route::get('uim', 'uimgController@index');
	Route::post('uim/add', 'UserController@speichern');
	Route::post('report/{id}', 'ReportController@report')->middleware('auth');  // added by klee
	Route::get('messages', 'MessageController@page')->middleware('auth');
	Route::get('user/{username}', 'UserController@show')->middleware('auth');
	Route::get('user/{username}/uploads', 'UserController@random')->middleware('auth');
	Route::get('user/{username}/uploads/{id}', 'UserController@play')->where('id', '[0-9]+');
	Route::get('user/{username}/favs', 'UserController@random_fav')->middleware('auth');
	Route::get('user/{username}/favs/{id}', 'UserController@play_fav')->where('id', '[0-9]+');
	Route::get('user/{username}/favs/index', 'UserController@show_favs')->middleware('auth');
	Route::get('user/{username}/comments', 'UserController@show_comments')->middleware('auth');
	Route::get('logout', 'UserController@logout');
	Route::post('login', 'UserController@login');
	Route::get('register', 'UserController@create');
	//Route::post('register', 'UserController@store');
	Route::get('activate/{token}', 'UserController@activate');
	Route::get('index', 'VideoController@index')->middleware('auth');
	Route::get('main', 'VideoController@main')->middleware('auth');
	Route::post('index/{id}', 'VideoController@update')->middleware('auth');
	Route::get('upload', 'VideoController@create')->middleware('auth');
	Route::get('categories', 'CategoryController@index')->middleware('auth');
	Route::get('webm', function() { return view('webm'); })->middleware('auth');
	Route::get('about', function() { return view('about'); })->middleware('auth');
	Route::get('irc', function() { return view('irc'); });
	Route::get('rules', 'rulezController@index')->middleware('auth');
	#Route::get('rulez', 'rulezController@index')->middleware('auth');
	Route::get('todo', function() { return view('todo'); })->middleware('auth');
	Route::get('contact', function() { return view('contact'); });
	Route::get('terms', function() { return view('tos'); })->middleware('auth');
	Route::get('privacy', function() { return view('privacy'); })->middleware('auth');
	Route::get('stats', function() { 
	    return view('stats', [
		'user_count' => \App\Models\User::count(),
		'upload_count' => \App\Models\Video::count(),
		'comment_count' => \App\Models\Comment::count(),
		//'fav_count' => \App\Models\UserFavorite::count(),
		'latest_video' => \App\Models\Video::getLastId(),
		'newest_user' => \App\Models\User::orderBy('id', 'DESC')->first()->username,
		'dirsize' => shell_exec("(du -sh " . public_path() . "/b | cut -f1)")
	]);
	})->middleware('auth');

	Route::get('/latest', function () {
	    Session::reflash();

	    $video = \App\Models\Video::orderBy('id', 'DESC')->first();

	    return redirect($video->id);
	});
	#Route::get('help', function() { return view('help'); });
	#Route::get('announcement', function() { return view('announcement'); });
	#Route::get('map', function() { return view('map'); });
	#Route::get('donate', function() {
	#    return view('donate', [
	#        'donations' => \App\Models\Donation::orderBy('timestamp', 'DESC')->get()
	#    ]);
	#});
	#Route::get('login', function() { return view('login'); });
	Route::get('login', 'UserController@loggain');
	#Route::get('counter-strike', function() { return view('counter-strike'); });

	Route::get('{id}', 'VideoController@show')->where('id', '[0-9]+');
	Route::get('{id}/fav', 'VideoController@favorite')->where('id', '[0-9]+');
	Route::post('{id}', 'CommentController@store')->where('id', '[0-9]+');

	Route::get('{shortname}', 'CategoryController@showVideo')->where('shortname', '[a-z][a-z0-9]+')->middleware('auth');
	Route::get('{shortname}/{id}', 'CategoryController@showVideo')->where(['shortname' => '[a-z][a-z0-9]+', 'id' => '[0-9]+'])->middleware('auth');
});
