<?php

namespace App\Repositories;

use App\Counts;
use App\User;
use Auth;
use Validator;
use Mail;
use Exception;
use Input;
use Session;
use Hash;
use Request;

class AuthRepository
{


    public function login($email, $password, $remember_me = false)
    {

        $credentials = [
            'email' => $email,
            'password' => $password
        ];

        $user = User::whereEmail($email)->first();
        try {

            if (Auth::attempt($credentials, $remember_me, $user->status == '1')) {
                $counts = Counts::where('client_id', Auth::user()->id)->get();
                if (sizeof($counts) == 0) {
                    $counts = new Counts();
                    $counts->client_id = Auth::user()->id;
                    $counts->save();
                }
                if ($user->role == 'admin') {
                    return ['result' => 2, 'message' => 'Login Successful'];
                } else {

                    return ['result' => 1, 'message' => 'Login Successful'];
                }
            } else {
                return ['result' => 0, 'message' => 'Invalid Credentials'];
            }

        } catch (\Exception $e) {
            return ['result' => 0, 'message' => $e->getMessage()];
        }
    }

    public function register($username, $email, $password, $ip)
    {

        $validator = Validator::make([
            'username' => $username,
            'email' => $email,
            'password' => $password
        ], [
            'username' => 'required',
            'email' => 'required|email|unique:users',
            'password' => 'required|min:6',

        ]);


        if ($validator->fails()) {
            return ['result' => 0, 'validator' => $validator];
        } else {
            //$users = User::where('ip', $ip)->get();
           // if (sizeof($users) > 0) {
           //    return ['result' => 2];
           // }
            $confirmation_code = str_random(30);
            $user = new User();
            $user->name = $username;
            $user->email = $email;
            $user->ip = $ip;
            $user->confirmation_code = $confirmation_code;
            $user->password = Hash::make($password);
            $user->save();

            Mail::send('auth.verify', ['confirmation_code' => $confirmation_code], function ($message) {
                $message->to(Input::get('email'))
                    ->subject('Verify your email address');
            });

            return ['result' => 1];
        }

    }

    public function confirm($confirmation_code)
    {
        if (!$confirmation_code) {
            throw new Exception('invalid confirmation code');
        }

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

        if (!$user) {
            throw new Exception('invalid confirmation code');
        }

        $user->status = 1;
        $user->confirmation_code = null;
        $user->save();
        return ['result' => 1];

    }

    public function changePassword($old_password, $new_password, $email = NULL)
    {

        $hash_new_password = Hash::make($new_password);

        if (Hash::check($old_password, Auth::user()->password)) {
            if ($email == NULL) {
                User::whereId(Auth::user()->id)->update(['password' => $hash_new_password]);
            } else {
                User::whereId(Auth::user()->id)->update(['password' => $hash_new_password, 'email' => $email]);
            }
            return ['result' => 1];

        } else {
            return ['result' => 0];

        }

    }

    public function logout()
    {
        Auth::logout();
        Session::flush();
        return ['result' => 1];


    }


}