<?php

namespace App\Http\Controllers;

use App\Models\ShoppingPayment;
use App\User;
use Auth;
use Carbon\Carbon;
use Config;
use Request;
use Util;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
       // $this->middleware('guest');
    }

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

    }

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

    public function loadingModal(){

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

        $response = "";
        if($data === "data_protection"){
            $data = [
                'modal' => true,
                'user_shop' => Util::getUserShop(),
            ];
            $response = view('legal.data_protect_de', $data)->render();
        }
        if($data === "imprint"){
            $data = [
                'modal' => true,
                'user_shop' => Util::getUserShop(),
            ];
            $response = view('legal.imprint_de', $data)->render();
        }
        if($data === "shop_term_of_use"){
            $data = [
                'modal' => true,
                'user_shop' => Util::getUserShop(),
            ];
            $response = view('legal.shop_term_of_use_de', $data)->render();
        }
        if($data === "agb"){
            $data = [
                'modal' => true,
                'user_shop' => Util::getUserShop(),
            ];
            $response = view('legal.agb_de', $data)->render();
        }
        if(Request::ajax()) {
            return response()->json(['response' =>  $response, 'target'=>$target]);
        }
        abort(404);
    }


    public function verify($confirmation_code){
        if( ! $confirmation_code)
        {
            return redirect('/status/error');
        }

        $user = User::whereConfirmationCode($confirmation_code)->first();

        if ( ! $user)
        {
            return redirect('/status/not/found');
        }
        if($user->confirmed === 0){
            $user->confirmed = 1;
            //kill after wizzard!
                 //   $user->confirmation_code = null;
                 //   $user->confirmation_code_to = null;
                 //   $user->confirmation_code_remider = 0;
            $user->confirmation_date = now();
        }

        $user->save();

        //Login!
        Auth::login($user);

        return redirect('/home');
    }

    public function statusRegister(){
        return view('status.status_register');
    }
    public function statusVerify(){
        return view('status.status_verify');
    }
    public function statusError(){
        return view('status.status_error');
    }
    public function notFound(){
        return view('status.not_found');
    }

    public function blocked()
    {
        return view('status.user_blocked');
    }

    /**
     * @return string
     */
    public function checkMail(){

        $data = Request::all();
        if($data['user_id'] === "new"){
            if(User::where('email', $data['email'])->count()){
                return json_encode(false);
            }
        }else{
            if(User::where('email', $data['email'])->where('id', '!=', $data['user_id'])->count()){
                return json_encode(false);
            }
        }
        return json_encode(true);
    }

}
