package com.eactive.eai.rms.onl.manage.comm.lifecycle;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.SortDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;

import com.eactive.eai.rms.common.base.OnlBaseAnnotationController;
import com.eactive.eai.rms.common.combo.ComboService;
import com.eactive.eai.rms.common.combo.ComboVo;
import com.eactive.eai.rms.common.vo.GridResponse;

@Controller
public class LifecycleController extends OnlBaseAnnotationController {

	@Autowired
	private LifecycleManService lifecycleManService;

	@Autowired
	private ComboService comboService;

	@GetMapping(value = "/onl/admin/common/lifecycleMan.view")
	public void view() {
		// view
	}

	@GetMapping(value = "/onl/admin/common/lifecycleMan.view", params = "cmd=DETAIL")
	public String detailView() {
		return "/onl/admin/common/lifecycleManDetail";
	}

	@PostMapping(value = "/onl/admin/common/lifecycleMan.json", params = "cmd=LIST")
	public ResponseEntity<GridResponse<LifecycleUI>> selectList(
			@SortDefault(sort = { "lodinseq", "lifecyclclsname" }) Pageable pageable) {
		Page<LifecycleUI> page = lifecycleManService.selectList(pageable);
		return ResponseEntity.ok(new GridResponse<>(page));
	}

	@PostMapping(value = "/onl/admin/common/lifecycleMan.json", params = "cmd=LIST_INIT_COMBO")
	public ModelAndView initCombo(HttpServletRequest request, HttpServletResponse response) {
		List<ComboVo> listLifeCyclUseDstcdRows = comboService.getFromCode("LIFE_CYCL_TYPE");
		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("lifeCyclUseDstcdRows", listLifeCyclUseDstcdRows);
		return new ModelAndView("jsonView", resultMap);
	}

	@PostMapping(value = "/onl/admin/common/lifecycleMan.json", params = "cmd=DETAIL")
	public ResponseEntity<LifecycleUI> selectDetail(String lifeCyclClsName) {
		LifecycleUI lifecycleUI = lifecycleManService.selectDetail(lifeCyclClsName);
		return ResponseEntity.ok(lifecycleUI);
	}

	@PostMapping(value = "/onl/admin/common/lifecycleMan.json", params = "cmd=INSERT")
	public ResponseEntity<?> insert(@Valid LifecycleUI lifecycleUI, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			Map<String, Object> errors = new HashMap<>();
			for (FieldError error : bindingResult.getFieldErrors()) {
				errors.put(error.getField(), error.getDefaultMessage());
			}
			return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errors);
		}
		lifecycleManService.insert(lifecycleUI);
		return ResponseEntity.ok().build();
	}

	@PostMapping(value = "/onl/admin/common/lifecycleMan.json", params = "cmd=UPDATE")
	public ResponseEntity<?> update(@Valid LifecycleUI lifecycleUI, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			Map<String, Object> errors = new HashMap<>();
			for (FieldError error : bindingResult.getFieldErrors()) {
				errors.put(error.getField(), error.getDefaultMessage());
			}
			return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errors);
		}
		lifecycleManService.update(lifecycleUI);
		return ResponseEntity.ok().build();
	}

	@PostMapping(value = "/onl/admin/common/lifecycleMan.json", params = "cmd=DELETE")
	public ResponseEntity<Void> delete(LifecycleUI lifecycleUI) {
		lifecycleManService.delete(lifecycleUI.getLifeCyclClsName());
		return ResponseEntity.ok().build();
	}
}