package io.jboot.apidoc;

import com.jfinal.template.Engine;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public abstract class ApiDocRender {
    public static ApiDocRender DEFAULT_RENDER = new ApiDocRender() {
        private Engine engine = new Engine("apidoc");
        private String template = "#(\"#\") #(document.value ??)\n" +
                "\n" +
                "#(document.notes ??)" +
                "\n" +
                "\n" +
                "#for(operation : document.apiOperations)" +
                "#(\"##\") #(operation.value ??)\n" +
                "\n" +
                "#(operation.notes ??)" +
                "\n" +
                "- 访问路径: `#(operation.actionKey ??)`\n" +
                "- 数据类型: `#(operation.contentType.value ??)`\n" +
                "#if(operation.hasParameter())" +
                "- 参数：\n" +
                "\n" +
                "  | 名称 | 参数 | 是否必须 | 提交方式 | 描述 |  \n" +
                "  | --- | --- | --- | --- | --- |\n" +
                "#for(parameter : operation.apiParameters)" +
                "  | #(parameter.value ??) | #(parameter.name ??) | #(parameter.require ? '是' : '否') | #(parameter.httpMethodsString) | #(parameter.notes ??) |  \n" +
                "#end" +
                "#end" +
                "\n" +
                "   > #(operation.paraNotes ??)" +
                "#end";

        @Override
        void render(List<ApiDocument> apiDocuments, ApiDocConfig config) {

//            engine.setBaseTemplatePath(config.getBasePathAbsolute());

            for (ApiDocument document : apiDocuments) {
                Map<String, Object> templateParas = new HashMap<>();
                templateParas.put("config", config);
                templateParas.put("document", document);

                File file = new File(config.getBasePathAbsolute(), document.getFilePath() + ".md");
                if (!file.exists()){
                    try {
                        file.getParentFile().mkdirs();
                        file.createNewFile();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                System.err.println("gen doc -----> " + file.getAbsolutePath());
                engine.getTemplateByString(template).render(templateParas, file);
            }
        }
    };


    abstract void render(List<ApiDocument> apiDocuments, ApiDocConfig config);
}

@@ -1351,32 +1351,77 @@
          %22%5Cn%22 +%0A
+                %22#if(operation.paraNotes)%22 +%0A
                 
@@ -1447,32 +1447,56 @@
 araNotes ??)%22 +%0A
+                %22#end%22+%0A
                 
@@ -1602,97 +1602,282 @@
  %7B%0A%0A
-//            engine.setBaseTemplatePath(config.getBasePathAbsolut
+            try %7B%0A                for (ApiDocument document : apiDocuments) %7B%0A                    doRender(document, config);%0A                %7D%0A            %7D catch (IOException e) %7B%0A                e.printStackTrac
 e()
-)
 ;%0A
-%0A            for 
+            %7D%0A%0A        %7D%0A%0A        private void doRender
 (Api
@@ -1893,32 +1893,57 @@
 document
- : a
+, A
 piDoc
-uments)
+Config config) throws IOException
  %7B%0A     
@@ -1949,20 +1949,16 @@
         
-    
 Map%3CStri
@@ -2006,36 +2006,32 @@
 ();%0A            
-    
 templateParas.pu
@@ -2055,36 +2055,32 @@
 g);%0A            
- 
-   
 templateParas.pu
@@ -2105,20 +2105,16 @@
 ment);%0A%0A
-    
         
@@ -2213,20 +2213,16 @@
         
- 
-   
 if (!fil
@@ -2236,44 +2236,11 @@
 s())
+ 
 %7B%0A
-                    try %7B%0A        
     
@@ -2294,32 +2294,24 @@
             
-        
 file.createN
@@ -2336,131 +2336,10 @@
     
-     
-   %7D catch (IOException e) %7B%0A                        e.printStackTrace();%0A                    %7D%0A                %7D%0A    
+%7D%0A
     
@@ -2370,12 +2370,27 @@
 ln(%22
-gen 
+Jboot generated api
 doc 
@@ -2408,24 +2408,25 @@
 file.get
-Absolute
+Canonical
 Path());
@@ -2426,20 +2426,16 @@
 ath());%0A
-    
         
@@ -2504,30 +2504,16 @@
  file);%0A
-            %7D%0A
         

add apidoc component