<?php

namespace App\Http\Controllers;

use App\Http\Requests;
use App\Metier\GestSession;
use App\Models\Etudiant;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Mail;

class InscriptionController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

    protected $gestSession;

    public function __construct(GestSession $gestSession)
    {
        $this->gestSession = $gestSession;
    }

    public function index($sessionId)
    {
        return view('inscriptionView')
            ->with('sessionId', $sessionId);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request, $sessionId, $etudiantId, $examenId)
    {
        $this->gestSession->inscrireEtudiantExamen($examenId, $etudiantId);

        return redirect('/session/' . $sessionId . '/inscription/' . $etudiantId)
            ->withOk("Vous étes inscrit");
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($sessionId, $etudiantId)
    {
        //Recupération ensemble des examens dans cette session
        $examens = $this->gestSession->getExamens($sessionId);
        //Verification de validation de stage
        $validation = $this->gestSession->getValidationEtudiant($sessionId, $etudiantId); //tab contient le méme nombre de case de l'examen mais vrai ou faux pour indiquer la possiblité de s'inscrire

        return view('choixExamenEtudiant')
            ->with('examens', $examens)
            ->with('validation', $validation)
            ->with('sessionId', $sessionId)
            ->with('etudiantId', $etudiantId);
    }


    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update($sessionId = null, $etudiantId = null, $confirmation_code = null)
    {
        if ($etudiantId && $confirmation_code) {
            $etudiant = Etudiant::find($etudiantId);
            if ($etudiant)
                if ($confirmation_code == $etudiant->confirmation_code) {
                    $etudiant->active = true;
                    $etudiant->save();
                    return redirect('/session/' . $sessionId . '/inscription/' . $etudiantId);
                }
        }
        return redirect('/session/' . $sessionId . '/inscription/')
            ->withError('invalid confirmation infos');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    public function verif(Request $request, $sessionId)
    {
        $cin = Input::get('cin');
        $email = Input::get('email');
        $id_Etudiant = $this->gestSession->verifEtudiant($cin, $email);
        if (!$id_Etudiant) {
            //Caractéristique fausse ;
            return redirect('session/' . $sessionId . '/inscription')->withError("Verifier votre CIN");
        } else {
            $verif_niveau = $this->gestSession->verifNiveauEtudiant($id_Etudiant, $sessionId);
            //echo $verif_niveau;
            if (!$verif_niveau) {
                return redirect('session/' . $sessionId . '/inscription')->withError("Vous n'avez pas le niveau requis");
            } else {
                $etudiant = Etudiant::find($id_Etudiant);
                if ($etudiant->active == 0) {
                    $confirmation_code = str_random(80);
                    $link = $request->root() . "/email/confirmation/" . $sessionId . "/"
                        . $id_Etudiant . "/" . $confirmation_code;
                    $etudiant->confirmation_code = $confirmation_code;
                    $etudiant->email = $email;
                    $etudiant->save();
                    Mail::send('validationEmail', ['nom' => $etudiant->nom,
                        'prenom' => $etudiant->prenom, 'CIN' => $etudiant->CIN,
                        'carte_Etudiant' => $etudiant->carte_Etudiant, 'link' => $link], function ($message) use ($email) {
                        $message->to($email)->subject('Verification');
                    });
                    return redirect('session/' . $sessionId . '/inscription')->withOk("Vous recevrez un mail de confirmation sur votre email !");
                } else {
                    return redirect('/session/' . $sessionId . '/inscription/' . $id_Etudiant);
                }
            }
        }
    }
}
