<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use App\UserLogin;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Auth;




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;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/user/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }
    public function username()
    {
        return 'username';
    }



    /**
     * The user has been authenticated.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  mixed  $user
     * @return mixed
     */

    public function authenticated(Request $request, $user)
    {



        if($user->status == 0){
            $this->guard()->logout();
            $notification =  array('message' => 'Sorry Your Account is Block Now.!','alert-type' => 'warning');
            return redirect('/login')->with($notification);
        }

//        $ip = NULL; $deep_detect = TRUE;
//
//        if (filter_var($ip, FILTER_VALIDATE_IP) === FALSE) {
//            $ip = $_SERVER["REMOTE_ADDR"];
//            if ($deep_detect) {
//                if (filter_var(@$_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP))
//                    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
//                if (filter_var(@$_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP))
//                    $ip = $_SERVER['HTTP_CLIENT_IP'];
//            }
//        }
//        $xml = simplexml_load_file("http://www.geoplugin.net/xml.gp?ip=".$ip);
//
//        $country =  $xml->geoplugin_countryName ;
//        $city = $xml->geoplugin_city;
//        $area = $xml->geoplugin_areaCode;
//        $code = $xml->geoplugin_countryCode;

//        $user_agent     =   $_SERVER['HTTP_USER_AGENT'];
//        $os_platform    =   "Unknown OS Platform";
//        $os_array       =   array(
//            '/windows nt 10/i'     =>  'Windows 10',
//            '/windows nt 6.3/i'     =>  'Windows 8.1',
//            '/windows nt 6.2/i'     =>  'Windows 8',
//            '/windows nt 6.1/i'     =>  'Windows 7',
//            '/windows nt 6.0/i'     =>  'Windows Vista',
//            '/windows nt 5.2/i'     =>  'Windows Server 2003/XP x64',
//            '/windows nt 5.1/i'     =>  'Windows XP',
//            '/windows xp/i'         =>  'Windows XP',
//            '/windows nt 5.0/i'     =>  'Windows 2000',
//            '/windows me/i'         =>  'Windows ME',
//            '/win98/i'              =>  'Windows 98',
//            '/win95/i'              =>  'Windows 95',
//            '/win16/i'              =>  'Windows 3.11',
//            '/macintosh|mac os x/i' =>  'Mac OS X',
//            '/mac_powerpc/i'        =>  'Mac OS 9',
//            '/linux/i'              =>  'Linux',
//            '/ubuntu/i'             =>  'Ubuntu',
//            '/iphone/i'             =>  'iPhone',
//            '/ipod/i'               =>  'iPod',
//            '/ipad/i'               =>  'iPad',
//            '/android/i'            =>  'Android',
//            '/blackberry/i'         =>  'BlackBerry',
//            '/webos/i'              =>  'Mobile'
//        );
//        foreach ($os_array as $regex => $value) {
//            if (preg_match($regex, $user_agent)) {
//                $os_platform    =   $value;
//            }
//        }
//        $browser        =   "Unknown Browser";
//        $browser_array  =   array(
//            '/msie/i'       =>  'Internet Explorer',
//            '/firefox/i'    =>  'Firefox',
//            '/safari/i'     =>  'Safari',
//            '/chrome/i'     =>  'Chrome',
//            '/edge/i'       =>  'Edge',
//            '/opera/i'      =>  'Opera',
//            '/netscape/i'   =>  'Netscape',
//            '/maxthon/i'    =>  'Maxthon',
//            '/konqueror/i'  =>  'Konqueror',
//            '/mobile/i'     =>  'Handheld Browser'
//        );
//        foreach ($browser_array as $regex => $value) {
//            if (preg_match($regex, $user_agent)) {
//                $browser    =   $value;
//            }
//        }
        $user->login_time = Carbon::now();
        $user->save();
        $ul['user_id'] = $user->id;
        $ul['user_ip'] =  request()->ip();
//        $ul['location'] = $city.(" - $area - ").$country .(" - $code ");
//        $ul['details'] = $browser.' on '.$os_platform;
        UserLogin::create($ul);
    }



    public function logout(Request $request)
    {
        Auth::guard()->logout();
        session()->flash('message', 'Just Logged Out!');

        return redirect('/login');
    }
}
