package br.gov.servicos.editor.servicos;

import br.gov.servicos.editor.cartas.Carta;
import br.gov.servicos.editor.cartas.Cartas;
import lombok.SneakyThrows;
import lombok.experimental.FieldDefaults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.Formatter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;

import static java.util.Locale.getDefault;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;
import static lombok.AccessLevel.PRIVATE;
import static org.springframework.web.bind.annotation.RequestMethod.GET;

@Controller
@FieldDefaults(level = PRIVATE, makeFinal = true)
class ListarCartasController {

    Path v3;
    Cartas cartas;

    Formatter<Carta> formatter;

    @Autowired
    ListarCartasController(File repositorioCartasLocal, Cartas cartas, Formatter<Carta> formatter) {
        this.cartas = cartas;
        this.formatter = formatter;
        this.v3 = Paths.get(repositorioCartasLocal.getAbsolutePath(), "cartas-servico", "v3", "servicos");
    }

    @ResponseBody
    @RequestMapping(value = "/editar/api/servicos", method = GET)
    Iterable<Metadados> listar() throws IOException {
        FilenameFilter filter = (x, name) -> name.endsWith(".xml");
        Function<Path, Carta> getId = f -> carta(formatter, f);
        Function<Path, Map<Carta, Path>> indexaServicos = f -> Arrays.asList(f.toFile().listFiles(filter))
                .stream()
                .map(File::toPath)
                .collect(toMap(getId, x -> x));

        Map<Carta, Path> mapaServicos = indexaServicos.apply(v3);

        return mapaServicos.entrySet().parallelStream()
                .map(p -> p.getKey().getMetadados())
                .filter(Objects::nonNull)
                .collect(toList());
    }

    @SneakyThrows
    private Carta carta(Formatter<Carta> factory, Path f) {
        return factory.parse(f.toFile().getName().replaceAll(".xml$", ""), getDefault());
    }

}

@@ -116,22 +116,30 @@
 .cartas.
-Cartas
+RepositorioGit
 ;%0Aimport
@@ -489,60 +489,8 @@
 y;%0A%0A
-import java.io.File;%0Aimport java.io.FilenameFilter;%0A
 impo
@@ -544,36 +544,8 @@
 th;%0A
-import java.nio.file.Paths;%0A
 impo
@@ -569,82 +569,71 @@
 ys;%0A
+%0A
 import 
-java.util.Map;%0Aimport java.util.Objects;%0Aimport java.util.f
+static br.gov.servicos.editor.utils.Unchecked.F
 unction.
 Func
@@ -632,18 +632,18 @@
 ion.
-F
 unc
-tion;%0A
+hecked;
 %0Aimp
@@ -736,57 +736,8 @@
 st;%0A
-import static java.util.stream.Collectors.toMap;%0A
 impo
@@ -950,36 +950,38 @@
     
-Path v3;%0A    Cartas cartas;%0A
+RepositorioGit repositorioGit;
 %0A   
@@ -1056,14 +1056,9 @@
 ler(
-File r
+R
 epos
@@ -1067,34 +1067,26 @@
 orio
-CartasLocal, Cartas cartas
+Git repositorioGit
 , Fo
@@ -1117,38 +1117,8 @@
 ) %7B%0A
-        this.cartas = cartas;%0A
     
@@ -1166,100 +1166,39 @@
 his.
-v3 = Paths.get(repositorioCartasLocal.getAbsolutePath(), %22cartas-servico%22, %22v3%22, %22servicos%22)
+repositorioGit = repositorioGit
 ;%0A  
@@ -1352,236 +1352,118 @@
     
-FilenameFilter filter = (x, name) -%3E name.endsWith(%22.xml%22);%0A        Function%3CPath, Carta%3E getId = f -%3E carta(formatter, f);%0A        Function%3CPath, Map%3CCarta, Path%3E%3E indexaServicos = f -%3E Arrays.asList(f.toFile().listFiles(filter
+return Arrays.asList(repositorioGit.getCaminhoAbsoluto().toFile().listFiles((x, name) -%3E name.endsWith(%22.xml%22)
 ))%0A 
@@ -1478,17 +1478,25 @@
        .
-s
+parallelS
 tream()%0A
@@ -1520,287 +1520,163 @@
 map(
-File::toPath)%0A                .collect(toMap(getId, x -%3E x));%0A%0A        Map%3CCarta, Path%3E mapaServicos = indexaServicos.apply(v3);%0A%0A        return mapaServicos.entrySet().parallelStream()%0A                .map(p -%3E p.getKey().getMetadados())%0A                .filter(Objects::nonNull
+f -%3E f.getName().replaceAll(%22.xml$%22, %22%22))%0A                .map(unchecked(id -%3E formatter.parse(id, getDefault())))%0A                .map(Carta::getMetadados
 )%0A  

Simplifica listagem de cartas