package pt.ist.socialsoftware.edition.ldod.controller.api.microfrontend.documents;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import pt.ist.socialsoftware.edition.ldod.controller.api.microfrontend.dto.FragmentDto;
import pt.ist.socialsoftware.edition.ldod.controller.api.microfrontend.dto.SourceInterDto;
import pt.ist.socialsoftware.edition.ldod.domain.Fragment;
import pt.ist.socialsoftware.edition.ldod.domain.LdoD;
import pt.ist.socialsoftware.edition.ldod.domain.Source;

@RestController
@RequestMapping("/api/microfrontend/documents")
public class MicrofrontendDocumentsController {

	@GetMapping("/fragments")
    public List<FragmentDto> getFragments() {

        return LdoD.getInstance().getFragmentsSet().stream()
                .map(FragmentDto::new)
                .collect(Collectors.toList());
    }
    
    @GetMapping("/sources")
    public List<SourceInterDto> getSources() {
        
        LdoD ldoD = LdoD.getInstance();
        
        List<Source> sources = new ArrayList<>();
		for (Fragment frag : ldoD.getFragmentsSet()) {
			sources.addAll(frag.getSourcesSet());
		}

        return sources.stream()
                .map(SourceInterDto::new)
                .collect(Collectors.toList());
    }
}
