package io.craigmiller160.school.controller;

import java.util.List;

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 org.springframework.web.bind.annotation.RequestParam;

import io.craigmiller160.school.entity.Course;
import io.craigmiller160.school.persist.GenericPaginatedJoinHolderService;

@Controller
@RequestMapping (value="/admin/course")
public class AdminCourseController {

	private final GenericPaginatedJoinHolderService service;
	
	@Autowired (required=true)
	public AdminCourseController(GenericPaginatedJoinHolderService service){
		this.service = service;
	}
	
	@RequestMapping(value="/all", method=RequestMethod.GET)
	public String getAllCourses(Model model, @RequestParam int page){
		int firstRowVal = page * 10 - 10;
		List<Course> courses = service.getNextEntities(
				Course.class, firstRowVal, 10);
		model.addAttribute("courses", courses);
		model.addAttribute("page", page);
		
		return "courses";
	}
	
	@RequestMapping (value="/new", method=RequestMethod.GET)
	public String createNewCourse(Course course){
		return "course-form";
	}
	
	@RequestMapping (value="/new", method=RequestMethod.PUT)
	public String insertCourse(Course course){
		service.insertEntity(course);
		
		return "redirect:/admin/course/all.html?page=1";
	}
	
	@RequestMapping (value="/{courseId}", method=RequestMethod.GET)
	public String editCourse(Model model, Course course,
			@PathVariable ("courseId") String courseId){
		//TODO need a way to handle NumberFormatException here
		int id = Integer.parseInt(courseId);
		course = service.getEntityById(Course.class, id);
		model.addAttribute("course", course);
		
		return "course-form";
	}
	
	@RequestMapping (value="/{courseId}", method=RequestMethod.POST)
	public String updateCourse(Course course, Model model,
			@PathVariable ("courseId") String courseId,
			@RequestParam (required=false) String cancel){
		if(cancel == null){
			service.updateEntity(course);
		}
		
		return "redirect:/admin/course/all.html?page=1";
	}
	
	//TODO finish delete operation
	@RequestMapping (value="/{courseId}", method=RequestMethod.DELETE)
	public String deleteCourse(@PathVariable ("courseId") String courseId){
		System.out.println("Delete CourseID: " + courseId);
		
		return null;
	}
	
	
	
}

@@ -2274,40 +2274,8 @@
 %7D%0A%09%0A
-%09//TODO finish delete operation%0A
 %09@Re
@@ -2417,76 +2417,271 @@
 %7B%0A%09%09
-System.out.println(%22Delete CourseID: %22 + courseId);%0A%09%09%0A%09%09return null
+//TODO need deleteByID method%0A%09%09//TODO NumberFormatException needs to be handled%0A%09%09int id = Integer.parseInt(courseId);%0A%09%09Course course = service.getEntityById(Course.class, id);%0A%09%09service.deleteEntity(course);%0A%09%09%0A%09%09return %22redirect:/admin/course/all.html?page=1%22
 ;%0A%09%7D

Delete Course Operation Now Works