<?php

namespace App\Http\Controllers\Authentication;

use App\Http\Controllers\Controller;
use App\Repositories\AuthRepository;
use App\Repositories\BiodataRepository;
use App\Repositories\UsersRepository;
use App\User;
use Auth;
use Exception;
use GuzzleHttp\Client as GuzzleHttpClient;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Str;

class LoginController extends Controller
{
    private $authRepo;
    private $userRepo;
    private $biodataRepo;
    private $userDetailRepo;

    public function __construct(
        AuthRepository $authRepo,
        UsersRepository $userRepo,
        BiodataRepository $biodataRepo
    ) {
        $this->authRepo = $authRepo;
        $this->userRepo = $userRepo;
        $this->biodataRepo = $biodataRepo;
    }

    public function index()
    {
        try {
            $sessso = $_COOKIE['email_sso'];
            if (isset($sessso)) {
                $urlsso = 'https://sso.unesa.ac.id/api/user-cookie';
                $cek_sesisso = file_get_contents($urlsso."/$sessso");
                $rr = (json_decode($cek_sesisso));
                $emailsso = $rr->email;
                $useridsso = $rr->userid;
            }

            $user = $this->userRepo->find(null, null, $emailsso);

            if ($user) {
                // Login
                return $this->getlogin($user->id);
            } else {
                // Add User
                if (isset($sessso)) {
                    $auth = $this->authRepo->getEmailAuth($emailsso);

                    $this->getadduser($auth);

                    return redirect()->route('dashboard');
                }
            }

            return Redirect::to('https://sso.unesa.ac.id/dashboard');
        } catch (\Exception $sessso) {
            return View('auth.login');
        }
    }

    public function sso(Request $request, $email, $session_id)
    {
        $auth = $this->authRepo->sso($request, $email, $session_id);

        if (!is_array($auth)) {
            return redirect('https://sso.unesa.ac.id/user');
        }

        $user = $this->userRepo->find(null, null, $auth[0]->email);

        if ($user) {
            // Login
            return $this->getlogin($user->id);
        } else {
            // Add User
            return $this->getadduser($auth);
        }

        return redirect()->route('/');
    }

    private function getlogin($userid)
    {
        Auth::loginUsingId($userid);

        return redirect()->intended('dashboard');
    }

    private function getAdduser($auth)
    {
        $biodata = $this->biodataRepo->biodata($auth);

        if ($biodata) {
            DB::beginTransaction();
            try {
                $id = (string) Str::uuid();
                $this->userRepo->storeSso($id, $biodata);
                $this->biodataRepo->storeSso($id, $biodata);

                Auth::loginUsingId($id);

                DB::commit();

                return redirect()->intended('dashboard');
            } catch (Exception $ex) {
                DB::rollBack();
            }
        }

        return redirect()->route('login');
    }

    // public function check($email)
    // {
    //     $clientbiodata = new GuzzleHttpClient();
    //     $apiRequestbiodata = $clientbiodata->request('GET', 'https://sso.unesa.ac.id/userid/'. $email);
    //     $auth = json_decode($apiRequestbiodata->getBody()->getContents());

    //     // $auth[0] = (object)['userid' => '18020074065'];
    //     $check = $this->biodataRepo->siakadu($auth);

    //     dd($check);
    // }
}
