package kr.co.swh.lecture.opensource.server.webservice;

import java.util.Collection;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.HttpMethod;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.google.common.collect.Lists;

import kr.co.swh.lecture.opensource.server.Lecture;


/**
 * <pre>
 * kr.co.swh.lecture.opensource.server.webservice 
 * LectureService.java
 *
 * 설명 :	LectureService 웹서비스
 * </pre>
 * 
 * @since : 2018. 2. 1.
 * @author : tobby48
 * @version : v1.0
 */
@Path("/service")
public class LectureService {

	private static final Logger logger = LogManager.getLogger(LectureService.class.getName());
	
	private ObjectMapper mapper = new ObjectMapper();
	private ObjectWriter writer = mapper.writerWithDefaultPrettyPrinter();
	public static final String WEBSERVICE_IN_CONTEXTS = "[Method {}] {}";
	
	public LectureService() {
		// TODO Auto-generated constructor stub
	}
	
	@GET
	@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
	public Collection<Lecture> select() {
		logger.info(WEBSERVICE_IN_CONTEXTS, HttpMethod.GET, "select()");
		Lecture l = new Lecture();
		l.setId("1");
		l.setName("SWH");
		List<Lecture> list = Lists.newArrayList();
		list.add(l);
		return list;
	}
	
	@GET
	@Path("/{id}")
	@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
	public Lecture select(@PathParam("id") String id) {
		logger.info(WEBSERVICE_IN_CONTEXTS, HttpMethod.GET, "select()");
		return new Lecture();
	}
	
	@POST
	@Consumes(MediaType.APPLICATION_JSON + ";charset=utf-8")
	@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
	public Lecture insert(Lecture obj) throws JsonProcessingException {
		logger.info(WEBSERVICE_IN_CONTEXTS, HttpMethod.POST);
		logger.info(writer.writeValueAsString(obj));
		return obj;
	}
	
	@PUT
	@Consumes(MediaType.APPLICATION_JSON + ";charset=utf-8")
	@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
	public Lecture update(Lecture obj) throws JsonProcessingException {
		logger.info(WEBSERVICE_IN_CONTEXTS, HttpMethod.PUT);
		return obj;
	}

	@DELETE
	@Path("/{id}")
	@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
	public Lecture delete(@PathParam("id") String id) {
		logger.info(WEBSERVICE_IN_CONTEXTS, HttpMethod.DELETE);
		return new Lecture();
	}

}