<?php

/*
  |--------------------------------------------------------------------------
  | Note: different editions are handled with an App::before filter
  | Check filters.php for more information
  |--------------------------------------------------------------------------
  |
 */

/*
  |--------------------------------------------------------------------------
  | Admin Routes
  |--------------------------------------------------------------------------
  |
 */

Route::filter('admin_extra', function() {
    Asset::enqueue('admin-extra.js');
    Asset::enqueue('admin-extra.css');
    Asset::json('admin', array(
        'urls' => array(
            'post_notify' => route('admin.band.post-notify'),
            'post_resend' => route('admin.band.post-resend'),
        ),
    ));

    Config::set('view.pagination', 'pagination::slider-3');

    View::share('admin_title', 'EDP Tanto Fado');
});

Route::filter('admin_home', function ($route, $request) {
    $user = Sentry::getUser();
    $group = Sentry::findGroupByName('Users');

    if ($user->inGroup($group)) {

        if (Carbon\Carbon::parse(Contest::getRatingsDeadline())->isPast()) {
            return Redirect::to(route('home'));
        }

        return Redirect::to(route('admin.jury-vote.jury-votes'));
    }
});

Route::filter('jury_filter', function ($route, $request) {
    $user = Sentry::getUser();
    $group = Sentry::findGroupByName('Users');

    if (!$user->inGroup($group)) {
        return Redirect::to(route('admin.home'));
    }

    if (Carbon\Carbon::parse(Contest::getRatingsDeadline())->isPast()) {
        return Redirect::to(route('home'));
    }
});



//Admin frontend participation
Route::group(
        array(
    'before' => 'admin_auth|admin_user'
        ), function() {
    Route::get('admininscricao', array(
        'as' => 'adminParticipate',
        'uses' => 'FrontEndController@adminParticipate',
            )
    );
}
);





Route::group(
        array(
    'prefix' => 'admin',
    'namespace' => 'Admin',
    'before' => 'admin_auth|admin_user|admin_assets|admin_extra'
        ), function() {
    edpAdminRoutes();
}
);

function edpAdminRoutes() {

    Route::get('/', array(
        'before' => 'admin_home',
        'as' => 'admin.home', function() {

            if (Contest::status() === 'semi-final') {
                return Redirect::to(route('admin.band.index-of-type', 'shortlist'));
            } else {

                return Redirect::to(route('admin.band.index-of-type', 'pending'));
            }
        }));

    Route::get('/debug', array(
        'as' => 'admin.debug', function() {
            dd(trans('emails.rejected.subject', array('title' => Contest::title()), 'messages', 'es'));
        }));


    //Email testing route
    Route::get('/testmail', array(
        'as' => 'admin.testmail', function() {
            return View::make('admin.bands.emailtest');
        }));

    Route::post('/testmail', array(
        'as' => 'admin.testmail', function() {

            $email = Input::get('email');
            Mail::send("emails.test", [], function($message) use ($email) {
                        $message
                                ->replyTo(Contest::email())
                                ->from(Contest::emailFrom(), 'EDP Tanto Fado')
                                ->to($email, $email)
                                ->subject(Contest::name() . ' - Email Testing');
                    });
            View::share('msg', 'Mensagem enviada');
            return View::make('admin.bands.emailtest');
        }));
    //END Email testing route



    Route::get('jury', array(
        'as' => 'admin.jury-vote.jury',
        'uses' => 'JuryVotesController@juryIndex'
    ));

    Route::get('juror-bands-ratings/{jurorId}', array(
        'as' => 'admin.jury-vote.juror-bands-ratings',
        'uses' => 'JuryVotesController@jurorBandsRatings'
    ));

    Route::get('jury-votes', array(
        'before' => ['jury_filter'],
        'as' => 'admin.jury-vote.jury-votes',
        'uses' => 'JuryVotesController@juryVoting'
    ));

    Route::post('jury-votes/{bandId}', array(
        'as' => 'admin.vote.post-jury-votes',
        'uses' => 'JuryVotesController@postJuryVoting'
    ));

    Route::post('post-notify', array(
        'as' => 'admin.band.post-notify',
        'before' => 'csrf',
        'uses' => 'BandsController@notify',
    ));

    Route::post('post-resend', array(
        'as' => 'admin.band.post-resend',
        'before' => 'csrf',
        'uses' => 'BandsController@resendPassword',
    ));

    Route::get('band/status/{status}', array(
        'as' => 'admin.band.index-of-type',
        'uses' => 'BandsController@indexOfType',
    ));
    Route::resource('band', 'BandsController');

    Route::get('voter/origin/{origin}', array(
        'as' => 'admin.voter.index-of-type',
        'uses' => 'VotersController@indexOfType',
    ));



    Route::resource('voter', 'VotersController');

    Route::resource('vote', 'VotesController');

    Route::resource('jury_vote', 'JuryVotesController');

    Route::resource('contest_option', 'ContestOptionsController');
}

/*
  |--------------------------------------------------------------------------
  | API routes
  |--------------------------------------------------------------------------
  |
 */

Route::group([
    'prefix' => 'api',
    'before' => [
        'http_basic',
        'api_auth',
        'no.session.cookie'
    ]
], function() {
    edpAPIRoutes();
});

function edpAPIRoutes() {
    // Route::post('register-teaser', [
    //     'as' => 'register-teaser',
    //     'uses' => 'APIController@teaserRegister'
    // ]);

    Route::get('contest-details', [
        'as' => 'contest-details',
        'uses' => 'APIController@contestDetails'
    ]);

    Route::post('voter-register', [
        'as' => 'voter-register',
        'uses' => 'APIController@voterRegister'
    ]);

    // Route::post('voter-login', [
    //     'as' => 'voter-login',
    //     'uses' => 'APIController@voterLogin'
    // ]);

    Route::post('voter-facebook-login', [
        'as' => 'voter-facebook-login',
        'uses' => 'APIController@voterFacebookLogin'
    ]);

    Route::post('vote/{bandSlug}', [
        'as' => 'vote',
        'before' => 'voter_auth',
        'uses' => 'APIController@vote'
    ]);

    Route::get('get-votes', [
        'as' => 'vote',
        'before' => 'voter_auth',
        'uses' => 'APIController@getVotesByVoter'
    ]);

    // Route::post('band-login', [
    //     'as' => 'band-login',
    //     'uses' => 'APIController@bandLogin'
    // ]);

    Route::post('register-band', [
        'as' => 'register-band',
        'uses' => 'APIController@registerBand'
    ]);

    Route::get('list-bands', [
        'as' => 'list-band',
        'uses' => 'APIController@listBands'
    ]);

    // Route::get('context-list-bands', [
    //     'as' => 'context-list-bands',
    //     'uses' => 'APIController@contextListBands'
    // ]);

    // Route::get('top-bands', [
    //     'as' => 'top-bands',
    //     'uses' => 'APIController@topBands'
    // ]);

    Route::get('show-band/{id}', [
        'as' => 'show-band',
        'uses' => 'APIController@showBand'
    ]);

    // Route::post('edit-band/{id}', [
    //     'as' => 'edit-band',
    //     'before' => 'jwt-band-auth',
    //     'uses' => 'APIController@editBand'
    // ]);

    // Route::post('generate-promotional-material/{band_id}', [
    //     'as' => 'blitz',
    //     'uses' => 'APIController@generatePromotionalMaterial'
    // ]);
}

// function edpFrontEndRoutes() {

//     Route::get('/', array(
//         'as' => 'home',
//         'uses' => 'FrontEndController@home',
//             )
//     );




//     Route::get('concurso', array(
//         'as' => 'contest',
//         'uses' => 'FrontEndController@contest',
//             )
//     );

//     Route::get('edicoes-anteriores/{year}', array(
//         'as' => 'past-editions',
//         'uses' => 'FrontEndController@pastEditions',
//             )
//     );

//     Route::get('inscricao', array(
//         'as' => 'participate',
//         'uses' => 'FrontEndController@participate',
//             )
//     );


//     Route::get('votacoes', array(
//         'as' => 'vote',
//         'uses' => 'FrontEndController@vote',
//             )
//     );

//     Route::get('banda/{slug}/{token?}', array(
//         'as' => 'band',
//         'uses' => 'FrontEndController@band',
//             )
//     );

//     Route::get('faqs', array(
//         'as' => 'faqs',
//         'uses' => 'FrontEndController@faqs',
//             )
//     );

//     Route::get('area-reservada', array(
//         'as' => 'reserved',
//         'uses' => 'FrontEndController@reserved',
//             )
//     );

//     /*
//       |--------------------------------------------------------------------------
//       | Back-end routes
//       |--------------------------------------------------------------------------
//       |
//      */

//     Route::post('post-participate', array(
//         'as' => 'post-participate',
//         'uses' => 'BackEndController@participate',
//             )
//     );

//     Route::post('post-register-to-vote', array(
//         'as' => 'post-register-to-vote',
//         'uses' => 'BackEndController@registerToVote',
//             )
//     );

//     Route::post('post-vote', array(
//         'as' => 'post-vote',
//         'uses' => 'BackEndController@vote',
//             )
//     );

//     Route::post('band-login', array(
//         'as' => 'band-login',
//         'uses' => 'BackEndController@bandLogin',
//             )
//     );

//     Route::any('band-logout', array(
//         'as' => 'band-logout',
//         'uses' => 'BackEndController@bandLogout',
//             )
//     );

//     Route::post('blitz', array(
//         'as' => 'blitz',
//         'uses' => 'BlitzController@generate',
//             )
//     );
// }

/*
  |--------------------------------------------------------------------------
  | Social auth routes
  |--------------------------------------------------------------------------
  |
 */

Route::group(array(
    'prefix' => 'oauth',
        ), function() {
    Route::get('authorize/{provider}', array(
        'as' => 'oauth.authorize',
        'uses' => 'OAuthController@authorize',
    ));

    Route::get('login/{provider}', array(
        'as' => 'oauth.login',
        'uses' => 'OAuthController@login',
    ));
});
