package io.abnd.rvep.user.rest;

import io.abnd.rvep.security.service.impl.FirebaseAuthVerifier;
import io.abnd.rvep.security.service.impl.RvepJwtGenerator;
import io.abnd.rvep.user.model.impl.RvepUserRegistrationRequest;
import io.abnd.rvep.user.model.impl.RvepUserRegistrationResponse;
import io.abnd.rvep.user.service.impl.RvepRegisterUserService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.security.GeneralSecurityException;

@RestController
@RequestMapping("/api/registration")
public class UserRegistrationController {

    private RvepRegisterUserService rvepRegisterUserService;
    private FirebaseAuthVerifier fbAuthVerifier;
    private RvepJwtGenerator jwtGenerator;

    public UserRegistrationController(RvepRegisterUserService rvepRegisterUserService,
                                      FirebaseAuthVerifier fbAuthVerifier,
                                      RvepJwtGenerator jwtGenerator) {
        this.rvepRegisterUserService = rvepRegisterUserService;
        this.fbAuthVerifier = fbAuthVerifier;
        this.jwtGenerator = jwtGenerator;
    }

    @ResponseBody
    @RequestMapping(value="/register/user",
                    method=RequestMethod.POST,
                    headers="Content-Type=application/json",
                    consumes="application/json",
                    produces="application/json")
    public ResponseEntity<RvepUserRegistrationResponse>
    register(@RequestBody RvepUserRegistrationRequest request)
            throws GeneralSecurityException, IOException {
        // init response
        RvepUserRegistrationResponse registerUserResponse =
                new RvepUserRegistrationResponse();

        // check if user is not registered
        if (!rvepRegisterUserService.isUserRegistered(request.getEmail())) {
            // register user
            registerUserResponse.setIsRegistered(
                    rvepRegisterUserService
                            .registerUser(request.getEmail(),
                                    request.getProvider()));

            // get firebase idToken from header

            // generate rvep api idtoken
            String idToken = jwtGenerator.generateIdToken(
                    request.getEmail(),
                    request.getProvider());
            registerUserResponse.setIdToken(idToken);

        } else {
            // user already registered
            registerUserResponse.setIsRegistered(false);
        }

        ResponseEntity<RvepUserRegistrationResponse> response =
                new ResponseEntity<>(registerUserResponse, HttpStatus.OK);

        return response;
    }

    @ResponseBody
    @RequestMapping(value="/is/user/registered",
            method= RequestMethod.POST,
            headers="Content-Type=application/json",
            produces="application/json")
    public ResponseEntity<RvepUserRegistrationResponse>
    isUserRegistered(@RequestBody RvepUserRegistrationRequest request)
            throws GeneralSecurityException, IOException {
        // init response
        RvepUserRegistrationResponse registerUserResponse =
                new RvepUserRegistrationResponse();

        boolean isRegistered = this.rvepRegisterUserService.isUserRegistered(request.getEmail());
        registerUserResponse.setIsRegistered(isRegistered);

        // generate rvep api idtoken
        if (isRegistered) {
            String idToken = jwtGenerator.generateIdToken(
                    request.getEmail(),
                    request.getProvider());
            registerUserResponse.setIdToken(idToken);
        }

        ResponseEntity<RvepUserRegistrationResponse> response =
                new ResponseEntity<>(registerUserResponse, HttpStatus.OK);

        return response;
    }

}

@@ -30,72 +30,8 @@
 t;%0A%0A
-import io.abnd.rvep.security.service.impl.FirebaseAuthVerifier;%0A
 impo
@@ -660,57 +660,8 @@
 ce;%0A
-    private FirebaseAuthVerifier fbAuthVerifier;%0A
     
@@ -791,83 +791,8 @@
 ce,%0A
-                                      FirebaseAuthVerifier fbAuthVerifier,%0A
     
@@ -926,54 +926,8 @@
 ce;%0A
-        this.fbAuthVerifier = fbAuthVerifier;%0A
     

remove unused service