package com.ym.mec.student.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.*;
import com.ym.mec.common.controller.BaseController;
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.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@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 ImGroupMemberService imGroupMemberService;

	@Autowired
	private SysUserService sysUserService;

	@ApiOperation("查询群列表")
	@GetMapping(value = "/queryGroupList")
	public Object queryGroupList(String search,String groupType) {
		return succeed(imGroupService.queryByUserId(sysUserService.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 = "/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(sysUserService.getUserId(), userId);
		if (dto == null) {
			dto = new ImUserFriendDto();
			SysUser user = sysUserService.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(sysUserService.getUserId(), search));
	}

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

	@ApiOperation("入群")
	@GetMapping(value = "/addGroupMember")
	@ApiImplicitParam(name = "imGroupId", value = "群编号", required = true, dataType = "String")
	public Object addGroupMember(String imGroupId) {
		return succeed(imGroupMemberService.join(imGroupId,sysUserService.getUserId(),"",false));
	}
}
