package com.eactive.eai.rms.common.acl.audit.adapter.in.web;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.SessionAttribute;

import com.eactive.eai.rms.common.acl.audit.port.in.SelectListAuditLogCommand;
import com.eactive.eai.rms.common.acl.audit.port.in.SelectListAuditLogUseCase;
import com.eactive.eai.rms.common.combo.ComboService;
import com.eactive.eai.rms.common.combo.ComboVo;
import com.eactive.eai.rms.common.login.LoginVo;
import com.eactive.eai.rms.common.util.CommonConstants;
import com.eactive.eai.rms.common.vo.GridResponse;

import lombok.RequiredArgsConstructor;

@Controller
@RequiredArgsConstructor
public class AuditLogManListController {

	private final SelectListAuditLogUseCase selectListAuditLogUseCase;

	private final AuditLogManMapper auditLogManMapper;

	private final ComboService comboService;

	@GetMapping(value = "/common/acl/audit/auditLogMan.view")
	public String viewList(@SessionAttribute(name = CommonConstants.LOGIN) LoginVo loginVo, Model model) {
		List<ComboVo> serviceTypeCombos = comboService.getFromServiceType(loginVo.getUseServiceTypes());
		model.addAttribute("serviceTypeCombos", serviceTypeCombos);

		return "/common/acl/audit/auditLogMan";
	}

	@PostMapping(value = "/common/acl/audit/auditLogMan.json", params = "cmd=LIST")
	public ResponseEntity<GridResponse<AuditLogManSelectListUI>> selectList(Pageable pageable,
			SelectListAuditLogCommand selectListAuditLogCommand) {

		Page<AuditLogManSelectListUI> page = selectListAuditLogUseCase
				.selectList(pageable, selectListAuditLogCommand)
				.map(auditLogManMapper::map);
		return ResponseEntity.ok(new GridResponse<>(page));
	}

}
