package com.ym.mec.web.controller;

import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dto.ImGroupDto;
import com.ym.mec.biz.dal.dto.ImGroupMemberDto;
import com.ym.mec.biz.dal.dto.ImUserFriendDto;
import com.ym.mec.biz.dal.dto.NameDto;
import com.ym.mec.biz.dal.entity.ImGroup;
import com.ym.mec.biz.dal.page.ImGroupNoticeQueryInfo;
import com.ym.mec.biz.dal.page.ImGroupQueryInfo;
import com.ym.mec.biz.service.*;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import com.ym.mec.common.page.PageInfo;
import com.yonge.log.model.AuditLogAnnotation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
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 ImGroupMemberService imGroupMemberService;

	@Autowired
	private ImGroupNoticeService imGroupNoticeService;

	@Autowired
	private SysUserService sysUserService;

	@ApiOperation("查询群列表")
	@GetMapping(value = "/queryGroupList")
	public HttpResponseResult<List<ImGroup>> queryGroupList(String search,String groupType) {
		return succeed(imGroupService.queryByUserId(sysUserService.getUserId(), search,groupType));
	}

	@ApiOperation("创建群聊")
	@PostMapping(value = "/createGroup")
	public HttpResponseResult queryGroupList(@RequestBody ImGroupDto imGroupDto) {
		return succeed(imGroupService.createGroup(imGroupDto));
	}

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

	@ApiOperation("修改群信息")
	@PostMapping(value = "/updateImGroup")
	public HttpResponseResult updateImGroup(@RequestBody ImGroup imGroup) {
		imGroup.setUpdateTime(new Date());
		imGroupService.updateImGroup(imGroup);
		return succeed();
	}

	@ApiOperation("修改群成员信息")
	@PostMapping(value = "/updateNickName")
	public HttpResponseResult updateNickName(Integer userId,String nickName) {
		imGroupService.updateNickName1(userId,nickName);
		return succeed();
	}

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

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

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

	@ApiOperation("查询好友详情")
	@GetMapping(value = "/queryFriendDetail")
	public HttpResponseResult<ImUserFriendDto> queryFriendDetail(Integer userId) {
		Integer currentUserId = sysUserService.getUserId();
		ImUserFriendDto dto = imUserFriendService.queryFriendDetail(currentUserId, userId);
		if (dto == null) {
			dto = new ImUserFriendDto();
			SysUser user = sysUserService.queryUserById(userId);
			dto.setFriend(user);
			//这里由原来的优先取老师的realName改成了优先取学员的username，
			// 因为管理员在修改通讯录备注的时候，如果是多角色，优先改的是学员的username
			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 HttpResponseResult<List<ImUserFriendDto>> queryFriendList(String search) {
		return succeed(imUserFriendService.queryFriendListByUserId(sysUserService.getUserId(), search));
	}

	@ApiOperation("查询好友中的学生列表")
	@GetMapping(value = "/queryFriendStudentList")
	public HttpResponseResult<List<ImUserFriendDto>> queryFriendStudentList(String search) {
		return succeed(imUserFriendService.queryFriendListByUserId(sysUserService.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));
	}

	@ApiOperation("添加群成员(单个用户)")
	@PostMapping(value = "/addGroupMember")
	@ApiImplicitParams({ @ApiImplicitParam(name = "imGroupId", value = "群编号", required = true, dataType = "String"),
			@ApiImplicitParam(name = "roleType", value = "指导老师,乐队指导,乐团主管,运营主管,学员不传", required = true, dataType = "String"),
			@ApiImplicitParam(name = "isAdmin", value = "是否管理员", required = true, dataType = "Boolean"),
			@ApiImplicitParam(name = "userId", value = "用户编号", required = true, dataType = "Integer")})
	public Object addGroupMember(String imGroupId, Integer userId, String roleType, boolean isAdmin) {
		return succeed(imGroupMemberService.join(imGroupId,userId,roleType,isAdmin));
	}

	@ApiOperation("退群")
	@PostMapping(value = "/quitGroup")
	@AuditLogAnnotation(operateName = "退群",interfaceURL = "imGroup/quitGroup")
	@ApiImplicitParams({ @ApiImplicitParam(name = "imGroupId", value = "群编号", required = true, dataType = "String"),
			@ApiImplicitParam(name = "userId", value = "用户编号", required = true, dataType = "String")})
	public Object quitGroup(String imGroupId, String userId) {
		List<Integer> userIds = Arrays.stream(userId.split(",")).mapToInt(Integer::valueOf).boxed().collect(Collectors.toList());
		return succeed(imGroupMemberService.quit(imGroupId,userIds));
	}

	@ApiOperation("解散群")
	@PostMapping(value = "/dismissGroup")
	@AuditLogAnnotation(operateName = "解散群",interfaceURL = "imGroup/dismissGroup")
	@ApiImplicitParam(name = "imGroupId", value = "群编号", required = true, dataType = "String")
	public Object dismissGroup(String imGroupId) {
		imGroupService.dismissGroup(imGroupId);
		return succeed();
	}

	@ApiOperation("修改角色")
	@PostMapping(value = "/updateRoleType")
	@ApiImplicitParams({ @ApiImplicitParam(name = "imGroupId", value = "群编号", required = true, dataType = "String"),
			@ApiImplicitParam(name = "roleType", value = "指导老师,乐队指导,乐团主管,运营主管,学员不传", required = true, dataType = "String"),
			@ApiImplicitParam(name = "userId", value = "用户编号", required = true, dataType = "Integer")})
	public Object updateRoleType(String imGroupId, Integer userId, String roleType) {
		return succeed(imGroupMemberService.updateRoleType(imGroupId,userId,roleType));
	}

	@ApiOperation("获取群聊、用户详情")
	@PostMapping(value = "/queryDetail")
	public HttpResponseResult<List<NameDto>> queryDetail(@RequestBody List<NameDto> nameDto) {
		return succeed(imGroupService.queryDetail(nameDto));
	}
}
