package com.oguerreiro.resilient.web.rest;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.oguerreiro.resilient.domain.UnitConverter;
import com.oguerreiro.resilient.error.VariableUnitConverterMoreThanOneFoundException;
import com.oguerreiro.resilient.error.VariableUnitConverterNotFoundException;
import com.oguerreiro.resilient.error.VariableUnitConverterWithoutFormulaException;
import com.oguerreiro.resilient.repository.UnitConverterRepository;
import com.oguerreiro.resilient.service.UnitConverterService;
import com.oguerreiro.resilient.service.criteria.UnitConverterCriteria;
import com.oguerreiro.resilient.service.dto.UnitConverterDTO;
import com.oguerreiro.resilient.service.query.UnitConverterQueryService;

/**
 * REST controller for managing {@link com.oguerreiro.resilient.domain.UnitConverter}.
 */
@RestController
@RequestMapping("/api/unit-converters")
public class UnitConverterResource
    extends AbstractResilientQueryResource<UnitConverter, UnitConverterDTO, Long, UnitConverterCriteria> {

  private static final String ENTITY_NAME = "unitConverter";

  private final UnitConverterService unitConverterService;

  public UnitConverterResource(UnitConverterService unitConverterService,
      UnitConverterRepository unitConverterRepository, UnitConverterQueryService unitConverterQueryService) {
    super(UnitConverter.class, unitConverterRepository, unitConverterService, unitConverterQueryService);

    this.unitConverterService = unitConverterService;
  }

  /* AbstractResilientResource methods impl*/
  @Override
  protected String getEntityName() {
    return ENTITY_NAME;
  }

  /* UnitConverterResource endpoints */
  /**
   * {@code GET  /unit-converters} : get all the unitConverters.
   *
   * @param  eagerload flag to eager load entities from relationships (This is applicable for many-to-many).
   * @return           the {@link ResponseEntity} with status {@code 200 (OK)} and the list of unitConverters in body.
   */
  @GetMapping("/from/{fromUnitId}/to/{toUnitId}")
  public List<UnitConverterDTO> getAllUnitConvertersFromAndTo(@PathVariable("fromUnitId") Long fromUnitId,
      @PathVariable("toUnitId") Long toUnitId) {
    log.debug("REST request to get all getAllUnitConvertersFromAndTo");
    List<UnitConverterDTO> values = unitConverterService.findAllFromAndTo(fromUnitId, toUnitId);
    return values;
  }

  @GetMapping("/convert/{periodId}/{variableId}/{variableClassCode}/{fromUnitId}/{value}")
  public ResponseEntity<BigDecimal> getConvert(@PathVariable("fromUnitId") Long fromUnitId,
      @PathVariable("periodId") Long periodId, @PathVariable("variableId") Long variableId,
      @PathVariable("variableClassCode") String variableClassCode, @PathVariable("value") BigDecimal value)
      throws VariableUnitConverterNotFoundException, VariableUnitConverterWithoutFormulaException,
      VariableUnitConverterMoreThanOneFoundException {
    log.debug("REST request to get getConvert : {}", fromUnitId, periodId, variableId, variableClassCode, value);

    BigDecimal convertedValue = null;
    convertedValue = unitConverterService.convertIfNeeded(periodId, variableId, fromUnitId, value, variableClassCode);

    return ResponseEntity.ok(convertedValue);
  }
}
