<?php

namespace App\Controllers;

use App\Repositories\UserRepository;

/**
*
*@author : Tiamiyu waliu
*@website : http://www.iDocrea8.com
*/
class SignupController extends \BaseController
{
    public function __construct(UserRepository $userRepository)
    {
        parent::__construct();
        $this->user = $userRepository;
    }

    public function index()
    {
        $response = [
            'response' => 0,
            'message' => '',
            'url' => ''
        ];


        if (!\Config::get('user-signup')) {
            if ($val = \Input::get('val')) {

                $errorMessage = null;

                $validator = \Validator::make($val, [
                    'fullname' => 'required',
                    'username' => 'required|alpha_num|slug|unique:users',
                    'email_address' => 'required|email|unique:users',
                    'password' => 'required|min:6',
                    'genre' => 'required',
                    'country' => 'required',
                    'terms-and-condition' => 'required'
                ]);

                if (\Config::get('enable-captcha') and !$this->captchaValid()) {

                    $response['message'] = trans('user.captcha-error');

                } else  {
                    if (!$validator->fails()) {
                        $user = $this->user->signup($val);
                        $response['response'] = 1;
                        /**
                         * send to proper destination
                         */
                        if (\Config::get('user-activation')) {
                            $this->user->sendActivation($user);
                            $response['url'] = \URL::route('user-activation').'?email='.$user->email_address;

                        } else {
                            /**
                             * Login user
                             */
                            $this->user->login([
                                'username' => $val['username'],
                                'password' => $val['password']
                            ]);
                            $response['url'] = \URL::route('user-home');
                        }
                    } else {
                        $response['message'] = $validator->messages()->first();
                    }
                }

                return json_encode($response);
            }
        }
    }

    public function captchaValid()
    {
        session_start();

        return ($_SESSION['captcha'] == \Input::get('val.captcha'));
    }

    public function activate()
    {
        $this->setTitle(trans('user.account-activation'));
        $email = \Input::get('email');
        $code = \Input::get('code');

        if ($val = \Input::get('val')) {

            if ($this->user->activate($val)) {
                return \Redirect::route('user-home');
            }
        }

        return $this->theme->view('user.activate', [
            'email' => $email,
            'code' => $code
        ])->render();
    }
}