package com.ym.mec.student.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.Objects;

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.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.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 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("查询群成员列表")
	@GetMapping(value = "/queryGroupMemberList")
	public Object queryGroupMemberList(Long imGroupId) {

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

	@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, "请登录");
		}

		return succeed(imUserFriendService.queryFriendDetail(sysUser.getId(), userId));
	}

	@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 = "/queryNoticeList")
	public Object queryNoticeList(ImGroupNoticeQueryInfo queryInfo) {
		return succeed(imGroupNoticeService.queryPage(queryInfo));
	}
}
