<?php namespace App\Http\Controllers\Auth;

use App\Counts;
use App\Http\Controllers\Controller;
use App\Links;
use App\Repositories\AuthRepository;
use App\User;
use App\Videos;
use App\Websites;
use Input;
use Session;
use Auth;

class AuthController extends Controller
{

    public $authRepository;

    public function __construct(AuthRepository $authRepository)
    {
        $this->authRepository = $authRepository;
    }

    public function getLogin()
    {
        return view('auth.login');
    }

    public function postLogin()
    {

        $response = $this->authRepository->login(Input::get('email'), Input::get('password'), Input::get('remember_me'));

        if ($response['result'] == 0) {
            Session::flash('message', 'Invalid Credentials');
            return redirect()->back()->withInput(Input::all());
        }

            if($response['result'] == 2)
            {
                return redirect()->to('/administrator');
            }

        if($response['result'] == 1)
        {
            return redirect()->to('/dashboard');

        }

    }

    public function delete($id=Null)
    {
        if(env('WRITE_ACCESS')==false)
        {
            Session::flash('error_msg', 'Disabled In Demo');

            return redirect()->back();
        }

        if($id==Null) {
            User::whereId(Auth::user()->id)->delete();
            Websites::where('client_id',Auth::user()->id)->delete();
            Links::where('client_id',Auth::user()->id)->delete();
            Videos::where('client_id',Auth::user()->id)->delete();
            Counts::where('client_id',Auth::user()->id)->delete();
        }
        else
        {
            User::whereId($id)->delete();

        }
        Session::flash('success_msg', 'Successfully deleted');
        return redirect()->back();

    }

    public function edit($id)
    {
         $user=User::whereId($id)->first();
        return view('administrator.edit', ['user' => $user]);

    }
    public function postEdit($id)
    {
        $points=Input::get('points');
        $type=Input::get('type');

        User::whereId($id)->update(['points' =>$points,'membership' =>$type]);
        Session::flash('success_msg', 'Successfully Updated');
        return redirect()->back();


    }

    public function changePassword()
    {
        if(env('WRITE_ACCESS')==false)
        {
            Session::flash('error_msg', 'Disabled In Demo');

            return redirect()->back();
        }

        if(Input::has('email')) {
            $response = $this->authRepository->changePassword(Input::get('old_password'), Input::get('new_password'), Input::get('email'));
        }
        else{
            $response = $this->authRepository->changePassword(Input::get('old_password'), Input::get('new_password'));

        }
        if ($response['result'] == 0) {
            Session::flash('error_msg', 'Invalid Credentials');
            return redirect()->back()->withInput(Input::all());
        }
        if ($response['result'] == 1) {
            Session::flash('success_msg', 'Successfully Updated');
            return redirect()->back()->withInput(Input::all());
        }
    }

    public function getRegister($ref_id = 0)
    {
        return view('auth.register', ['ref_id' => $ref_id]);
    }

    public function postRegister($ref_id = 0)
    {
        $response = $this->authRepository->register(Input::get('username'), Input::get('email'), Input::get('password'),Input::get('ip'));

        if ($response['result'] == 0) {
            return redirect()->back()->withInput(Input::all())->withErrors($response['validator']);
        }
        if ($response['result'] == 2) {
            Session::flash('error_msg', ' Account Is Already Registered On This IP.');
            return redirect()->back();
        }
        if (strlen($ref_id) > 1) {
            $ref = explode('=', $ref_id);
            User::whereId($ref[1])->increment('points', '100');
        }
        Session::flash('success_msg', 'Thanks for signing up! Please check your email.');
        return redirect()->back();

    }

    public function confirm($confirmation_code)
    {
        $response = $this->authRepository->confirm($confirmation_code);

        if ($response['result'] == 1)
            Session::flash('success_msg', 'Successfully Registered');
        return redirect('/login');

    }


    public function logout()
    {
        $response = $this->authRepository->logout();
        if ($response['result'] == 1)
            return redirect()->to('/login');

    }

}
