package projet;

import java.io.StringReader;

import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonReader;

public interface BlocRenderer {
   
    String renderTexte(String contenu);
    String renderListe(String contenu, String style);
    String renderTitre(String contenu, int level);
    String renderCode(String contenu, String language);
    String renderPage(String title, String from);
    String renderSeparateur();
    String renderCitation(String contenu, String type);
    
    default JsonObject parseMetadata(String metadata) {
        try (JsonReader reader = Json.createReader(new StringReader(metadata))) {
            return reader.readObject();
        }
    }

    default String render(Bloc bloc) {
    	JsonObject metadata = parseMetadata(bloc.getMetadata());
        switch (bloc.getType()) {
            case TEXTE:
                return renderTexte(bloc.getContenu());
            case LISTE:
            	String style = metadata.getString("style", "bullet");
                return renderListe(bloc.getContenu(), style);
            case TITRE:
            	int level = metadata.getInt("level", 1);
                return renderTitre(bloc.getContenu(), level);
            case CODE:
            	String language = metadata.getString("language", "");
                return renderCode(bloc.getContenu(), language);
            case PAGE:
                String title = metadata.getString("title", "Page");
                String from = metadata.getString("from", "");
                return renderPage(title, from);
            case SEPARATEUR:
                return renderSeparateur();
            case CITATION:
            	String type = metadata.getString("type", "normal");
                return renderCitation(bloc.getContenu(), type);
            default:
                throw new IllegalArgumentException("Type de bloc inconnu : " + bloc.getType());
        }
    }
}

