package com.keao.edu.im.service.Impl;

import com.keao.edu.im.service.GroupService;
import io.rong.RongCloud;
import io.rong.methods.conversation.Conversation;
import io.rong.methods.group.Group;
import io.rong.methods.group.mute.MuteAllMembers;
import io.rong.methods.group.mute.MuteMembers;
import io.rong.methods.group.mute.whitelist.User;
import io.rong.models.Result;
import io.rong.models.conversation.ConversationModel;
import io.rong.models.group.GroupMember;
import io.rong.models.group.GroupModel;
import io.rong.models.group.UserGroup;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class GroupServiceImpl implements GroupService {

    @Value("${cn.rongcloud.im.appkey}")
    private String appKey;
    @Value("${cn.rongcloud.im.secret}")
    private String appSecret;

    private Group getGroup(){
        RongCloud rongCloud = RongCloud.getInstance(appKey, appSecret);
        return new Group(appKey,appSecret,rongCloud);
    }
    private MuteMembers getMuteMembers(){
        RongCloud rongCloud = RongCloud.getInstance(appKey, appSecret);
        return new MuteMembers(appKey,appSecret,rongCloud);
    }
    private MuteAllMembers getMuteAllMembers(){
        RongCloud rongCloud = RongCloud.getInstance(appKey, appSecret);
        return new MuteAllMembers(appKey,appSecret,rongCloud);
    }
    private User getUser(){
        RongCloud rongCloud = RongCloud.getInstance(appKey, appSecret);
        return new User(appKey,appSecret,rongCloud);
    }
    private Conversation getConversation(){
        RongCloud rongCloud = RongCloud.getInstance(appKey, appSecret);
        Conversation conversation = new Conversation(appKey, appSecret);
        conversation.setRongCloud(rongCloud);
        return conversation;
    }

    @Override
    public Result groupSync(UserGroup userGroup) throws Exception {
        return getGroup().sync(userGroup);
    }

    @Override
    public Result groupCreate(GroupModel groupModel) throws Exception {
        return getGroup().create(groupModel);
    }

    @Override
    public Result groupGet(GroupModel groupModel) throws Exception {
        return getGroup().get(groupModel);
    }

    @Override
    public Result groupUpdate(GroupModel groupModel) throws Exception {
        return getGroup().update(groupModel);
    }

    @Override
    public Result groupJoin(GroupModel groupModel) throws Exception {
        return getGroup().join(groupModel);
    }

    @Override
    public Result groupQuit(GroupModel groupModel) throws Exception {
        return getGroup().quit(groupModel);
    }

    @Override
    public Result groupDismiss(GroupModel groupModel) throws Exception {
        return getGroup().dismiss(groupModel);
    }

    @Override
    public Result muteMembersAdd(GroupModel groupModel) throws Exception {
        return getMuteMembers().add(groupModel);
    }

    @Override
    public Result muteMembersRemove(GroupModel groupModel) throws Exception {
        return getMuteMembers().remove(groupModel);
    }

    @Override
    public Result muteMembersGetList(String groupId) throws Exception {
        return getMuteMembers().getList(groupId);
    }

    @Override
    public Result muteAllMembersAdd(String[] groupIds) throws Exception {
        return getMuteAllMembers().add(groupIds);
    }

    @Override
    public Result muteAllMembersRemove(String[] groupIds) throws Exception {
        return getMuteAllMembers().remove(groupIds);
    }

    @Override
    public Result muteAllMembersGetList() throws Exception {
        return getMuteAllMembers().getList();
    }

    @Override
    public Result muteWhiteAdd(GroupModel groupModel) throws Exception {
        return getUser().add(groupModel);
    }

    @Override
    public Result muteWhiteRemove(GroupModel groupModel) throws Exception {
        return getUser().remove(groupModel);
    }

    @Override
    public Result muteWhiteGetList(String groupId) throws Exception {
        return getUser().getList(groupId);
    }

    @Override
    public Result conversationMute(ConversationModel conversationModel) throws Exception {
        return getConversation().mute(conversationModel);
    }

    @Override
    public Result conversationUnmute(ConversationModel conversationModel) throws Exception {
        return getConversation().unMute(conversationModel);
    }

    @Override
    public Result conversationGet(ConversationModel conversationModel) throws Exception {
        return getConversation().get(conversationModel);
    }

    @Override
    public void batchCreate(List<GroupModel> groupModels) {
        if(groupModels != null && groupModels.size() > 0){
            Group group = getGroup();
            groupModels.forEach(e->{
                try {
                    group.create(e);
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            });
        }
    }

    @Override
    public void groupBatchDismiss(List<GroupModel> groupModels) {
        if(groupModels != null && groupModels.size() > 0){
            Group group = getGroup();
            GroupMember[] groupMember = {new GroupMember("1",null,null)};
            groupModels.forEach(e->{
                try {
                    e.setMembers(groupMember);
                    group.dismiss(e);
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            });
        }
    }

    @Override
    public void groupBatchQuit(List<GroupModel> groupModels) {
        if(groupModels != null && groupModels.size() > 0){
            Group group = getGroup();
            GroupMember[] groupMember = {new GroupMember("1",null,null)};
            groupModels.forEach(e->{
                try {
                    e.setMembers(groupMember);
                    group.quit(e);
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            });
        }
    }
}
