<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Auth;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;


//    protected $redirectTo = RouteServiceProvider::HOME;

    public function __construct()
    {
        $this->middleware('guest')->except('logout');
        $this->middleware('guest:admin')->except('logout');
        $this->middleware('guest:master-dealer')->except('logout');
        $this->middleware('guest:sub-dealer')->except('logout');
        $this->middleware('guest:trial-account')->except('logout');
        $this->middleware('guest:paid-account')->except('logout');
    }

    public function redirectToDashboard()
    {
        if (Auth::guard('admin')->check()) {
            return redirect()->route('admin.dashboard');
        } elseif (Auth::guard('master-dealer')->check()) {
            return redirect()->route('master-dealer.dashboard');
        } elseif (Auth::guard('sub-dealer')->check()) {
            return redirect()->route('sub-dealer.dashboard');
        } elseif (Auth::guard('trial-account')->check()) {
            return redirect()->route('trial-accounts.dashboard');
        } elseif (Auth::guard('paid-account')->check()) {
            return redirect()->route('paid-accounts.dashboard');
        } else {
            return redirect()->route('paid-accounts.dashboard');
        }


    }

    public function showPaidLoginForm()
    {
        return view('auth.paid-account.auth-login', ['url' => route('paid-account.login-view'), 'title' => 'Trial Account Login']);
    }

    public function paidLogin(Request $request)
    {
        $this->validate($request, [
            'email' => 'required|email',
            'password' => 'required|min:6'
        ]);

        if (Auth::guard('paid-account')->attempt($request->only(['email', 'password']), $request->get('remember'))) {
            return redirect()->intended('/');

        }

        return back()->withInput($request->only('email', 'remember'))->with('login-error', 'Invalid Credentials');
    }


    public function showTrialLoginForm()
    {
        return view('auth.trial-account.auth-login', ['url' => route('trial-account.login-view'), 'title' => 'Trial Account Login']);
    }

    public function trialLogin(Request $request)
    {
        $this->validate($request, [
            'email' => 'required|email',
            'password' => 'required|min:6'
        ]);

        if (Auth::guard('trial-account')->attempt($request->only(['email', 'password']), $request->get('remember'))) {
            return redirect()->intended('/');

        }

        return back()->withInput($request->only('email', 'remember'))->with('login-error', 'Invalid Credentials');
    }


    public function showAdminLoginForm()
    {
        return view('auth.admin.auth-login', ['url' => route('admin.login-view'), 'title' => 'Admin Login']);
    }

    public function adminLogin(Request $request)
    {
        $this->validate($request, [
            'email' => 'required|email',
            'password' => 'required|min:6'
        ]);

        if (Auth::guard('admin')->attempt($request->only(['email', 'password']), $request->get('remember'))) {
            return redirect()->intended('/');

        }

        return back()->withInput($request->only('email', 'remember'))->with('login-error', 'Invalid Credentials');
    }

    public function showMasterDealerLoginForm()
    {
        return view('auth.master-dealer.auth-login', ['url' => route('master-dealer.login-view'), 'title' => 'Master Dealer Login']);
    }

    public function masterDealerLogin(Request $request)
    {
        $this->validate($request, [
            'email' => 'required|email',
            'password' => 'required|min:6'
        ]);

        if (Auth::guard('master-dealer')->attempt($request->only(['email', 'password']), $request->get('remember'))) {
            return redirect()->intended('/');

        }

        return back()->withInput($request->only('email', 'remember'))->with('login-error', 'Invalid Credentials');
    }


    public function showSubDealerLoginForm()
    {
        return view('auth.sub-dealer.auth-login', ['url' => route('sub-dealer.login-view'), 'title' => 'Dealer Login']);
    }

    public function subDealerLogin(Request $request)
    {
        $this->validate($request, [
            'email' => 'required|email',
            'password' => 'required|min:8'
        ]);

        if (Auth::guard('sub-dealer')->attempt($request->only(['email', 'password']), $request->get('remember'))) {
            return redirect()->intended('/');

        }

        return  back()->withInput($request->only('email', 'remember'))->with('login-error', 'Invalid Credentials');
//        return back()->withInput($request->only('email', 'remember'));
    }


}
