<?php

namespace Admin;

use Band;
use Input;
use Response;
use Sentry;
use Redirect;

class BandsController extends AdminController {

    public function index($data = array()) {
        
        //check if user can be here - only administrators can be here
        if (Sentry::getUser()->inGroup(Sentry::findGroupByName('Users'))){
            return Redirect::to('admin');
        }
        
        if (!isset($data['items'])) {
            $data['items'] = Band::orderByVotes()
                    ->getManaged();
        }

        return parent::index($data);
    }

    public function emailTest() {
        exit(__FILE__ . '--' . __LINE__);
    }

    
    public function indexOfType($type, $data = array()) {

        switch ($type) {

            case 'shortlist' :
                $data['items'] = Band::ofType('shortlist')
                        ->orderByVotes()
                        ->getManaged();
                break;

            case 'pending' :
                $data['items'] = Band::ofType('pending')
                        ->orderByVotes()
                        ->getManaged();
                break;

            case 'approved' :
                $data['items'] = Band::ofType('approved')
                        ->orderByVotes()
                        ->getManaged();
                break;

            case 'rejected' :
                $data['items'] = Band::ofType('rejected')
                        ->orderByVotes()
                        ->getManaged();
                break;

            case 'selected' :
                $data['items'] = Band::ofType('selected')
                        ->orderByVotes()
                        ->getManaged();
                break;
        }

        return parent::indexOfType($type, $data);
    }

    public function notify() {
        $status = Input::get('status');

        if ($status === 'shortlist' || $status === 'final') {
            $bands = Band::where($status, 1)->get();

            foreach ($bands as $band) {
                try {
                    $notification = $band->notify(Input::get('status'));

                    if ($notification) {
                        $column = "{$status}_notification";
                        $band->$column = 1;

                        $band->save();
                    }
                } catch (\Exception $e) {
                    return Response::json(array(
                                'success' => false,
                                'message' => $e->getMessage()
                    ));
                }
            }

            return Response::json(array(
                        'success' => true,
                        'prevent' => true
            ));
        }

        $band = Band::findOrFail(Input::get('band_id'));
        try {
            $band->notify($status, Input::get('reasons'));
        } catch (\Exception $e) {
            return Response::json(array(
                        'success' => false,
            ));
        }

        return Response::json(array(
                    'success' => true,
        ));
    }

    public function resendPassword() {


        $band = Band::findOrFail(Input::get('band_id'));

        $res = $band->resendAccess();


        return Response::json(array(
                    'success' => $res,
        ));
    }

}
