package com.libreecm.commonAPI.auxiliars.exceptions;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.http.HttpStatus;

import java.net.ConnectException;

import org.springframework.hateoas.VndErrors;

/**
 * Class that responses a call that produces an exception
 * This class catches the exception and converts it to a html response
 * @author Jordi Pérez Bayerri
 *
 */
@ControllerAdvice
public class RepositoryControllerAdvice {

    @ResponseBody
    @ExceptionHandler(ConnectionDBNotPossibleException.class)
    @ResponseStatus(HttpStatus.SERVICE_UNAVAILABLE)
    VndErrors connectionDBNotPossibleExceptionHandler(ConnectionDBNotPossibleException ex) {
        return new VndErrors("error", ex.getMessage());
    }
    
    @ResponseBody
    @ExceptionHandler(NotFoundUserException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    VndErrors userNotFoundExceptionHandler(NotFoundUserException ex) {
        return new VndErrors("error", ex.getMessage());
    }
    
    @ResponseBody
    @ExceptionHandler(RepositoryNotFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    VndErrors repositoryNotFoundExceptionHandler(RepositoryNotFoundException ex) {
        return new VndErrors("error", ex.getMessage());
    }
       
    @ResponseBody
    @ExceptionHandler(AlreadyExistingRepositoryException.class)
    @ResponseStatus(HttpStatus.CONFLICT)
    VndErrors alreadyExistingRepositoryExceptionHandler(AlreadyExistingRepositoryException ex) {
        return new VndErrors("error", ex.getMessage());
    }
    
    @ResponseBody
    @ExceptionHandler(AlreadyExistingUserException.class)
    @ResponseStatus(HttpStatus.CONFLICT)
    VndErrors alreadyExistingUserExceptionHandler(AlreadyExistingUserException ex) {
        return new VndErrors("error", ex.getMessage());
    }
    
    
    @ResponseBody
    @ExceptionHandler(NoneExistingRepositoryException.class)
    @ResponseStatus(HttpStatus.NO_CONTENT)
    VndErrors noneExistingRepositoryExceptionHandler(NoneExistingRepositoryException ex) {
        return new VndErrors("error", ex.getMessage());
    }

    @ResponseBody
    @ExceptionHandler(AddUserOkException.class)
    @ResponseStatus(HttpStatus.OK)
    VndErrors addUserOkExceptionHandler(AddUserOkException ex) {
        return new VndErrors("info", ex.getMessage());
    }
    
    @ResponseBody
    @ExceptionHandler(AddRepositoryOkException.class)
    @ResponseStatus(HttpStatus.OK)
    VndErrors addRepositoryOkExceptionHandler(AddRepositoryOkException ex) {
        return new VndErrors("info", ex.getMessage());
    }

    @ResponseBody
    @ExceptionHandler(ConnectException.class)
    @ResponseStatus(HttpStatus.SERVICE_UNAVAILABLE)
    VndErrors connectExceptionHandler(ConnectException ex) {
        return new VndErrors("error", ex.getMessage());
    }

    @ResponseBody
    @ExceptionHandler(HttpClientErrorException.class)
    @ResponseStatus(HttpStatus.SERVICE_UNAVAILABLE)
    VndErrors httpClientErrorException(HttpClientErrorException ex) {
        return new VndErrors("error", ex.getMessage());
    }

}