package com.wecloud.im.sdk.internal;

import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import com.alibaba.fastjson.JSON;
import com.wecloud.im.sdk.common.HttpMethod;
import com.wecloud.im.sdk.common.RequestMessage;
import com.wecloud.im.sdk.model.GroupMembersFriendRequestRequest;
import com.wecloud.im.sdk.model.SetConversationDoNotDisturbRequest;
import com.wecloud.im.sdk.model.SetConversationTopRequest;

import static com.wecloud.im.sdk.ResourcePathConstants.CHAT_SETTING;
import static com.wecloud.im.sdk.ResourcePathConstants.GROUP_MEMBERS_FRIEND_REQUEST;
import static com.wecloud.im.sdk.utils.CodingUtils.assertParameterNotNull;

/**
 * client
 * @Author luozh
 * @Date 2022年04月13日 15:26
 * @Version 1.0
 */
public class WecloudImConversationOperation extends WecloudImOperation {

    public WecloudImConversationOperation(URL apiDomain, String appKey, String appSecret) {
        super(apiDomain, appKey, appSecret);
    }

    /**
     * 设置会话置顶
     * @Author luozh
     * @Date 2022年04月26日 03:57:51
     * @param setConversationTopRequest
     * @Return
     */
    public Boolean setConversationTop(SetConversationTopRequest setConversationTopRequest) {
        String userId = setConversationTopRequest.getUserId();
        Long conversationId = setConversationTopRequest.getConversationId();
        Boolean top = setConversationTopRequest.getTop();
        // 参数校验
        assertParameterNotNull(userId, "userId");
        assertParameterNotNull(conversationId, "conversationId");
        assertParameterNotNull(top, "top");
        // 校验通过 构建参数
        Map<String, String> param = new HashMap<>();
        param.put("clientId", userId);
        param.put("conversationId", conversationId.toString());
        param.put("type", "TOP");
        param.put("on", top.toString());
        // 发送请求
        RequestMessage request = new WecloudRequestMessageBuilder().setEndpoint(CHAT_SETTING)
                .setMethod(HttpMethod.POST).setParameters(param)
                .setOriginalRequest(setConversationTopRequest).build();

        doOperation(request);
        return true;
    }

    /**
     * 设置会话免打扰
     * @Author luozh
     * @Date 2022年04月26日 03:58:10
     * @param setConversationDoNotDisturbRequest 设置会话免打扰
     * @Return
     */
    public Boolean setConversationDoNotDisturb(SetConversationDoNotDisturbRequest setConversationDoNotDisturbRequest) {
        String userId = setConversationDoNotDisturbRequest.getUserId();
        Long conversationId = setConversationDoNotDisturbRequest.getConversationId();
        Boolean doNotDisturb = setConversationDoNotDisturbRequest.getDoNotDisturb();
        // 参数校验
        assertParameterNotNull(userId, "userId");
        assertParameterNotNull(conversationId, "conversationId");
        assertParameterNotNull(doNotDisturb, "doNotDisturb");
        // 校验通过 构建参数
        Map<String, String> param = new HashMap<>();
        param.put("clientId", userId);
        param.put("conversationId", conversationId.toString());
        param.put("type", "DO_NOT_DISTURB");
        param.put("on", doNotDisturb.toString());
        // 发送请求
        RequestMessage request = new WecloudRequestMessageBuilder().setEndpoint(CHAT_SETTING)
                .setMethod(HttpMethod.POST).setParameters(param)
                .setOriginalRequest(setConversationDoNotDisturbRequest).build();

        doOperation(request);
        return true;
    }

    public Boolean groupMembersFriendRequest(GroupMembersFriendRequestRequest groupMembersFriendRequestRequest) {
        Long conversationId = groupMembersFriendRequestRequest.getConversationId();
        String userId = groupMembersFriendRequestRequest.getUserId();
        String friendId = groupMembersFriendRequestRequest.getFriendId();
        // 参数校验
        assertParameterNotNull(conversationId, "conversationId");
        assertParameterNotNull(userId, "userId");
        assertParameterNotNull(friendId, "friendId");
        // 校验通过 构建参数
        Map<String, String> param = new HashMap<>();
        param.put("conversationId", conversationId + "");
        param.put("clientId", userId + "");
        param.put("friendClientId", friendId + "");
        param.put("friendName", Optional.ofNullable(groupMembersFriendRequestRequest.getFriendName()).orElse(""));
        param.put("requestRemark", Optional.ofNullable(groupMembersFriendRequestRequest.getRequestRemark()).orElse(""));
        // 发送请求
        RequestMessage request = new WecloudRequestMessageBuilder().setEndpoint(GROUP_MEMBERS_FRIEND_REQUEST)
                .setMethod(HttpMethod.POST).setParameters(param)
                .setOriginalRequest(groupMembersFriendRequestRequest).build();
        Object result = doOperation(request);
        Boolean flag = JSON.parseObject(JSON.toJSONString(result), Boolean.class);
        return flag;
    }
}
