package studycave.studycaverestservice.controller;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import studycave.studycaverestservice.PropertiesLoader;
import studycave.studycaverestservice.model.user.User;
import studycave.studycaverestservice.usos.AuthorizationService;
import studycave.studycaverestservice.usos.TokenResponseDTO;
import studycave.studycaverestservice.usos.UsosResponseDTO;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

@RestController
@RequestMapping("/usos")
public class UsosController {

    private static final String ENC = "UTF-8";
    @Autowired
    private AuthorizationService authorizationService;
    @Autowired
    private PropertiesLoader propertiesLoader;

    @GetMapping("/access_token")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "oauth_token", value = "User's oauth_token", required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "oauth_token_secret", value = "User's oauth_token_secret", required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "oauth_verifier", value = "User's oauth_verifier", required = true, dataType = "string", paramType = "header")
    })
    public ResponseEntity<TokenResponseDTO> authorize(HttpServletRequest request) {
        HttpHeaders responseHeaders = new HttpHeaders();

        String oauth_token = request.getHeader("oauth_token");
        String oauth_token_secret = request.getHeader("oauth_token_secret");
        String oauth_verifier = request.getHeader("oauth_verifier");

        try {
            TokenResponseDTO responseToken = authorizationService.authorize(oauth_token, oauth_token_secret, oauth_verifier);
            responseHeaders.add("authorization", responseToken.getKey());
            return ResponseEntity.status(HttpStatus.OK).headers(responseHeaders).body(responseToken);
        } catch (NoSuchAlgorithmException | InvalidKeyException | IOException e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).headers(responseHeaders).body(new TokenResponseDTO(e.getCause().toString(), null, null));
        }
    }

    @GetMapping("/request_token")
    public ResponseEntity<UsosResponseDTO> getToken(@RequestParam(required = false, defaultValue = "oob") String oauth_callback) {
        HttpHeaders responseHeaders = new HttpHeaders();
        try {
            return ResponseEntity.status(HttpStatus.OK).headers(responseHeaders).body(new UsosResponseDTO(authorizationService.requestToken(oauth_callback)));
        } catch (NoSuchAlgorithmException | UnsupportedEncodingException | InvalidKeyException e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).headers(responseHeaders).body(new UsosResponseDTO(e.getCause().toString()));
        }
    }

}
