package com.wecloud.im.sdk;

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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;

import com.wecloud.im.sdk.internal.WecloudImClientOperation;
import com.wecloud.im.sdk.internal.WecloudImConversationOperation;
import com.wecloud.im.sdk.internal.WecloudImMessageOperation;
import com.wecloud.im.sdk.model.Blacklist;
import com.wecloud.im.sdk.model.ChatRoomMember;
import com.wecloud.im.sdk.model.ClientLoginRequest;
import com.wecloud.im.sdk.model.Conversation;
import com.wecloud.im.sdk.model.Friend;
import com.wecloud.im.sdk.model.FriendRequestRequest;
import com.wecloud.im.sdk.model.GetFriendsRequest;
import com.wecloud.im.sdk.model.GetUnsureFriendsRequest;
import com.wecloud.im.sdk.model.GroupChatMessage;
import com.wecloud.im.sdk.model.GroupChatStatusMessage;
import com.wecloud.im.sdk.model.GroupMembersFriendRequestRequest;
import com.wecloud.im.sdk.model.ImClient;
import com.wecloud.im.sdk.model.ListChatRoomMemberRequest;
import com.wecloud.im.sdk.model.ListConversationRequest;
import com.wecloud.im.sdk.model.ModifyUserRequest;
import com.wecloud.im.sdk.model.PageBlacklistRequest;
import com.wecloud.im.sdk.model.PageResult;
import com.wecloud.im.sdk.model.PrivateChatMessage;
import com.wecloud.im.sdk.model.PrivateChatStatusMessage;
import com.wecloud.im.sdk.model.PublishGroupChatMessageRequest;
import com.wecloud.im.sdk.model.PublishGroupChatStatusMessageRequest;
import com.wecloud.im.sdk.model.PublishPrivateChatMessageRequest;
import com.wecloud.im.sdk.model.PublishPrivateChatStatusMessageRequest;
import com.wecloud.im.sdk.model.RegisterClientRequest;
import com.wecloud.im.sdk.model.SetConversationDoNotDisturbRequest;
import com.wecloud.im.sdk.model.SetConversationTopRequest;
import com.wecloud.im.sdk.model.Token;
import com.wecloud.im.sdk.model.UnsureFriend;

/**
 *
 * @Author luozh
 * @Date 2022年04月13日 11:23:24
 * @Version 1.0
 */
public class WecloudImClient implements WecloudIm {

    /**
     * 请求地址
     */
    private URL apiDomain;

    /**
     * 蔚可云分配后台分配的appKey
     */
    private String appKey;

    /**
     * 蔚可云分配后台分配的appSecret
     */
    private String appSecret;

    /* The miscellaneous IM operations */
    private WecloudImClientOperation imClientOperation;

    private WecloudImConversationOperation imConversationOperation;

    private WecloudImMessageOperation wecloudImMessageOperation;

    public WecloudImClient(URL apiDomain, String appKey, String appSecret) {
        this.apiDomain = apiDomain;
        this.appKey = appKey;
        this.appSecret = appSecret;
        initOperations();
    }

    @Override
    public ImClient registerClient(String userId, String headPortrait, String nickname) {
        RegisterClientRequest registerClientRequest =
                RegisterClientRequest.builder()
                        .userId(userId).headPortrait(headPortrait)
                        .nickname(nickname)
                        .build();
        return imClientOperation.registerClient(registerClientRequest);
    }

    @Override
    public Token getToken(String userId, Integer deviceType) {
        ClientLoginRequest clientLoginRequest = ClientLoginRequest.builder().clientId(userId).deviceType(deviceType).build();

        return imClientOperation.getToken(clientLoginRequest);
    }

    @Override
    public Boolean modifyUser(String userId, String nickname, String headPortrait) {
        ModifyUserRequest modifyUserRequest =
                ModifyUserRequest.builder().userId(userId).nickname(nickname).headPortrait(headPortrait).build();

        return imClientOperation.modifyUser(modifyUserRequest);
    }

    @Override
    public List<UnsureFriend> getUnsureFriends(String userId) {
        GetUnsureFriendsRequest getUnsureFriendsRequest = GetUnsureFriendsRequest.builder().userId(userId).build();

        return imClientOperation.getUnsureFriends(getUnsureFriendsRequest);
    }


    @Override
    public List<Friend> getFriends(String userId) {
        GetFriendsRequest getFriendsRequest = GetFriendsRequest.builder().userId(userId).build();
        return imClientOperation.getFriends(getFriendsRequest);
    }

    @Override
    public PageResult<Blacklist> pageBlacklist(Integer pageIndex, Integer pageSize, String userId) {
        PageBlacklistRequest pageBlacklistRequest =
                PageBlacklistRequest.builder().pageSize(pageSize).pageIndex(pageIndex).userId(userId).build();
        return imClientOperation.pageBlacklist(pageBlacklistRequest);
    }

    @Override
    public Boolean friendRequest(String userId, String friendId, String friendName, String requestRemark) {
        FriendRequestRequest friendRequestRequest =
                FriendRequestRequest.builder().userId(userId).friendId(friendId).friendName(friendName).requestRemark(requestRemark).build();
        return imClientOperation.friendRequest(friendRequestRequest);
    }

    @Override
    public Boolean groupMembersFriendRequest(Long conversationId, String userId, String friendId, String friendName, String requestRemark) {
        GroupMembersFriendRequestRequest groupMembersFriendRequestRequest =
                GroupMembersFriendRequestRequest.builder().conversationId(conversationId)
                        .userId(userId).friendId(friendId)
                        .friendName(friendName)
                        .requestRemark(requestRemark)
                        .build();
        return imConversationOperation.groupMembersFriendRequest(groupMembersFriendRequestRequest);
    }

    @Override
    public List<ChatRoomMember> listChatRoomMember(Long chatRoomId) {
        ListChatRoomMemberRequest request = ListChatRoomMemberRequest.builder().chatRoomId(chatRoomId).build();
        return imClientOperation.listChatRoomMember(request);
    }

    @Override
    public List<Conversation> listConversation(Integer chatType, List<Long> conversationIds) {
        ListConversationRequest request = ListConversationRequest.builder().chatType(chatType)
                .conversationIds(conversationIds).build();
        return imClientOperation.listConversation(request);
    }

    @Override
    public Boolean setConversationTop(String userId, Long conversationId, Boolean top) {
        SetConversationTopRequest setConversationTopRequest =
                SetConversationTopRequest.builder().userId(userId).conversationId(conversationId).top(top).build();
        return imConversationOperation.setConversationTop(setConversationTopRequest);
    }

    @Override
    public Boolean setConversationDoNotDisturb(String userId, Long conversationId, Boolean doNotDisturb) {
        SetConversationDoNotDisturbRequest setConversationDoNotDisturbRequest =
                SetConversationDoNotDisturbRequest.builder().userId(userId).conversationId(conversationId).doNotDisturb(doNotDisturb).build();
        return imConversationOperation.setConversationDoNotDisturb(setConversationDoNotDisturbRequest);
    }

    @Override
    public Boolean publishGroupChatMessage(GroupChatMessage groupChatMessage) {
        Assert.notNull(groupChatMessage);
        PublishGroupChatMessageRequest request = BeanUtil.toBean(groupChatMessage,
                PublishGroupChatMessageRequest.class);
        return wecloudImMessageOperation.publishGroupChatMessage(request);
    }

    @Override
    public Boolean publishGroupChatStatusMessage(GroupChatStatusMessage groupChatstatusMessage) {
        Assert.notNull(groupChatstatusMessage);
        PublishGroupChatStatusMessageRequest request = BeanUtil.toBean(groupChatstatusMessage,
                PublishGroupChatStatusMessageRequest.class);
        return wecloudImMessageOperation.publishGroupChatStatusMessage(request);
    }

    @Override
    public Boolean publishPrivateMessage(PrivateChatMessage privateChatMessage) {
        Assert.notNull(privateChatMessage);
        PublishPrivateChatMessageRequest request = BeanUtil.toBean(privateChatMessage,
                PublishPrivateChatMessageRequest.class);
        return wecloudImMessageOperation.publishPrivateMessage(request);
    }

    @Override
    public Boolean publishPrivateStatusMessage(PrivateChatStatusMessage privateChatStatusMessage) {
        Assert.notNull(privateChatStatusMessage);
        PublishPrivateChatStatusMessageRequest request = BeanUtil.toBean(privateChatStatusMessage,
                PublishPrivateChatStatusMessageRequest.class);
        return wecloudImMessageOperation.publishPrivateStatusMessage(request);
    }

    private void initOperations() {
        this.imClientOperation = new WecloudImClientOperation(apiDomain, appKey, appSecret);
        this.imConversationOperation = new WecloudImConversationOperation(apiDomain, appKey, appSecret);
        this.wecloudImMessageOperation = new WecloudImMessageOperation(apiDomain, appKey, appSecret);
    }


}
