<?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Models\Guardian;
use App\Repositories\GuardianRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class GuardiansController extends Controller {

    private $guardianRepository;

    public function __construct(GuardianRepository $guardianRepository)
    {
        $this->guardianRepository=$guardianRepository;
    }

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        $guardians=$this->guardianRepository->all();
        return view('guardians.index' , compact('guardians'));

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        $guardian = new Guardian();
        return view('guardians.create', compact('guardian'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store(Requests\CreateGuardianRequest $request)
    {
        $properties=$request->all();
        $properties['password'] = Hash::make($request->get('password'));
        $guardian = $this->guardianRepository->create($properties);
        if(!$guardian)
            return "404";
        return redirect(route('guardian.index'));
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        $guardian = $this->guardianRepository->find($id);
        $logs=$guardian->logs();
        return view('guardians.show' , compact('guardian','logs'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        $guardian = $this->guardianRepository->find($id);
        return view('guardians.edit' , compact('guardian'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update(Requests\EditGuardianRequest $request)
    {
        $properties=$request->all();
        $properties['password'] = Hash::make($request->get('password'));
        unset($properties['password_confirmation']);
        unset($properties['_method']);
        unset($properties['_token']);
        $guardian = $this->guardianRepository->update($properties,$request->get('id'));
        if  (!$guardian)
            return abort(403, 'Unauthorized action.');

        return redirect(route('guardian.index'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        $this->guardianRepository->delete($id);
        return redirect(route('guardian.index'));
    }
}
