<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class allowedDevice
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next)
    {
        $userAgent = $request->header('User-Agent');

       // if (!strpos($userAgent, 'Windows') && !$this->isFoundMyIp($request)) {
        //   abort(403);
     //  }

        return $next($request);
    }

    private function isFoundMyIp($request){
        $allowedIPs = [
            // Local Dev
            '127.0.0.1',
            // Dev
            '180.253.78.251',
            // '103.105.%.%',
            // '182.2.%.%',
            // Gudang
            '180.253.78.128',
        ];

        $clientIP = $request->ip();

        if (!in_array($clientIP, $allowedIPs)) {
            return false;
        }
        return true;
    }

}
