package org.asciidoctor.diagram.ditaa;

import org.asciidoctor.diagram.*;
import org.stathissideris.ascii2image.core.ConversionOptions;
import org.stathissideris.ascii2image.graphics.BitmapRenderer;
import org.stathissideris.ascii2image.text.TextGrid;

import javax.imageio.ImageIO;
import javax.imageio.stream.MemoryCacheImageOutputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class Ditaa implements DiagramGenerator
{
    public static final MimeType DEFAULT_OUTPUT_FORMAT = MimeType.PNG;

    @Override
    public String getName()
    {
        return "ditaa";
    }

    @Override
    public ResponseData generate(Request request) throws IOException {
        MimeType format = request.headers.getValue(HTTPHeader.ACCEPT);

        if (format == null) {
            format = DEFAULT_OUTPUT_FORMAT;
        }

        if (!format.equals(MimeType.PNG)) {
            throw new IOException("Unsupported output format: " + format);
        }

        ConversionOptions conversionOptions;

        String optionString = request.headers.getValue(HTTPHeader.OPTIONS);
        if (optionString != null) {
            String[] options = optionString.split(" ");
            conversionOptions = ConversionOptions.parseCommandLineOptions(options);
        } else {
            conversionOptions = new ConversionOptions();
        }
        
        TextGrid textGrid = new TextGrid();
        textGrid.initialiseWithText(request.asString(), conversionOptions.processingOptions);

        org.stathissideris.ascii2image.graphics.Diagram diagram = new org.stathissideris.ascii2image.graphics.Diagram(textGrid, conversionOptions);
        BufferedImage image = new BitmapRenderer().renderToImage(diagram, conversionOptions.renderingOptions);

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        MemoryCacheImageOutputStream cacheStream = new MemoryCacheImageOutputStream(out);
        ImageIO.write(image, "png", out);
        cacheStream.flush();
        return new ResponseData(
                format,
                out.toByteArray()
        );
    }
}

@@ -63,24 +63,89 @@
 .diagram.*;%0A
+import org.stathissideris.ascii2image.core.CommandLineConverter;%0A
 import org.s
@@ -557,16 +557,69 @@
 ception;
+%0Aimport java.util.ArrayList;%0Aimport java.util.Arrays;
 %0A%0Apublic
@@ -731,16 +731,156 @@
 ype.PNG;
+%0A    public static final MimeType DEFAULT_CONTENT_TYPE = MimeType.TEXT_PLAIN_UTF8;%0A    public static final String DEFAULT_CHARSET = %22UTF-8%22;
 %0A%0A    @O
@@ -1108,17 +1108,16 @@
 CCEPT);%0A
-%0A
         
@@ -1233,16 +1233,48 @@
 ype.PNG)
+ && !format.equals(MimeType.SVG)
 ) %7B%0A    
@@ -1367,73 +1367,28 @@
     
-ConversionOptions conversionOptions;%0A%0A        String optionString
+MimeType contentType
  = r
@@ -1426,15 +1426,20 @@
 der.
-OPTIONS
+CONTENT_TYPE
 );%0A 
@@ -1453,22 +1453,21 @@
 if (
-optionString !
+contentType =
 = nu
@@ -1488,50 +1488,42 @@
     
-String%5B%5D options = optionString.split(%22 %22)
+contentType = DEFAULT_CONTENT_TYPE
 ;%0A  
@@ -1532,156 +1532,143 @@
     
+%7D%0A%0A
     
-conversionOptions = ConversionOptions.parseCommandLineOptions(options);%0A        %7D else %7B%0A            conversionOptions = new ConversionOptions()
+    String charset = contentType.parameters.get(%22charset%22);%0A        if (charset == null) %7B%0A            charset = DEFAULT_CHARSET
 ;%0A  
@@ -1675,24 +1675,16 @@
       %7D%0A
-        
 %0A       
@@ -1688,266 +1688,237 @@
     
-TextGrid textGrid = new TextGrid();%0A        textGrid.initialiseWithText(request.asString(), conversionOptions.processingOptions);%0A%0A        org.stathissideris.ascii2image.graphics.Diagram diagram = new org.stathissideris.ascii2image.graphics.Diagram(textGrid,
+java.util.List%3CString%3E options = new ArrayList();%0A        options.add(%22--encoding%22);%0A        options.add(charset);%0A%0A        if (format.equals(MimeType.SVG)) %7B%0A            options.add(%22--svg%22);%0A        %7D%0A%0A        ConversionOptions
  con
@@ -1931,18 +1931,18 @@
 nOptions
-)
 ;
+%0A
 %0A       
@@ -1946,110 +1946,189 @@
     
-BufferedImage image = new BitmapRenderer().renderToImage(diagram, conversionOptions.renderingOptions);
+String optionString = request.headers.getValue(HTTPHeader.OPTIONS);%0A        if (optionString != null) %7B%0A            options.addAll(Arrays.asList(optionString.split(%22 %22)));%0A        %7D
 %0A%0A  
@@ -2158,16 +2158,19 @@
 ream out
+put
  = new B
@@ -2205,161 +2205,201 @@
     
-MemoryCacheImageOutputStream cacheStream = new MemoryCacheImageOutputStream(out);%0A        ImageIO.write(image, %22png%22, out);%0A        cacheStream.flush
+CommandLineConverter.convert(%0A                options.toArray(new String%5B0%5D),%0A                new ByteArrayInputStream(request.data),%0A                output%0A        );%0A        output.close
 ();%0A
+%0A
     
@@ -2470,16 +2470,19 @@
      out
+put
 .toByteA

Remove code duplicated Ditaamini code from Ditaa generator