<?php

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use App\Http\Requests\Web\ForgetRequest;
use App\Http\Requests\Web\LoginRequest;
use App\Http\Requests\Web\RegistRequest;
use App\Http\Requests\Web\ResetRequest;
use App\Http\Requests\Web\VerifyRequest;
use App\Models\User;
use App\Services\Login\LoginAuthService;
use App\Services\Login\LoginService;
use App\Services\OTP\OTPService;
use App\Services\Response\ResponseService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;
use Illuminate\View\View;

class AuthController extends Controller
{

    private $view = 'web.pages.auth.';

    public function login() : View
    {
        $title = __('web.login');
        $action = route('login_check');
        $forget_action = route('forgetpassword');
        $regist_action = route('regist');
        return view($this->view . 'login', get_defined_vars());
    }

    public function check(LoginRequest $request) : JsonResponse
    {
        $login = new LoginService(new LoginAuthService());
        $response = $login->authLogin($request->all());
        return ResponseService::response($response);
    }

    /**
     * view of register
     * @return view
     */
    public function regist(): View
    {
        $title = __('web.regist');
        $action = route('registing');
        $login_action = route('login');
        return view($this->view . 'regist', get_defined_vars());
    }

    /**
     * Function for Web login to the cms.
     *
     * @param RegistRequest $request
     * @return RedirectResponse
     * @throws Exception
     */
    public function registing(RegistRequest $request): JsonResponse
    {
        $content = $request->all();
        $content['role'] = 'user';
        $user = User::create($content);
        $user->update(['password'=>bcrypt($request->password)]);

        $login = new LoginService(new LoginAuthService());
        $response = $login->authLogin($content);
        return ResponseService::response($response);
    }

    /**
     * Web logout.
     * @return RedirectResponse
     */
    public function logout(): RedirectResponse
    {
        Auth::logout();
        return redirect(route('login'));
    }



    /**
     * forgetpassword
     *
     * @return view
     */
    public function forgetpassword(): View
    {
        $title = __('web.forgetpassword');
        $action = route('forgettingpassword');
        $login_action = route('login');
        return view($this->view.'forgetpassword',get_defined_vars());
    }

    /**
     * Verify Email Forget Password
     *
     * @param Request $request
     * @return view
     */
    public function forgettingpassword(ForgetRequest $request): JsonResponse
    {
        $content =
            [
                'target'=>$request->email,
                'type'=>'email'
            ];
        $check_code = OTPService::CheckSendOTP($content);
        return ResponseService::response($check_code);
    }

    public function verify(ForgetRequest $request)
    {
        $url = url()->full();
        $title = __('web.verify');
        $type = 'email';
        $target = $request->{$type};

        $user = User::where($type,$target)->first();
        if(!$user)
        {
            return redirect(route('forgetpassword'))->with('error',__('web.checkinput'));
        }
        $content =
            [
                'target'=>$target,
                'type'=>$type
            ];
        $send_code = OTPService::SendOTP($content);
        $status = $send_code['status'];
        if($status != 200)
        {
            return redirect(route('forgetpassword'))->with('error',__('web.checkinput'));
        }

        $action = route('verifying');

        return view($this->view.'verify',get_defined_vars());
    }

    public function verifying(VerifyRequest $request): JsonResponse
    {
        $link = url()->previous();
        $url_components = parse_url($link);
        parse_str($url_components['query'], $params);

        if(!isset($params['email']))
        {
            $error = __('web.error_refresh_req');
            return ResponseService::response([$error]);
        }
        $email = $params['email'];
        $content =
            [
                'target'=>$email,
                'type'=>'email',
                'code'=>$request->code
            ];
        $send_code = OTPService::CheckOTP($content);

        return ResponseService::response($send_code);
    }

    /**
     * reset password
     *
     * @param Request $request
     * @return view
     */
    public function resetPassword(Request $request)
    {
        $target = Session::get('target');
        if(!$target)
        {
            return redirect(route('forgetpassword'))->with('error',__('web.email_req'));
        }

        $title = __('web.resetpassword');
        $action = route('resetting');
        return view($this->view.'resetpassword',get_defined_vars());
    }

    /**
     * reseting password
     *
     * @param Request $request
     * @return view
     */
    public function resetting(ResetRequest $request): JsonResponse
    {
        $target = Session::get('target');
        $type = Session::get('type');

        $user = User::where($type,$target)->first();

        Session::forget('target');
        Session::forget('type');

        $user->update(['password'=>bcrypt($request->password)]);

        $request->merge(['name'=>$user->name]);
        $login = new LoginService(new LoginAuthService());
        $response = $login->authLogin($request->all());
        return ResponseService::response($response);
    }

}
