package com.oguerreiro.resilient.web.rest;

import java.util.List;

import org.springframework.http.ContentDisposition;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
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.EmissionFactor;
import com.oguerreiro.resilient.repository.EmissionFactorRepository;
import com.oguerreiro.resilient.service.criteria.EmissionFactorCriteria;
import com.oguerreiro.resilient.service.dto.EmissionFactorDTO;
import com.oguerreiro.resilient.service.query.EmissionFactorQueryService;
import com.oguerreiro.resilient.service.query.EmissionFactorService;

/**
 * REST controller for managing {@link com.oguerreiro.resilient.domain.EmissionFactor}.
 */
@RestController
@RequestMapping("/api/emission-factors")
public class EmissionFactorResource
    extends AbstractResilientQueryResource<EmissionFactor, EmissionFactorDTO, Long, EmissionFactorCriteria> {

  private static final String ENTITY_NAME = "emissionFactor";

  private final EmissionFactorService emissionFactorService;

  public EmissionFactorResource(EmissionFactorService emissionFactorService,
      EmissionFactorRepository emissionFactorRepository, EmissionFactorQueryService emissionFactorQueryService) {
    super(EmissionFactor.class, emissionFactorRepository, emissionFactorService, emissionFactorQueryService);
    this.emissionFactorService = emissionFactorService;
  }

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

  /* EmissionFactorResource endpoints */
  @GetMapping("/export")
  public ResponseEntity<byte[]> export(EmissionFactorCriteria criteria) {
    log.debug("REST request to export : {}");

    Integer year = criteria.getYear() == null ? null : criteria.getYear().getEquals();
    Long scopeId = criteria.getVariableScopeId() == null ? null : criteria.getVariableScopeId().getEquals();
    Long categoryId = criteria.getVariableCategoryId() == null ? null : criteria.getVariableCategoryId().getEquals();

    byte[] fileContent = this.emissionFactorService.export(year, scopeId, categoryId);

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
    headers.setContentDisposition(ContentDisposition.attachment().filename("report.xlsx").build());

    return new ResponseEntity<>(fileContent, headers, HttpStatus.OK);
  }

  @GetMapping("/search/{year}/{scopeId}/{categoryId}")
  public ResponseEntity<List<EmissionFactorDTO>> getAllBySearch(
      @PathVariable(value = "year", required = true) Integer year,
      @PathVariable(value = "scopeId", required = false) String scopeId,
      @PathVariable(value = "categoryId", required = false) String categoryId) {
    log.debug("REST request to getAllBySearch by: {}", year, scopeId, categoryId);

    Long searchScopeId = null;
    if (scopeId != null && !scopeId.equals("ALL")) {
      searchScopeId = Long.valueOf(scopeId);
    }

    Long searchCategoryId = null;
    if (categoryId != null && !categoryId.equals("ALL")) {
      searchCategoryId = Long.valueOf(categoryId);
    }

    List<EmissionFactorDTO> domainList = emissionFactorService.findAllBySearch(year, searchScopeId, searchCategoryId);
    return ResponseEntity.ok().body(domainList);
  }
}
