package io.skysail.server.app.designer.application.resources;

import java.io.IOException;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;

import io.skysail.server.app.designer.DesignerApplication;
import io.skysail.server.app.designer.application.DbApplication;
import io.skysail.server.app.designer.application.ImportDefinition;
import io.skysail.server.app.designer.exceptions.MappingException;
import io.skysail.server.app.designer.repo.DesignerRepository;
import io.skysail.server.restlet.resources.PostEntityServerResource;

public class ImportResource extends PostEntityServerResource<ImportDefinition> {

    private static ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
    
    @Override
    public ImportDefinition createEntityTemplate() {
        return new ImportDefinition();
    }
    
    @Override
    public void addEntity(ImportDefinition entity) {
        String content = entity.getYamlImport().replace("&#34;", "\"");
        try {
            DbApplication dbApplication = mapper.readValue(content, DbApplication.class);
//            DesignerRepository.add(dbApplication, ((DesignerApplication)getApplication()).getApplicationModel());
//            
//            String id = app.getRepository(io.skysail.server.app.wikiapp.space.Space.class).save(entity, app.getApplicationModel()).toString();
//            entity.setId(id);
            
        } catch (IOException e) {
            throw new MappingException(e.getMessage(), e);
        }
    }
    
    @Override
    public String redirectTo() {
        return super.redirectTo(ApplicationsResource.class);
    }

}
