<?php

namespace App\Http\Controllers\Web;


use App\Http\Controllers\Controller;
use App\Mail\MailContact;
use App\Mail\MailVerifyAccount;
use App\Models\UserHistory;
use App\Repositories\UserRepository;
use App\Services\SysLog;
use App\Services\UserService;
use App\User;
use GuzzleHttp\Client;
use Request;
use Illuminate\Support\Facades\Mail;
use App\Services\Util;
use Validator;


class RegisterController extends Controller
{

    protected $userRepo;
    /**
     * Create a new controller instance.
     *
     * @return void
     */

    public function __construct(UserRepository $userRepo)
    {
        $this->middleware('guest');
        $this->userRepo = $userRepo;
    }

    public function member($member_id = false)
    {
        if(!$member_id){
            return redirect('/registrierung');
        }
        $user_id = (int) str_replace('gs', '', $member_id) - config('main.add_number_id');
        $user = User::find($user_id);
        if(!$user || !$user->isActive() ||  !$user->isActiveAccount()){
            return redirect('/registrierung');
        }
        $data = [
            'from_member_id' => $member_id
        ];
        return view('auth.register', $data);
    }

    public function register(){


        $rules = array(
            'salutation' => 'required',
            'first_name'=>'required',
            'last_name'=>'required',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|string|min:6|confirmed',
            'password_confirmation' => 'required|string|min:6',
            'accepted_data_protection' => 'required',
        );

        $validator = Validator::make(Request::all(), $rules);
        if ($validator->fails()) {
            return back()->withErrors($validator)->withInput(Request::all());
        }

        $data = Request::all();
        $user = $this->userRepo->create($data);

        $confirmation_code = UserService::createConfirmationCode();

        $user->lang = !empty(\App::getLocale()) ? \App::getLocale() : "de";
        $user->confirmation_code = $confirmation_code;
        $user->confirmation_code_to = date('Y-m-d H:i:s', strtotime('+1 week'));
        $user->confirmation_code_remider = 0;
        if(isset($data['from_member_id'])){
            $user->m_sponsor = (int) str_replace('gs', '', $data['from_member_id']) - config('main.add_number_id');
        }
        $user->save();

        $user = User::find($user->id);

        try {
            Mail::to($user->email)->send(new MailVerifyAccount($confirmation_code, $user));
        }
        catch(\Exception $e){
            SysLog::action('register-user', 'auth_register', 5)
                ->setUserId($user->id)
                ->setModel($user->id, User::class)
                ->setMessage('Error send register E-Mail: '.$e->getMessage())
                ->save();
        }
        UserHistory::create(['user_id' => $user->id, 'action'=>'register', 'status'=>0]);

        return redirect('/user_register/finish');
    }

    public function finish()
    {
        $data = [

        ];
        return view('auth.finish', $data);
    }
}