package com.ym.mec.teacher.controller;

import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dto.ImUserFriendDto;
import com.ym.mec.biz.dal.page.ImGroupNoticeQueryInfo;
import com.ym.mec.biz.service.ImGroupNoticeService;
import com.ym.mec.biz.service.ImGroupService;
import com.ym.mec.biz.service.ImUserFriendService;
import com.ym.mec.biz.service.SysUserService;
import com.ym.mec.common.controller.BaseController;
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.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.stream.Collectors;

@RequestMapping("imGroup")
@Api(tags = "IM群服务")
@RestController
public class ImGroupController extends BaseController {

	@Autowired
	private ImGroupService imGroupService;

	@Autowired
	private ImUserFriendService imUserFriendService;

	@Autowired
	private ImGroupNoticeService imGroupNoticeService;

	@Autowired
	private SysUserService userLoginService;

	@ApiOperation("查询群列表")
	@GetMapping(value = "/queryGroupList")
	public Object queryGroupList(String search,String groupType) {
		return succeed(imGroupService.queryByUserId(userLoginService.getUserId(), search,groupType));
	}

	@ApiOperation("查询群详情")
	@GetMapping(value = "/queryGroupDetail")
	public Object queryGroupDetail(String imGroupId) {
		if (imGroupId.contains("S") || imGroupId.contains("I")){
			imGroupId = imGroupId.substring(1);
		}
		return succeed(imGroupService.get(imGroupId));
	}

	@ApiOperation("查询群成员列表")
	@GetMapping(value = "/queryGroupMemberList")
	public Object queryGroupMemberList(String imGroupId) {
		return succeed(imGroupService.queryMemberById(imGroupId));
	}

	@ApiOperation("查询群学生列表")
	@GetMapping(value = "/queryGroupStudentList")
	public Object queryGroupStudentList(String imGroupId) {
		return succeed(imGroupService.queryMemberById(imGroupId).stream().filter(e -> StringUtils.isBlank(e.getRoleType())).collect(Collectors.toList()));
	}

	@ApiOperation("查询群成员详情")
	@GetMapping(value = "/queryGroupMemberDetail")
	public Object queryGroupMemberDetail(String imGroupId, Integer userId) {
		return succeed(imGroupService.queryMember(imGroupId, userId));
	}

	@ApiOperation("查询好友详情")
	@GetMapping(value = "/queryFriendDetail")
	public Object queryFriendDetail(Integer userId) {
		ImUserFriendDto dto = imUserFriendService.queryFriendDetail(userLoginService.getUserId(), userId);
		if (dto == null) {
			dto = new ImUserFriendDto();
			SysUser user = userLoginService.queryUserById(userId);
			dto.setFriend(user);
			if (user.getUserType().contains("STUDENT")) {
				dto.setFriendNickname(user.getUsername());
			} else {
				dto.setFriendNickname(user.getRealName());
			}
			dto.setFriendId(userId);
		}
		return succeed(dto);
	}

	@ApiOperation("查询好友列表")
	@GetMapping(value = "/queryFriendList")
	public Object queryFriendList(String search) {
		return succeed(imUserFriendService.queryFriendListByUserId(userLoginService.getUserId(), search));
	}

	@ApiOperation("查询好友中的学生列表")
	@GetMapping(value = "/queryFriendStudentList")
	public Object queryFriendStudentList(String search) {
		return succeed(imUserFriendService.queryFriendListByUserId(userLoginService.getUserId(), search).stream().filter(e -> StringUtils.isBlank(e.getTags()))
				.collect(Collectors.toList()));
	}

	@ApiOperation("查询群公告列表")
	@GetMapping(value = "/queryNoticeList")
	public Object queryNoticeList(ImGroupNoticeQueryInfo queryInfo) {
		return succeed(imGroupNoticeService.queryPage(queryInfo));
	}
}
