package com.community.huiju.controller;

import com.community.commom.constant.Constant;
import com.community.commom.mode.ResponseBean;
import com.community.commom.session.UserElement;
import com.community.huiju.service.MessageServiceI;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @author FXF
 * @date 2018-10-22
 */
@RestController
@RefreshScope
@RequestMapping("/")
@Api(value = "消息相关的API",description = "消息相关的API")
public class MessageController extends BaseController {
	
	@Autowired
	private MessageServiceI messageService;
	
	@ApiOperation(value = "按小区获取消息总数", notes = "按小区获取个人消息总数")
	@ApiImplicitParams({ @ApiImplicitParam(paramType = "path", dataType = "String", name = "communityId", value = "小区Id"),
			@ApiImplicitParam(paramType = "header",dataType = "String",name = "X-Auth-Token",value = "Token") })
	@RequestMapping(value = "/message/{communityId}/total",method = RequestMethod.GET)
	public ResponseBean getMessageTotal(@PathVariable Integer communityId, HttpSession session){
		UserElement userElement = (UserElement) session.getAttribute(Constant.APP_USER_SESSION);
		ResponseBean responseBean = new ResponseBean();

		if (!check(communityId)) {
			responseBean.addError("小区不存在");
			return responseBean;
		}

		Map<String,Object> totalMap = messageService.getMessageTotal(communityId, userElement.getUserVerifyId());
		responseBean.addSuccess(totalMap);
		return responseBean;
	}
	
	@ApiOperation(value = "按小区获取分页消息", notes = "按小区获取分页消息")
	@ApiImplicitParams({ @ApiImplicitParam(paramType = "path", dataType = "String", name = "communityId", value = "小区Id"),
			@ApiImplicitParam(paramType = "query", dataType = "integer", name = "pageNum", value = "分页第几页"),
			@ApiImplicitParam(paramType = "query", dataType = "integer", name = "pageSize", value = "分页每页长度"),
			@ApiImplicitParam(paramType = "header",dataType = "String",name = "X-Auth-Token",value = "Token")})
	@RequestMapping(value = "/message/{communityId}/news",method = RequestMethod.GET)
	public ResponseBean getNews(@PathVariable Integer communityId,
	                            @RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
	                            @RequestParam(value ="pageSize",defaultValue = "5") Integer pageSize, HttpSession session){
		UserElement userElement = (UserElement) session.getAttribute(Constant.APP_USER_SESSION);
		ResponseBean responseBean = new ResponseBean();
		if (!check(communityId)) {
			responseBean.addError("小区不存在");
			return responseBean;
		}
		Map<String,Object> map = messageService.getMessages(communityId,pageNum,pageSize, Constant.MODEL_TYPE_NEWS, userElement.getUserVerifyId());
		responseBean.addSuccess(map);
		return responseBean;
	}
	
	@ApiOperation(value = "按小区获取分页待办", notes = "按小区获取分页待办")
	@ApiImplicitParams({ @ApiImplicitParam(paramType = "path", dataType = "String", name = "communityId", value = "小区Id"),
			@ApiImplicitParam(paramType = "query", dataType = "integer", name = "pageNum", value = "分页第几页"),
			@ApiImplicitParam(paramType = "query", dataType = "integer", name = "pageSize", value = "分页每页长度"),
			@ApiImplicitParam(paramType = "header",dataType = "String",name = "X-Auth-Token",value = "Token")})
	@RequestMapping(value = "/message/{communityId}/upcoming",method = RequestMethod.GET)
	public ResponseBean getUpcoming(@PathVariable Integer communityId,
	                                @RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
	                                @RequestParam(value ="pageSize",defaultValue = "5") Integer pageSize, HttpSession session){
		UserElement userElement = (UserElement) session.getAttribute(Constant.APP_USER_SESSION);
		Integer userId = userElement.getId();
		ResponseBean responseBean = new ResponseBean();
		
		if (!check(communityId)) {
			responseBean.addError("小区不存在");
			return responseBean;
		}
		Map<String,Object> map = messageService.getMessages(communityId,pageNum,pageSize,Constant.MODEL_TYPE_UPCOMING, userId);
		responseBean.addSuccess(map);
		return responseBean;
	}

	@ApiOperation(value = "消息已读接口", notes = "消息已读接口")
	@ApiImplicitParams({ @ApiImplicitParam(paramType = "path", dataType = "String", name = "communityId", value = "小区Id"),
			@ApiImplicitParam(paramType = "header",dataType = "String",name = "X-Auth-Token",value = "Token")})
	@RequestMapping(value = "/readStatus/{communityId}",method = RequestMethod.POST)
	public ResponseBean allMessageRead(@PathVariable("communityId") Integer communityId, HttpSession session){
		UserElement userElement = (UserElement) session.getAttribute(Constant.APP_USER_SESSION);
		Integer userId = userElement.getId();
		ResponseBean responseBean = new ResponseBean();

		if (!check(communityId)) {
			responseBean.addError("小区不存在");
			return responseBean;
		}

		messageService.updateMessageReadStatus(communityId, userId);
		return responseBean;
	}


	@ApiOperation(value = "根据消息id进行已读", notes = "根据消息id进行已读")
	@ApiImplicitParams({ @ApiImplicitParam(paramType = "path", dataType = "String", name = "messageId", value = "消息id"),
			@ApiImplicitParam(paramType = "header",dataType = "String",name = "X-Auth-Token",value = "Token")})
	@RequestMapping(value = "/messageIdreadStatus/{messageId}",method = RequestMethod.PUT)
	public ResponseBean updateMessageByIdReadStatus(HttpSession session, @PathVariable("messageId") Integer messageId) {
		ResponseBean responseBean = new ResponseBean();
		UserElement userElement = (UserElement) session.getAttribute(Constant.APP_USER_SESSION);
		responseBean = messageService.updateMessageByIdReadStatus(userElement, messageId);
		return responseBean;
	}


}
