package org.codetome.snap.service.impl

import com.vladsch.flexmark.parser.Parser
import com.vladsch.flexmark.parser.ParserEmulationProfile
import com.vladsch.flexmark.util.options.MutableDataSet
import org.codetome.snap.adapter.parser.MarkdownParser
import org.codetome.snap.model.rest.RestService
import org.codetome.snap.service.RestServiceGenerator
import java.io.File

/**
 * Creates REST services based on a service description markdown file
 * (see sample_config.md for example) using <code>linkContent</code> as the
 * access linkContent.
 */
class SnapService(private val restServiceGenerator: RestServiceGenerator,
                  private val markdownParser: MarkdownParser) {

    fun run(descriptor: File): RestService {
        val options = MutableDataSet()
        options.setFrom(ParserEmulationProfile.MARKDOWN)
        val parser: Parser = Parser.builder(options).build()
        val md = descriptor.readText()
        val document = parser.parse(md)
        val restService = markdownParser.parse(document).blockingGet()
        restServiceGenerator.generateServiceFrom(restService)
        return restService
    }
}