<?php namespace App\Http\Controllers;

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

use App\Models\Employee;
use App\Models\Log;
use App\Repositories\EmployeeRepository;
use App\Repositories\GuardianRepository;
use App\Repositories\LogRepository;
use App\Repositories\VisitorRepository;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Response;
use App\Util\CryptUtil;
use Illuminate\Support\Str;

class GuardServiceController extends Controller {

    private $guardianRepository;
    private $employeeRepository;
    private $visitorRepository;
    private $logRepository;

    public function __construct(GuardianRepository $guardianRepository,EmployeeRepository $employeeRepository,VisitorRepository $visitorRepository,LogRepository $logRepository)
    {
        $this->guardianRepository=$guardianRepository;
        $this->employeeRepository=$employeeRepository;
        $this->visitorRepository=$visitorRepository;
        $this->logRepository=$logRepository;
    }

    public function authentify(Request $request)
    {
        $success = 0;
        $error = 1;
        $cin = $request->get('cin');
        $password = $request->get('password');
        $guardian =$this->guardianRepository->findBy('cin',$cin);
        if (!Hash::check($password, $guardian->password))
        {
            $success = 1;
            $error = 0;
            $id=$guardian->id;
            $token['api_token'] = hash('sha256',Str::random(10),false);
            $token['expires_on'] = Carbon::now()->addMonth()->toDateTimeString();
            if($this->guardianRepository->update($token,$id)) {
                $api_token = $token['api_token'];
            }
        }
        return Response::json(compact('success','error','id','api_token'));
    }

    public function renderLogs(Request $request)
    {
        $success = 0;
        $error = 1;
        $id=$request->get('id');
        $logs = $this->guardianRepository->find($id)->logs()->get();
        if($logs){
            $success = 1;
            $error = 0;
        }
        return Response::json(compact('success','error','logs'));
    }


    public function identify(Request $request)
    {
        $success = 0;
        $error = 1;
        $cin=null;
        if($request->exists('key')){
            $cin=CryptUtil::isValideKey($request->get('key'));
            if($cin) {
                $employee = $this->employeeRepository->findBy('cin', $cin);
                $success = 1;
                $error = 0;
            }
        }else {
            $cin = $request->get('cin');
            $password = $request->get('password');
            $employee = $this->employeeRepository->findBy('cin', $cin);
            if (Hash::check($password, $employee->password)) {
                $success = 1;
                $error = 0;
            }else{
                $employee=null;
            }
        }
        return Response::json(compact('success','error','employee'));
    }

    public function logger(Requests\loggerRequest $request)
    {
        $success = 0;
        $error = 1;
        $log=new Log();
        $log->employee_id=$request->get('employee_id');
        $log->guardian_id=$request->get('guardian_id');
        $log->in_out=$request->get('in_out');
        $log->date_log=new \DateTime();
        if($log->save()){
            $success = 1;
            $error = 0;
        }
        return Response::json(compact('success','error'));
    }

    public function renderVisitors(Request $request)
    {
        $success = 0;
        $error = 1;
        $today=new \DateTime();
        $visitors = $this->visitorRepository->all();
        $todayVisitors = [];
        $i=0;
        foreach($visitors as $visitor){
            $date_visit=new \DateTime($visitor->date_visit);
            $interval = date_diff($date_visit, $today);
            if ($interval->days==0){
                $todayVisitors[$i]=$visitor;
                $i=$i+1;
            }
    }
        if($todayVisitors){
            $success = 1;
            $error = 0;
        }
        return Response::json(compact('success','error','todayVisitors'));
    }

    public function renderProfiles(){
        $success = 0;
        $error = 1;
        $employees = Employee::WhereNull('date_resignation')->get();
        if($employees){
            $success = 1;
            $error = 0;
        }
        return Response::json(compact('success','error','employees'));
    }
    public function saveLog(Requests\saveLogRequest $request){
        $success = 0;
        $error = 1;
        $log = $this->logRepository->create($request->all());
        if($log)
        {
            $success = 1;
             $error = 0;
        }
        return Response::json(compact('success','error'));
    }

}
