package com.blueland.ebpm.rest;

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

import javax.servlet.http.HttpServletRequest;

import com.blueland.ebpm.util.RestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.blueland.bpm.common.page.Page;
import com.blueland.bpm.engine.core.model.ExceptionErrorCode;
import com.blueland.ebpm.exception.RestException;
import com.blueland.unitework.model.UniteworkTask;
import com.blueland.unitework.service.UniteworkService;

@RestController
@RequestMapping("/unitework-sso")
public class UniteworkSSOController {

	@Autowired
	private UniteworkService uniteworkService;

	@RequestMapping(value = "/myrecordlist", produces = "application/json;charset=UTF-8")
	public Page<UniteworkTask> queryUniteworkList(HttpServletRequest request,@RequestBody UniteworkTask task) {
		String userid = request.getHeader("iv-user");

		if(RestUtils.isEmpty(userid) || "unauthenticated".equalsIgnoreCase(userid)){
			throw new RestException(ExceptionErrorCode.A1000.name(), "SSO Fault.");
		}else {
			task.setRecUserId(userid);
		}
		
		if (task.getPageSize() == 0)
			task.setPageNumber(10);
		if (RestUtils.isEmpty(task.getRecUserId()))
			throw new RestException(ExceptionErrorCode.B2001.name(),	"recUserId is empty");
		if (RestUtils.isEmpty(task.getStatus()))
			throw new RestException(ExceptionErrorCode.B2001.name(),	"status is empty");
		return uniteworkService.queryTask(task);
	}

	@RequestMapping(value = "/myrecordcount", produces = "application/json;charset=UTF-8")
	public Map<String, Object> queryUniteworkCount(HttpServletRequest request, @RequestBody UniteworkTask task) {
		if (RestUtils.isEmpty(task.getRecUserId()))
			throw new RestException(ExceptionErrorCode.B2001.name(),	"recUserId is empty");
		if (RestUtils.isEmpty(task.getStatus()))
			throw new RestException(ExceptionErrorCode.B2001.name(),	"status is empty");
		int count = uniteworkService.getTaskCount(task);
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("count", count);
		return result;
	}

	@RequestMapping(value = "/syncreadwork", produces = "application/json;charset=UTF-8")
	public Map<String, Object> syncReadWork(HttpServletRequest request, @RequestBody UniteworkTask task) {
		String type = task.getStatus();
		if (type.equals("4")) {
			if (RestUtils.isEmpty(task.getRecUserId())) {
				throw new RestException(ExceptionErrorCode.B2001.name(),	"recUserId is null");
			}
			if (RestUtils.isEmpty(task.getId())) {
				throw new RestException(ExceptionErrorCode.B2001.name(),	"id is null");
			}
			uniteworkService.insertRead(task);
		}else if(type.equals("5")) {
			uniteworkService.changeToRead(task.getId());			
		}else if(type.equals("7")) {
			uniteworkService.deleteRead(task.getId());
		}
		
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("result", "OK");
		return result;
	}
}
