package pingis.controllers;

import java.util.LinkedHashMap;

import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.view.RedirectView;
import pingis.entities.ImplementationType;
import pingis.entities.Task;
import pingis.entities.Challenge;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import pingis.services.ChallengeService;
import pingis.services.EditorService;
import pingis.services.TaskService;
import pingis.utils.EditorTabData;
import pingis.utils.JavaClassGenerator;
import pingis.utils.JavaSyntaxChecker;

@Controller
public class TaskController {

    @Autowired
    ChallengeService challengeService;
    @Autowired
    TaskService taskService;
    @Autowired
    EditorService editorService;

    @RequestMapping(value = "/task/{challengeId}/{taskId}", method = RequestMethod.GET)
    public String task(Model model,
                       @PathVariable Long challengeId,
                       @PathVariable int taskId) {

        Challenge currentChallenge = challengeService.findOne(challengeId);
        Task currentTask = taskService.findTaskInChallenge(challengeId, taskId);
        LinkedHashMap<String, EditorTabData> editorContents = editorService.generateContent(currentTask);

        model.addAttribute("challenge", currentChallenge);
        model.addAttribute("task", currentTask);
        model.addAttribute("editorContents", editorContents);

        String implFileName = JavaClassGenerator.generateImplClassFilename(currentChallenge);
        String testFileName = JavaClassGenerator.generateTestClassFilename(currentChallenge);

        model.addAttribute("implementationFileName", implFileName);
        model.addAttribute("testFileName", testFileName);

        String implCode = "TODO impl";
        String testCode = "TODO test";

        if (currentTask.getType() == ImplementationType.IMPLEMENTATION) {
            implCode = currentTask.getCodeStub();
        } else {
            testCode = currentTask.getCodeStub();
        }

        model.addAttribute("implementationCode", implCode);
        model.addAttribute("testCode", testCode);

        return "task";
    }

    @RequestMapping(value = "/task", method = RequestMethod.POST)
    public RedirectView task(String implementationCode,
                             String testCode,
                             Long challengeId,
                             Integer taskId,
                             RedirectAttributes redirectAttributes) {

        redirectAttributes.addAttribute("challengeId", challengeId);
        redirectAttributes.addAttribute("taskId", taskId);

        String[] syntaxErrors = JavaSyntaxChecker.parseCode(implementationCode);

        if (syntaxErrors == null) {
            return new RedirectView("/feedback");
        }

        redirectAttributes.addFlashAttribute("errors", syntaxErrors);
        redirectAttributes.addFlashAttribute("code", "");

        return new RedirectView("/task/{challengeId}/{taskId}");
    }


    @RequestMapping("/feedback")
    public String feedback(Model model) {
        model.addAttribute("feedback", "Good work!");
        return "feedback";
    }


}

@@ -38,28 +38,167 @@
 ava.
-util.LinkedHashMap;%0A
+io.IOException;%0Aimport java.util.HashMap;%0Aimport java.util.LinkedHashMap;%0Aimport java.util.Map;%0A%0Aimport org.apache.commons.compress.archivers.ArchiveException;
 %0Aimp
@@ -783,71 +783,30 @@
 gis.
-services.ChallengeService;%0Aimport pingis.services.EditorService
+entities.TmcSubmission
 ;%0Aim
@@ -826,27 +826,17 @@
 ervices.
-TaskService
+*
 ;%0Aimport
@@ -1134,16 +1134,156 @@
 rvice;%0A%0A
+    @Autowired%0A    private SubmissionPackagingService packagingService;%0A%0A    @Autowired%0A    private SubmissionSenderService senderService;%0A%0A
     @Req
@@ -3005,16 +3005,53 @@
 ributes)
+ throws IOException, ArchiveException
  %7B%0A%0A    
@@ -3187,60 +3187,287 @@
     
-String%5B%5D syntaxErrors = JavaSyntaxChecker.parse
+Challenge currentChallenge = challengeService.findOne(challengeId);%0A        Task currentTask = taskService.findTaskInChallenge(challengeId, taskId);%0A%0A        String checkedCode;%0A%0A        if (currentTask.getType() == ImplementationType.IMPLEMENTATION) %7B%0A            checked
 Code
-(
+ = 
 impl
@@ -3484,106 +3484,157 @@
 Code
-)
 ;%0A
-%0A        if (syntaxErrors == null) %7B%0A            return new RedirectView(%22/feedback%22
+        %7D else %7B%0A            checkedCode = testCode;%0A        %7D%0A%0A        String%5B%5D syntaxErrors = JavaSyntaxChecker.parseCode(checkedCode
 );%0A
+%0A
         
 %7D%0A%0A 
@@ -3621,35 +3621,64 @@
 Code);%0A%0A        
-%7D%0A%0A
+if (syntaxErrors != null) %7B%0A    
         redirect
@@ -3731,32 +3731,36 @@
 rrors);%0A        
+    
 redirectAttribut
@@ -3792,14 +3792,27 @@
 e%22, 
-%22%22);%0A%0A
+checkedCode);%0A%0A    
     
@@ -3868,24 +3868,656 @@
 %7BtaskId%7D%22);%0A
+        %7D%0A%0A        Map%3CString, byte%5B%5D%3E files = new HashMap%3C%3E();%0A%0A        String implFileName = JavaClassGenerator.generateImplClassFilename(currentChallenge);%0A        String testFileName = JavaClassGenerator.generateTestClassFilename(currentChallenge);%0A%0A        files.put(implFileName, implementationCode.getBytes());%0A        files.put(testFileName, testCode.getBytes());%0A%0A        byte%5B%5D packaged = packagingService.packageSubmission(files);%0A        TmcSubmission submission = senderService.sendSubmission(packaged);%0A%0A        redirectAttributes.addAttribute(%22submission%22, submission);%0A%0A        return new RedirectView(%22/feedback%22);%0A
     %7D%0A%0A%0A    

Make TaskController actually send stuff to the sandbox.