<?php

namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use App\Traits\CaptchaTrait;
use App\User;
use Illuminate\Support\Facades\Validator;
use App\Models\user\UserModel;

use Illuminate\Http\Request;

class CustomAuthController extends Controller
{
	use CaptchaTrait;

	public function authenticate(Request $request)
	{

		$post 	=	$request->all();
		$email = $post['email'];
		$password = $post['password'];

		if (Auth::attempt(['email' => $email, 'password' => $password])) {

			if (Auth::user()->role == 'admin') {
				$message = '
				<div class="alert alert-danger alert-dismissible">
					<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
						<h4><i class="icon fa fa-ban"></i> ERROR:</h4>
						You are not allowed to enter to this site.
				</div>';
				return response()->json(array('success' => false, 'message' => $message));
			}


			$message = "success";
			$navbar = view('layout.navbar', compact('view'))->render();
			$save_design_button = ' <button type="button" class="btn btn-lg btn-primary pull-right" data-toggle="modal" data-target="#modalDesignName"><i class="fa fa-floppy-o" aria-hidden="true"></i> Save Design</button>';

			return response()->json(array(
				'success' => true,
				'message' => $message,
				'navbar' => $navbar,
				'save_design_button' => $save_design_button
			));
		} else {

			$message = '
			<div class="alert alert-danger alert-dismissible">
				<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
					<h4><i class="icon fa fa-ban"></i> ERROR:</h4>
					Username or Password is incorrect.
			</div>';

			return response()->json(array('success' => false, 'message' => $message));
		}
	}

	public function postRegister(Request $request)
	{
		$post 	=	$request->all();
		$userModel = new UserModel;
		$post['captcha'] = $this->captchaCheck();

		$validator = Validator::make(
			$post,
			[
				'username' 				=> 'unique:user_logins',
				'email' 				=> 'unique:user_logins',
				'g-recaptcha-response'  => 'required',
				'captcha'               => 'required|min:1'
			],
			[
				'g-recaptcha-response.required' => 'Captcha is required',
				'captcha.min'           		=> 'Wrong captcha, please try again.'
			]
		);


		if ($validator->fails()) {
			$errors = "";
			foreach ($validator->errors()->all() as $error) {
				$errors .= "<li>" . $error . "</li>";
			}

			$message = '
			<div class="alert alert-danger alert-dismissible">
				<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
					<h4><i class="icon fa fa-ban"></i> ERROR:</h4>
					' . $errors .
				'</div>';

			return response()->json(array(
				'success' => false,
				'message' => $message
			));
		}

		$user = User::create([
			'name' => $post['firstname'] . ' ' . $post['lastname'],
			'username' => $post['email'],
			'email' => $post['email'],
			'password' => bcrypt($post['password']),
			'role' => 'user'
		]);

		$data = array(
			'UserId'			=> $user->id,
			'Fullname'			=> $post['firstname'] . ' ' . $post['lastname'],
			'ContactNumber'		=> $post['mobilenumber'],
			// 'OtherNotes' 	 	=> $post['othernotes'],
			'Address' 			=> $post['address'],
			'Address2' 			=> $post['address2'],
			'State'	 			=> $post['state'],
			'City' 		 		=> $post['city'],
			'ZipCode'     		=> $post['zipcode'],
			'CountryCode'     		=> $post['countryCode'],
			'Country'     		=> $post['country']
		);
		$userModel->insertAddressBook($data);

		Auth::attempt(['email' => $post['email'], 'password' => $post['password']]);

		return response()->json(array(
			'success' => true,
			'redirect' => $post['redirect']
		));
	}
}
