<?php

namespace App\Http\Controllers;

use App\Models\SfGuardUser;
use App\User;
use Auth;
use Carbon\Carbon;
use Config;
use Request;
use Input;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        if(!Auth::check()){
            return redirect('login');
        }
        return redirect('home');

    }

    //login
    public function show()
    {
        if(!Auth::check()){
            return redirect('login');
        }
        $data = [
            'user' => Auth::user(),
        ];
        return view('home', $data);
    }


    public function loadingModal(){

        $data = Input::get('data');
        $target = Input::get('target');

        $response = "";
        if($data == "data_protection"){
            $response = view('legal.data_protect_de')->render();
        }
        if($data == "imprint"){
            $response = view('legal.imprint_de')->render();
        }
        if(Request::ajax()) {
            return response()->json(['response' =>  $response, 'target'=>$target]);
        }
        abort(404);
    }

    public function checkLogin($identify, $token, $show = false)
    {
        if($identify){
            //user find by $identify
            $user = User::where('identify', '=', $identify)->first();
            if(!$user){
                return abort(404);
            }
            //user - check für from $sf_guard_user - old system
            $sf_guard_user = SfGuardUser::where('identify', '=', $identify)->first();
            if(!$sf_guard_user){
                return abort(404);
            }
            if($user->id != $sf_guard_user->user_id){
                return abort(404);

            }
            if($sf_guard_user->token != $token){
                return abort(404);
            }
            $time = Carbon::parse($sf_guard_user->token_at);
            $now = Carbon::now();
            $duration = $time->diffInSeconds($now);

            if($duration > 3){
                return abort(404);
            }
            $sf_guard_user->token = null;
            $sf_guard_user->token_at = null;
            $sf_guard_user->save();

            if(!Auth::check()){
                $user->last_login = now();
                $user->save();
                Auth::login($user);
            }
            if(Auth::check()){

                if(!$show){
                    return redirect(route('home'));
                }
                if($show == 'drafts'){
                    return redirect(route('drafts'));
                }
                if(strpos($show, 'drafts_edit_booking_') !== false){
                    $booking_id = str_replace('drafts_edit_booking_', '', $show);
                    return redirect(route('booking_detail', [$booking_id]));
                }
                if($show == 'requests'){
                    return redirect(route('requests'));
                }
                if($show == 'travel_agenda'){
                        return redirect(route('admin_settings_travel_program'));
                }
                if($show == 'travel_country'){
                    return redirect(route('admin_settings_travel_country'));
                }

            }


        }
        return abort(404);
    }

    public function legalDataProtected()
    {
        return view('legal.data_protected');
    }

    public function legalImprint()
    {
        return view('legal.imprint');
    }
}
