package com.wecloud.im.sdk.internal;

import java.net.URL;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.wecloud.im.sdk.common.HttpMethod;
import com.wecloud.im.sdk.common.RequestMessage;
import com.wecloud.im.sdk.model.CreateGroupRequest;
import com.wecloud.im.sdk.model.DismissGroupRequest;
import com.wecloud.im.sdk.model.JoinGroupRequest;
import com.wecloud.im.sdk.model.LeaveGroupRequest;
import com.wecloud.im.sdk.model.ListGroupMembersRequest;

import static com.wecloud.im.sdk.ResourcePathConstants.CREATE_GROUP_REQUEST;
import static com.wecloud.im.sdk.ResourcePathConstants.DISMISS_GROUP_REQUEST;
import static com.wecloud.im.sdk.ResourcePathConstants.JOIN_GROUP_REQUEST;
import static com.wecloud.im.sdk.ResourcePathConstants.LEAVE_GROUP_REQUEST;
import static com.wecloud.im.sdk.ResourcePathConstants.LIST_GROUP_MEMBERS_REQUEST;
import static com.wecloud.im.sdk.utils.CodingUtils.assertParameterNotBlank;

/**
 * 群组操作
 * @Author luozh
 * @Date 2022年05月10日 09:30
 * @Version 1.0
 */
public class WecloudImGroupOperation extends WecloudImOperation {

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

    /**
     * 创建群组
     * @Author luozh
     * @Date 2022年05月10日 10:27:41
     * @param createGroupRequest
     * @Return
     */
    public Long createGroup(CreateGroupRequest createGroupRequest) {
        String userId = createGroupRequest.getUserId();
        String memberIds = createGroupRequest.getMemberIds();
        // 必填参数校验
        assertParameterNotBlank(userId, "userId");
        assertParameterNotBlank(memberIds, "memberIds");
        // 校验通过 构建参数
        TypeReference<Map<String, String>> typeReference = new TypeReference<Map<String, String>>() {
        };
        Map<String, String> param = JSONObject.parseObject(JSON.toJSONString(createGroupRequest), typeReference);
        // 发送请求
        RequestMessage request = new WecloudRequestMessageBuilder().setEndpoint(CREATE_GROUP_REQUEST)
                .setMethod(HttpMethod.POST).setParameters(param)
                .setOriginalRequest(createGroupRequest).build();
        Object result = doOperation(request);
        return JSON.parseObject(JSON.toJSONString(result), Long.class);
    }

    /**
     * 解散群组
     * @Author luozh
     * @Date 2022年05月10日 10:27:54
     * @param dismissGroupRequest
     * @Return
     */
    public Boolean dismissGroup(DismissGroupRequest dismissGroupRequest) {
        String userId = dismissGroupRequest.getUserId();
        String groupId = dismissGroupRequest.getGroupId();
        // 必填参数校验
        assertParameterNotBlank(userId, "userId");
        assertParameterNotBlank(groupId, "groupId");
        // 校验通过 构建参数
        TypeReference<Map<String, String>> typeReference = new TypeReference<Map<String, String>>() {
        };
        Map<String, String> param = JSONObject.parseObject(JSON.toJSONString(dismissGroupRequest), typeReference);
        // 发送请求
        RequestMessage request = new WecloudRequestMessageBuilder().setEndpoint(DISMISS_GROUP_REQUEST)
                .setMethod(HttpMethod.POST).setParameters(param)
                .setOriginalRequest(dismissGroupRequest).build();
        doOperation(request);
        return true;
    }

    /**
     * 加入群组
     * @Author luozh
     * @Date 2022年05月10日 10:28:07
     * @param joinGroupRequest
     * @Return
     */
    public Integer joinGroup(JoinGroupRequest joinGroupRequest) {
        String groupId = joinGroupRequest.getGroupId();
        String userIds = joinGroupRequest.getUserIds();
        // 必填参数校验
        assertParameterNotBlank(groupId, "groupId");
        assertParameterNotBlank(userIds, "userIds");
        // 校验通过 构建参数
        TypeReference<Map<String, String>> typeReference = new TypeReference<Map<String, String>>() {
        };
        Map<String, String> param = JSONObject.parseObject(JSON.toJSONString(joinGroupRequest), typeReference);
        // 发送请求
        RequestMessage request = new WecloudRequestMessageBuilder().setEndpoint(JOIN_GROUP_REQUEST)
                .setMethod(HttpMethod.POST).setParameters(param)
                .setOriginalRequest(joinGroupRequest).build();
        Object result = doOperation(request);
        return JSON.parseObject(JSON.toJSONString(result), Integer.class);
    }

    /**
     * 离开群组
     * @Author luozh
     * @Date 2022年05月10日 10:28:21
     * @param leaveGroupRequest
     * @Return
     */
    public Integer leaveGroup(LeaveGroupRequest leaveGroupRequest) {
        String groupId = leaveGroupRequest.getGroupId();
        String userIds = leaveGroupRequest.getUserIds();
        // 必填参数校验
        assertParameterNotBlank(groupId, "groupId");
        assertParameterNotBlank(userIds, "userIds");
        // 校验通过 构建参数
        TypeReference<Map<String, String>> typeReference = new TypeReference<Map<String, String>>() {
        };
        Map<String, String> param = JSONObject.parseObject(JSON.toJSONString(leaveGroupRequest), typeReference);
        // 发送请求
        RequestMessage request = new WecloudRequestMessageBuilder().setEndpoint(LEAVE_GROUP_REQUEST)
                .setMethod(HttpMethod.POST).setParameters(param)
                .setOriginalRequest(leaveGroupRequest).build();
        Object result = doOperation(request);
        return JSON.parseObject(JSON.toJSONString(result), Integer.class);
    }

    /**
     * 获取群成员列表
     * @Author luozh
     * @Date 2022年05月10日 10:28:34
     * @param listGroupMembersRequest
     * @Return
     */
    public List<String> listGroupMembers(ListGroupMembersRequest listGroupMembersRequest) {
        String groupId = listGroupMembersRequest.getGroupId();
        // 必填参数校验
        assertParameterNotBlank(groupId, "groupId");
        // 校验通过 构建参数
        TypeReference<Map<String, String>> typeReference = new TypeReference<Map<String, String>>() {
        };
        Map<String, String> param = JSONObject.parseObject(JSON.toJSONString(listGroupMembersRequest), typeReference);
        // 发送请求
        RequestMessage request = new WecloudRequestMessageBuilder().setEndpoint(LIST_GROUP_MEMBERS_REQUEST)
                .setMethod(HttpMethod.POST).setParameters(param)
                .setOriginalRequest(listGroupMembersRequest).build();
        Object result = doOperation(request);
        TypeReference<List<String>> resultType = new TypeReference<List<String>>() {
        };
        return JSON.parseObject(JSON.toJSONString(result), resultType);
    }


}
