package com.ym.mec.web.controller;

import com.ym.mec.biz.service.ImGroupMemberService;
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 java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dto.ImUserFriendDto;
import com.ym.mec.biz.dal.entity.ImGroup;
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.common.controller.BaseController;

@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 SysUserFeignService sysUserFeignService;

	@ApiOperation("查询群列表")
	@GetMapping(value = "/queryGroupList")
	public Object queryGroupList(String search) {
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (Objects.isNull(sysUser)) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}

		return succeed(imGroupService.queryByUserId(sysUser.getId(), search));
	}

	@ApiOperation("查询群详情")
	@GetMapping(value = "/queryGroupDetail")
	public Object queryGroupList(Long imGroupId) {

		return succeed(imGroupService.get(imGroupId));
	}

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

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

	@ApiOperation("查询群成员列表")
	@GetMapping(value = "/queryGroupMemberList")
	public Object queryGroupMemberList(Long imGroupId) {

		return succeed(imGroupService.queryMemberById(imGroupId));
	}

	@ApiOperation("查询群学生列表")
	@GetMapping(value = "/queryGroupStudentList")
	public Object queryGroupStudentList(Long imGroupId) {

		return succeed(imGroupService.queryMemberById(imGroupId).stream().filter(e -> StringUtils.isBlank(e.getRoleType())).collect(Collectors.toList()));
	}

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

	@ApiOperation("查询好友详情")
	@GetMapping(value = "/queryFriendDetail")
	public Object queryFriendDetail(Integer userId) {
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (Objects.isNull(sysUser)) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}
		ImUserFriendDto dto = imUserFriendService.queryFriendDetail(sysUser.getId(), userId);
		if (dto == null) {
			dto = new ImUserFriendDto();
			SysUser user = sysUserFeignService.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 Object queryFriendList(String search) {
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (Objects.isNull(sysUser)) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}

		return succeed(imUserFriendService.queryFriendListByUserId(sysUser.getId(), search));
	}

	@ApiOperation("查询好友中的学生列表")
	@GetMapping(value = "/queryFriendStudentList")
	public Object queryFriendStudentList(String search) {
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (Objects.isNull(sysUser)) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}

		return succeed(imUserFriendService.queryFriendListByUserId(sysUser.getId(), 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 = "Integer"),
			@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(Long 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 = "Integer"),
			@ApiImplicitParam(name = "userId", value = "用户编号", required = true, dataType = "String")})
	public Object quitGroup(Long 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 = "/updateRoleType")
	@ApiImplicitParams({ @ApiImplicitParam(name = "imGroupId", value = "群编号", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "roleType", value = "指导老师,乐队指导,乐团主管,运营主管,学员不传", required = true, dataType = "String"),
			@ApiImplicitParam(name = "userId", value = "用户编号", required = true, dataType = "Integer")})
	public Object updateRoleType(Long imGroupId, Integer userId, String roleType) {
		return succeed(imGroupMemberService.updateRoleType(imGroupId,userId,roleType));
	}
}
