package ai.susi.server.api.cms;

import ai.susi.DAO;
import ai.susi.json.JsonObjectWithDefault;
import ai.susi.server.*;
import org.json.JSONObject;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;

/**
 * Servlet to load an model from the expert database
 * i.e.
 * http://localhost:4000/cms/getmodel.txt
 */
public class ModelListService extends AbstractAPIHandler implements APIHandler  {


    @Override
    public BaseUserRole getMinimalBaseUserRole() { return BaseUserRole.ANONYMOUS; }

    @Override
    public JSONObject getDefaultPermissions(BaseUserRole baseUserRole) {
        return null;
    }

    @Override
    public String getAPIPath() {
        return "/cms/getmodel.txt";
    }

    @Override
    public ServiceResponse serviceImpl(Query call, HttpServletResponse response, Authorization rights, final JsonObjectWithDefault permissions) {

            String[] models = DAO.model_watch_dir.list(new FilenameFilter() {
                @Override
                public boolean accept(File current, String name) {
                    return new File(current, name).isDirectory();
                }
            });
            String content = Arrays.toString(models);
            return new ServiceResponse(content);
    }
}

@@ -114,16 +114,43 @@
 rver.*;%0A
+import org.json.JSONArray;%0A
 import o
@@ -1298,40 +1298,45 @@
     
-String content = Arrays.toString
+JSONArray modelsArray = new JSONArray
 (mod
@@ -1384,15 +1384,19 @@
 nse(
-content
+modelsArray
 );%0A 

string array as JsonArray