package com.yonge.cooleshow.teacher.controller;

import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.dao.AppVersionInfoDao;
import com.yonge.cooleshow.biz.dal.dto.search.SysMessageQueryInfo;
import com.yonge.cooleshow.biz.dal.entity.SysMessage;
import com.yonge.cooleshow.biz.dal.enums.MessageSendMode;
import com.yonge.cooleshow.biz.dal.service.SysMessageService;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.cooleshow.common.entity.Mapper;
import com.yonge.toolset.base.exception.BizException;
import com.yonge.toolset.base.page.PageInfo;
import com.yonge.toolset.thirdparty.message.MessageSenderPluginContext;
import com.yonge.toolset.utils.validator.CommonValidator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "消息服务")
@RequestMapping("sysMessage")
public class SysMessageController extends BaseController {

	@Autowired
	private SysMessageService sysMessageService;

	@Autowired
	private SysUserFeignService sysUserFeignService;

	@Autowired
	private AppVersionInfoDao appVersionInfoDao;

	@ApiOperation("获取所有消息列表")
	@PostMapping(value = "list")
	public HttpResponseResult<PageInfo<SysMessage>> list(@RequestBody SysMessageQueryInfo queryInfo){
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (sysUser == null) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}
		queryInfo.setUserId(sysUser.getId());
		queryInfo.setType(MessageSendMode.PUSH.getCode());
		queryInfo.setClientId("TEACHER");
		return succeed(sysMessageService.queryPage(queryInfo));
	}

	// @ApiOperation("获取消息分类类型")
	// @GetMapping(value = "typeList")
	// public Object typeList(String memo){
	// 	//如果用户使用的版本比当前版本号大，那么屏蔽缴费信息
	// 	if(StringUtils.isNotEmpty(memo)){
	// 		//获取当前使用的版本
	// 		AppVersionInfo appVersionInfo = appVersionInfoDao.queryNewestByPlatform("ios-student").get(0);
	// 		int defaultVersion = Integer.parseInt(appVersionInfo.getVersion().replaceAll("\\.",""));
	// 		int currentVersion = Integer.parseInt(memo.replaceAll("\\.",""));
	// 		if(currentVersion > defaultVersion){
	// 			return succeed(JSON.parseArray("[{\"value\":\"全部\",\"key\":\"ALL\"}," +
	// 					"{\"value\":\"课程信息\",\"key\":\"COURSE\"}," +
	// 					"{\"value\":\"训练信息\",\"key\":\"WORK\"}," +
	// 					"{\"value\":\"其他\",\"key\":\"DEFAULT\"}]"));
	// 		}
	// 	}
	// 	return succeed(JSON.parseArray("[{\"value\":\"全部\",\"key\":\"ALL\"}," +
	// 			"{\"value\":\"缴费信息\",\"key\":\"PAY\"}," +
	// 			"{\"value\":\"课程信息\",\"key\":\"COURSE\"}," +
	// 			"{\"value\":\"训练信息\",\"key\":\"WORK\"}," +
	// 			"{\"value\":\"其他\",\"key\":\"DEFAULT\"}]"));
	// }

	@ApiOperation("一键已读")
	@PostMapping("batchSetRead")
	public Object batchSetRead() {
		int status = 1;
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (sysUser == null) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}
		return succeed(sysMessageService.updateStatus(sysUser.getId(), status,"TEACHER") > 0 );
	}

	@ApiOperation("设置已读")
	@PostMapping("setRead/{id}")
	public Object setRead(@PathVariable Long id) {
		int status = 1;
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (sysUser == null) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}
		return succeed(sysMessageService.updateOneStatus(id, status) >0) ;
	}

	@ApiOperation(value = "查询用户未读消息条数")
	@GetMapping("/queryCountOfUnread")
	public HttpResponseResult<List<Mapper>> queryCountOfUnread() {
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (sysUser == null) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}
		List<Mapper>  map = sysMessageService.queryCountOfUnread(MessageSendMode.PUSH, sysUser.getId(), "TEACHER");

		return succeed(map);
	}

	@ApiOperation(value = "发送消息")
	@PostMapping("/sendMessage")
	public Object sendMessage(MessageSenderPluginContext.MessageSender messageSender, String content, String receiver, int readStatus, String url, String group) {
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (sysUser == null) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}
		Long userId = sysUser.getId();

		String mobileNo = sysUser.getPhone();
		if (StringUtils.isBlank(mobileNo) || !CommonValidator.isMobileNo(mobileNo)) {
			throw new BizException("请输入正确的手机号");
		}
		sysMessageService.sendMessage(messageSender, userId, "", content, receiver, null, readStatus, url, group, "TEACHER");
		return succeed(true);
	}
}
