package th.go.dss.mstq.controller.rest;

import java.util.Iterator;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.web.bind.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;

import th.go.dss.mstq.model.IndustryCategory;
import th.go.dss.mstq.model.onestop.Location;
import th.go.dss.mstq.security.model.CurrentUser;
import th.go.dss.mstq.security.model.MstqUserDetails;
import th.go.dss.mstq.security.model.User;
import th.go.dss.mstq.service.MstqService;
import th.go.dss.mstq.webui.ResponseJSend;

@RestController
@RequestMapping("/service/IndustryCategory")  
public class IndustryCategoryRestController {
	
	private Log log = LogFactory.getLog(this.getClass());
	
	
	@Autowired
	private MstqService mstqService;
	

	@RequestMapping(value = "/search/page/{pageNum}", method = {RequestMethod.POST}) 
	public ResponseJSend<Page<IndustryCategory>> findByExample(
			@RequestBody JsonNode node, @PathVariable Integer pageNum) throws JsonMappingException {

		return mstqService.findIndustryCategoryByExample(node, pageNum);
	}

	@RequestMapping(value= "/", method = {RequestMethod.GET})
	public Iterable<IndustryCategory> findAll() {
		return mstqService.findAllIndustryCategory();
	}
	
	@RequestMapping(value= "/{id}", method = {RequestMethod.GET})
	public IndustryCategory findById(@PathVariable Long id) {
		return mstqService.findIndustryCategoryById(id);
	}
	
	@RequestMapping(value= "/{id}", method = {RequestMethod.PUT})
	public ResponseJSend<Long> updateById(@RequestBody JsonNode node, @AuthenticationPrincipal MstqUserDetails activeUser) throws JsonMappingException {
		return mstqService.saveIndustryCategory(node, activeUser);
	}
	
	@RequestMapping(value= "/{id}", method = {RequestMethod.DELETE})
	public ResponseJSend<Long> deleteById(
			@PathVariable Long id) {
		return mstqService.deleteIndustryCategory(id);
	}
	
	
	@RequestMapping(value = "", method = {RequestMethod.POST, RequestMethod.PUT}) 
	public ResponseJSend<Long> save(@CurrentUser @AuthenticationPrincipal MstqUserDetails activeUser, @RequestBody JsonNode node) throws JsonMappingException {
		
		return this.mstqService.saveLocation(node, activeUser);
	}
	
	
}
