package ai.susi.server.api.cms;

import ai.susi.DAO;
import ai.susi.json.JsonObjectWithDefault;
import ai.susi.server.*;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.json.JSONObject;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

/**
 * Created by saurabh on 7/6/17.
 * This Service creates an skill as per given query.
 * The skill name given in the query should not exist in the SUSI intents Folder
 * Can be tested on :-
 * http://localhost:4000/cms/createSkill.txt?model=general&group=knowledge&language=en&skill=whois
 */
public class CreateSkillService extends AbstractAPIHandler implements APIHandler {


    private static final long serialVersionUID = 2461878194569824151L;

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

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

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

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

        String model_name = call.get("model", "general");
        File model = new File(DAO.model_watch_dir, model_name);
        String group_name = call.get("group", "knowledge");
        File group = new File(model, group_name);
        String language_name = call.get("language", "en");
        File language = new File(group, language_name);
        String skill_name = call.get("skill", "whois");
        File skill = new File(language, skill_name + ".txt");
        String skillName = skill.getName();
        JSONObject json = new JSONObject(true);

        json.put("accepted", false);
        if (skill.exists()) {
            json.put("message", "The '" + skill + "' already exists.");
        } else {
            try {
                skill.createNewFile();
                json.put("created_file", skillName);
                json.put("accepted", true);
                // Add to git
                FileRepositoryBuilder builder = new FileRepositoryBuilder();
                Repository repository = null;
                try {

                    repository = builder.setGitDir((DAO.susi_skill_repo))
                            .readEnvironment() // scan environment GIT_* variables
                            .findGitDir() // scan up the file system tree
                            .build();

                    try (Git git = new Git(repository)) {
                        git.add()
                                .addFilepattern(skill_name)
                                .call();
                        // commit the changes
                        git.commit()
                                .setMessage("Created " + skill_name)
                                .call();

                    } catch (GitAPIException e) {
                        e.printStackTrace();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } catch (IOException e) {
                e.printStackTrace();
                json.put("message", "Unable to create skill.");
            }
        }

        return new ServiceResponse(json);

    }
}
@@ -390,20 +390,78 @@
 java.io.
-File
+BufferedWriter;%0Aimport java.io.File;%0Aimport java.io.FileWriter
 ;%0Aimport
@@ -775,16 +775,34 @@
 ll=whois
+&content=skillData
 %0A */%0Apub
@@ -882,17 +882,16 @@
 dler %7B%0A%0A
-%0A
     priv
@@ -1051,17 +1051,12 @@
 ole.
-ANONYMO
 US
+ER
 ;%0A  
@@ -1826,15 +1826,12 @@
 l%22, 
-%22whois%22
+null
 );%0A 
@@ -1891,24 +1891,25 @@
 .txt%22);%0A
+%0A
         
 String s
@@ -1904,43 +1904,38 @@
     
-String skillName = skill.getName();
+// Checking for file existence
 %0A   
@@ -1976,16 +1976,11 @@
 ect(
-true
 );%0A
-%0A
     
@@ -2039,17 +2039,16 @@
 xists())
- 
 %7B%0A      
@@ -2122,16 +2122,24 @@
        %7D
+%0A       
  else %7B%0A
@@ -2154,53 +2154,92 @@
     
-try %7B%0A                skill.createNewFile(
+// Reading Content for skill%0A            String content = call.get(%22content%22, %22%22
 );%0A
+%0A
     
@@ -2250,93 +2250,125 @@
     
-    json.put(%22created_file%22, skillName);%0A                json.put(%22accepted%22, true
+// Writing to File%0A            try (FileWriter file = new FileWriter(skill)) %7B%0A                file.write(content
 );%0A
+%0A
     
@@ -2381,17 +2381,16 @@
       //
- 
 Add to g
@@ -2537,17 +2537,16 @@
   try %7B%0A
-%0A
         
@@ -3190,24 +3190,134 @@
   .call();%0A%0A
+                        json.put(%22accepted%22, true);%0A                        return new ServiceResponse(json);%0A
             
@@ -3638,35 +3638,37 @@
 %22, %22
-Unable to create skill.%22
+error: %22 + e.getMessage()
 );%0A
+%0A
     
@@ -3683,25 +3683,24 @@
 %7D%0A        %7D%0A
-%0A
         retu
@@ -3729,16 +3729,15 @@
 (json);%0A
-%0A
     %7D%0A%7D

Modify CreateSkillService to accept content. Also Change base user role to USER (#444)