package com.wecloud.im.friend.controller;

import com.wecloud.im.entity.ImClient;
import com.wecloud.im.friend.param.ImFriendApplyDto;
import com.wecloud.im.friend.param.ImFriendBaseDto;
import com.wecloud.im.friend.param.ImFriendPageParam;
import com.wecloud.im.friend.param.ImFriendRecommendDto;
import com.wecloud.im.friend.service.ImFriendService;
import com.wecloud.im.service.ImClientService;
import io.geekidea.springbootplus.framework.common.api.ApiCode;
import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.common.controller.BaseController;
import io.geekidea.springbootplus.framework.core.pagination.Paging;
import io.geekidea.springbootplus.framework.log.annotation.OperationLog;
import io.geekidea.springbootplus.framework.log.enums.OperationLogType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
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 java.util.List;
import java.util.stream.Collectors;

/**
 * @author lixiaozhong
 * 好友与好友推荐相关接口
 */
@Slf4j
@RestController
@RequestMapping("/friend")
@Api(value = "好友与好友推荐相关API", tags = {"好友关系"})
public class ImFriendController extends BaseController {

    @Autowired
    private ImFriendService imFriendService;

    @Autowired
    private ImClientService imClientService;

    /**
     * 查询好友信息，只有自己的好友才查得到
     */
    @PostMapping("/info")
    @ApiOperation(value = "查询好友信息，只有自己的好友才查得到")
    public ApiResult<ImFriendApplyDto> getFriendInfo(@ApiParam("好友的clientId") String friendClientId) {
        ImClient currentClient = imClientService.getCurentClient();
        if(currentClient == null) {
            return ApiResult.fail(ApiCode.CLIENT_NOT_FOUNT, null);
        }
        ImClient friendClient = imClientService.getCacheImClient(currentClient.getFkAppid(), friendClientId);
        if(friendClient == null) {
            return ApiResult.fail(ApiCode.CLIENT_NOT_FOUNT, null);
        }
        ImFriendApplyDto friendInfo = imFriendService.getFriendInfo(currentClient.getId(), friendClient.getId());
        return ApiResult.ok(friendInfo);
    }

    /**
     * 申请添加好友
     */
    @PostMapping("/apply")
    @ApiOperation(value = "申请添加好友")
    public ApiResult<Boolean> applyFriend(@ApiParam("好友的clientId") String friendClientId, @ApiParam("备注好友名称") String friendName,
                                          @ApiParam("请求备注") String requestRemark) {
        ImClient currentClient = imClientService.getCurentClient();
        if(currentClient == null) {
            return ApiResult.fail(ApiCode.CLIENT_NOT_FOUNT, null);
        }
        ImClient friendClient = imClientService.getCacheImClient(currentClient.getFkAppid(), friendClientId);
        if(friendClient == null) {
            return ApiResult.fail(ApiCode.CLIENT_NOT_FOUNT, null);
        }
        if(currentClient.getId().equals(friendClient.getId())) {
            return ApiResult.fail(ApiCode.PARAMETER_EXCEPTION, null);
        }
        imFriendService.applyFriend(currentClient.getId(), friendClient.getId(), friendName, requestRemark);
        return ApiResult.ok();
    }

    /**
     * 待接受的好友请求列表
     */
    @PostMapping("/unsureFriends")
    @ApiOperation(value = "待接受的好友请求列表")
    public ApiResult<Paging<ImFriendApplyDto>> getUnsureFriends() {
        ImClient currentClient = imClientService.getCurentClient();
        if(currentClient == null) {
            return ApiResult.fail(ApiCode.CLIENT_NOT_FOUNT, null);
        }
        Paging<ImFriendApplyDto> friendInfo = imFriendService.getUnsureFriends(currentClient.getId());
        return ApiResult.ok(friendInfo);
    }

    /**
     * 接受/拒绝好友申请
     */
    @PostMapping("/approve")
    @ApiOperation(value = "接受/拒绝好友申请")
    public ApiResult<Boolean> approveFriend(String friendClientId, Boolean agree, String rejectRemark) {
        if(agree == null) {
            return ApiResult.fail(ApiCode.PARAMETER_EXCEPTION, null);
        }
        ImClient currentClient = imClientService.getCurentClient();
        if(currentClient == null) {
            return ApiResult.fail(ApiCode.CLIENT_NOT_FOUNT, null);
        }
        ImClient friendClient = imClientService.getCacheImClient(currentClient.getFkAppid(), friendClientId);
        if(friendClient == null) {
            return ApiResult.fail(ApiCode.CLIENT_NOT_FOUNT, null);
        }

        imFriendService.approveFriend(currentClient.getId(), friendClient.getId(), agree, rejectRemark);
        return ApiResult.ok();
    }


    /**
     * 批量删除好友
     */
    @PostMapping("/batchDelete")
    @ApiOperation(value = "删除好友")
    public ApiResult<Boolean> batchDeleteFriend(@RequestBody List<String> friendClientIds) {
        ImClient currentClient = imClientService.getCurentClient();
        if(currentClient == null) {
            return ApiResult.fail(ApiCode.CLIENT_NOT_FOUNT, null);
        }

        imFriendService.batchDeleteFriend(currentClient, friendClientIds);
        return ApiResult.ok();
    }

    /**
     * 好友分页列表
     */
    @PostMapping("/getPageList")
    @OperationLog(name = "好友分页列表", type = OperationLogType.PAGE)
    @ApiOperation(value = "好友分页列表")
    public ApiResult<Paging<ImFriendBaseDto>> getImFriendPageList(@RequestBody ImFriendPageParam pageParam) {
        ImClient currentClient = imClientService.getCurentClient();
        if(currentClient == null) {
            return ApiResult.fail(ApiCode.CLIENT_NOT_FOUNT, null);
        }

        Paging<ImFriendBaseDto> friends = imFriendService.getImFriendPageList(currentClient.getId(), pageParam);
        return ApiResult.ok(friends);
    }

    /**
     * 批量创建好友推荐
     */
    @PostMapping("/recommend/batchCreate")
    @OperationLog(name = "批量创建好友推荐", type = OperationLogType.PAGE)
    @ApiOperation(value = "批量创建好友推荐")
    public ApiResult<Boolean> batchCreateRecommend(@RequestBody List<ImFriendRecommendDto> recommendFriends) {
        ImClient currentClient = imClientService.getCurentClient();
        if(currentClient == null) {
            return ApiResult.fail(ApiCode.CLIENT_NOT_FOUNT, null);
        }
        if(CollectionUtils.isEmpty(recommendFriends)) {
            return ApiResult.fail(ApiCode.PARAMETER_EXCEPTION, null);
        }
        imFriendService.batchCreateRecommend(currentClient, recommendFriends);
        return ApiResult.ok();
    }

    /**
     * 好友推荐分页列表
     */
    @PostMapping("/recommend/getPageList")
    @OperationLog(name = "好友推荐分页列表", type = OperationLogType.PAGE)
    @ApiOperation(value = "好友推荐分页列表")
    public ApiResult<Paging<ImFriendRecommendDto>> getImFriendRecommendPageList(@RequestBody ImFriendPageParam pageParam) {
        ImClient currentClient = imClientService.getCurentClient();
        if(currentClient == null) {
            return ApiResult.fail(ApiCode.CLIENT_NOT_FOUNT, null);
        }
        Paging<ImFriendRecommendDto> res = imFriendService.getImFriendRecommendPageList(currentClient.getId(), pageParam);
        return ApiResult.ok(res);

    }

    /**
     * 批量删除好友推荐
     */
    @PostMapping("/recommend/batchDelete")
    @ApiOperation(value = "删除好友推荐")
    public ApiResult<Boolean> batchDeleteRecommend(@RequestBody List<String> friendClientIds) {
        ImClient currentClient = imClientService.getCurentClient();
        if(currentClient == null) {
            return ApiResult.fail(ApiCode.CLIENT_NOT_FOUNT, null);
        }
        imFriendService.batchDeleteRecommend(currentClient, friendClientIds);
        return ApiResult.ok();
    }
}

