package wepa.wepa.controller;

import java.util.Arrays;
import java.util.List;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import wepa.wepa.domain.Course;
import wepa.wepa.domain.Person;
import wepa.wepa.repository.CourseRepository;
import wepa.wepa.repository.PersonRepository;
import wepa.wepa.service.HelperService;
import wepa.wepa.service.LogService;
import wepa.wepa.service.PersonService;

@Controller
@RequestMapping("/assistants")
public class AssistantController {

    @Autowired
    private PersonRepository personRepository;

    @Autowired
    private CourseRepository courseRepository;

    @Autowired
    private PersonService personService;

    @Autowired
    private LogService logService;

    @Autowired
    private HelperService paginationService;

    @RequestMapping()
    public String defaultAssistantPage() {
        return "redirect:/";
    }

    @Secured({"ROLE_TEACHER"})
    @RequestMapping(value = "/courses/{courseId}/add/{personId}", method = RequestMethod.POST)
    public String addAssistant(@PathVariable Long courseId, @PathVariable Long personId) {

        Person person = personRepository.findOne(personId);
        Course course = courseRepository.findOne(courseId);
        if (person != null && course != null) {
            List<Course> courses = person.getCoursesAssisted();
            courses.add(course);
            person.setCoursesAssisted(courses);
            List<String> authorities = person.getAuthorities();
            authorities.add("ASSISTANT");
            person.setAuthorities(authorities);
            personRepository.save(person);

            Authentication auth = SecurityContextHolder.getContext().getAuthentication();

            if (auth != null) {
                Person loggedIn = personRepository.findByName(auth.getName());

                logService.info("Person \"" + person.getName() + "\" was added by " + loggedIn.getName() + " (" + loggedIn.getId() + ") as assistant on course " + course.getName());
            } else {
                logService.info("Person \"" + person.getName() + "\" was added as assistant on course " + course.getName() + ".");
            }

            return "redirect:/courses/" + course.getId();
        }

        return "redirect:/courses";
    }

    @Secured({"ROLE_TEACHER"})
    @RequestMapping(value = "/courses/{courseId}/remove/{personId}", method = RequestMethod.POST)
    public String removeAssistant(@PathVariable Long courseId, @PathVariable Long personId) {

        Person person = personRepository.findOne(personId);
        Course course = courseRepository.findOne(courseId);
        if (person != null && course != null) {

            List<Person> assistants = course.getAssistants();
            assistants.remove(person);
            course.setAssistants(assistants);
            courseRepository.save(course);

            List<Course> courses = person.getCoursesAssisted();
            courses.remove(course);
            person.setCoursesAssisted(courses);
            if (courses.isEmpty()) {
                List<String> authorities = person.getAuthorities();
                authorities.remove("ASSISTANT");
                person.setAuthorities(authorities);
            }
            personRepository.save(person);

            Authentication auth = SecurityContextHolder.getContext().getAuthentication();

            if (auth != null) {
                Person loggedIn = personRepository.findByName(auth.getName());

                logService.info("Person \"" + person.getName() + "\" was removed by " + loggedIn.getName() + " (" + loggedIn.getId() + ") as assistant on course " + course.getName());
            } else {
                logService.info("Person \"" + person.getName() + "\" was removed as assistant on course " + course.getName() + ".");
            }

            return "redirect:/courses/" + course.getId();
        }

        return "redirect:/courses";
    }

}

@@ -1935,32 +1935,221 @@
 urse != null) %7B%0A
+%0A            List%3CPerson%3E assistants = course.getAssistants();%0A            assistants.add(person);%0A            course.setAssistants(assistants);%0A            courseRepository.save(course);%0A%0A
             List
@@ -2491,546 +2491,52 @@
     
-Authentication auth = SecurityContextHolder.getContext().getAuthentication();%0A%0A            if (auth != null) %7B%0A                Person loggedIn = personRepository.findByName(auth.getName());%0A%0A                logService.info(%22Person %5C%22%22 + person.getName() + %22%5C%22 was added by %22 + loggedIn.getName() + %22 (%22 + loggedIn.getId() + %22) as assistant on course %22 + course.getName());%0A            %7D else %7B%0A                logService.info(%22Person %5C%22%22 + person.getName() + %22%5C%22 was added as assistant on course %22 + course.getName() + %22.%22);%0A            %7D
+logAssistantChange(%22added%22, person, course);
 %0A%0A  
@@ -3637,32 +3637,284 @@
 e(person);%0A%0A    
+        logAssistantChange(%22removed%22, person, course);%0A%0A            return %22redirect:/courses/%22 + course.getId();%0A        %7D%0A%0A        return %22redirect:/courses%22;%0A    %7D%0A%0A    private void logAssistantChange(String replace, Person person, Course course) %7B%0A
         Authenti
@@ -3988,28 +3988,24 @@
 );%0A%0A        
-    
 if (auth != 
@@ -4020,28 +4020,24 @@
             
-   
- 
 Person logge
@@ -4092,36 +4092,32 @@
 );%0A%0A            
-    
 logService.info(
@@ -4157,23 +4157,31 @@
 %22%5C%22 was 
-removed
+%22 + replace + %22
  by %22 + 
@@ -4284,20 +4284,16 @@
         
-    
 %7D else %7B
@@ -4297,36 +4297,32 @@
 e %7B%0A            
- 
-   
 logService.info(
@@ -4362,23 +4362,31 @@
 %22%5C%22 was 
-removed
+%22 + replace + %22
  as assi
@@ -4442,119 +4442,9 @@
     
-    %7D%0A%0A            return %22redirect:/courses/%22 + course.getId();%0A        %7D%0A%0A        return %22redirect:/courses%22;
+%7D
 %0A   

refactor and add save