package es.uv.saic.feign;

import java.io.IOException;
import java.math.BigInteger;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.core.io.FileSystemResource;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import es.uv.saic.domain.Document;
import es.uv.saic.dto.ArchiveOrganDTO;
import es.uv.saic.dto.CategoriaDTO;
import es.uv.saic.dto.DocumentTmpDTO;
import es.uv.saic.dto.PdfDTO;
import fr.opensagres.xdocreport.core.XDocReportException;

@FeignClient(name = "saic-docs-service")
public interface DocumentClient {

    @PostMapping("/documents/save")
    public Document save(@RequestBody Document doc);

    @PostMapping("/documents/categoria/organ")
    public Document findByCategoriaOrgan(@RequestBody CategoriaDTO categoria);

    @GetMapping("/documents/{idDocument}")
    public Document findByID(@PathVariable("idDocument") Integer idDocument);

    @PostMapping("/documents/upload")
    public String upload(@RequestBody DocumentTmpDTO documentTmpDTO);

    @PostMapping("/documents/archive")
    public Document archive(@RequestBody Document doc);

    @PostMapping("/documents/archive/organ")
    public void archiveByOrgan(@RequestBody ArchiveOrganDTO organ);

    @GetMapping("/downloads/{fileName}")
    @ResponseBody
	public ResponseEntity<byte[]> download(@PathVariable("fileName") BigInteger idInstanciaTasca);		

    @GetMapping(value="/downloads/document/{id}", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
	@ResponseBody
	public ResponseEntity<byte[]> downloadDocument(@PathVariable("id") Integer idDocument);

   	@GetMapping(value="/downloads/report/{t}/{p}", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
	@ResponseBody 
	public ResponseEntity<FileSystemResource> downloadReport(@PathVariable("t") Integer idTitulacio, @PathVariable("p") String nomProces);

    @GetMapping(value="/downloads/template/{id}", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
	@ResponseBody 
	public ResponseEntity<byte[]> downloadTemplate(@PathVariable("id") BigInteger idTascai);

    @GetMapping(value="/test/template/{titulacio}/{centre}/{idProces}/{idTascap}", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
	@ResponseBody
	public byte[] testTemplate(@PathVariable("titulacio") Integer idTitulacio, @PathVariable("centre") Integer idCentre,
		@PathVariable("idTascap") Integer idTascap, @PathVariable("idProces") Integer idProces) 
		throws IOException, XDocReportException;

    @GetMapping(value="/test/template2/{titulacio}/{centre}/{evidencia}/{curs}/{tipusTasca}", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
	@ResponseBody
	public byte[] testTemplate(@PathVariable("titulacio") Integer idTitulacio, @PathVariable("centre") Integer idCentre,
			@PathVariable("evidencia") String evidencia, @PathVariable("curs") Integer curs,
            @PathVariable("tipusTasca") Integer tipusTasca);

    @GetMapping(value="/pdf/{idTascai}")
	@ResponseBody
	public byte[] downloadTemplatePdf(@PathVariable("idTascai") BigInteger idTascai);

    @PostMapping(value="/pdf/preview")
	@ResponseBody
	public byte[] downloadTemplatePdf(@RequestBody PdfDTO pdf);

}
