package com.ym.controller;

import com.google.common.collect.Lists;
import com.ym.mec.common.entity.ImMessageDto;
import com.ym.pojo.NoticeMessage;
import com.ym.service.GroupService;
import com.ym.service.MessageService;
import io.rong.messages.FileMessage;
import io.rong.messages.ImgMessage;
import io.rong.messages.TxtMessage;
import io.rong.messages.UserInfo;
import io.rong.models.Result;
import io.rong.models.group.GroupMember;
import io.rong.models.group.GroupModel;
import io.rong.models.group.UserGroup;
import io.rong.models.message.GroupMessage;
import io.rong.models.message.MentionMessage;
import io.rong.models.message.RecallMessage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by weiqinxiao on 2019/2/25.
 */
@RestController
@RequestMapping("/group")
public class GroupController{

    @Autowired
    MessageService messageService;
    @Autowired
    GroupService groupService;

    @RequestMapping(value = "/send", method = RequestMethod.POST)
    public Object send(@RequestBody ImMessageDto imMessageDto) throws Exception {
        GroupMessage groupMessage = new GroupMessage();
        String objectName = imMessageDto.getObjectName();
        UserInfo userInfo = new UserInfo();
        if(imMessageDto.getUserInfo() != null){
            BeanUtils.copyProperties(imMessageDto.getUserInfo(),userInfo);
        }
        if("DY:NoticeMsg".equals(objectName)){
            NoticeMessage noticeMessage = new NoticeMessage(imMessageDto.getMessageContent(),imMessageDto.getExtra());
            noticeMessage.setUser(userInfo);
            groupMessage.setContent(noticeMessage);
        }else if("RC:ImgMsg".equals(objectName)){
            ImgMessage imgMessage = new ImgMessage(imMessageDto.getMessageContent(),imMessageDto.getExtra(),imMessageDto.getFileUrl());
            imgMessage.setUser(userInfo);
            groupMessage.setContent(imgMessage);
        }else if("RC:FileMsg".equals(objectName)){
            FileMessage imgMessage = new FileMessage(imMessageDto.getFileName(),imMessageDto.getFileSize(),imMessageDto.getFileUrl(),imMessageDto.getExtra());
            groupMessage.setContent(imgMessage);
        }else {
            TxtMessage txtMessage = new TxtMessage(imMessageDto.getMessageContent(),imMessageDto.getExtra());
            txtMessage.setUser(userInfo);
            groupMessage.setContent(txtMessage);
        }
        groupMessage.setSenderId(imMessageDto.getSenderId());
        groupMessage.setObjectName(imMessageDto.getObjectName());
        groupMessage.setIsIncludeSender(imMessageDto.getIsIncludeSender());
        //融云只支持3个群以内的群发
        String[] targetIds = imMessageDto.getTargetIds();
        List<String> imGroupIds = Arrays.stream(targetIds).collect(Collectors.toList());
        List<List<String>> partition = Lists.partition(imGroupIds, 3);
        Result result = null;
        for (List<String> integers : partition) {
            String[] strings = integers.toArray(new String[integers.size()]);
            groupMessage.setTargetId(strings);
            result = messageService.groupSend(groupMessage);
        }
        return result;
    }

    @RequestMapping(value = "/sendDirection", method = RequestMethod.POST)
    public Object sendDirection(@RequestBody GroupMessage groupMessage) throws Exception {
        return messageService.groupSendDirection(groupMessage);
    }

    @RequestMapping(value = "/sendMention", method = RequestMethod.POST)
    public Object sendMention(@RequestBody MentionMessage mentionMessage) throws Exception {
        return messageService.groupSendMention(mentionMessage);
    }

    @RequestMapping(value = "/recall", method = RequestMethod.POST)
    public Object recall(@RequestBody RecallMessage recallMessage) throws Exception {
        return messageService.groupRecall(recallMessage);
    }

    @RequestMapping(value = "/sync", method = RequestMethod.POST)
    public Object sync(@RequestBody UserGroup userGroup) throws Exception {
        return groupService.groupSync(userGroup);
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public Object create(@RequestBody GroupModel groupModel) throws Exception {
        return groupService.groupCreate(groupModel);
    }

    @RequestMapping(value = "/batchCreate", method = RequestMethod.POST)
    public void batchCreate(@RequestBody List<GroupModel> groupModels){
        groupService.batchCreate(groupModels);
    }

    @RequestMapping(value = "/get", method = RequestMethod.POST)
    public Object get(@RequestBody GroupModel groupModel) throws Exception {
        return groupService.groupGet(groupModel);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Object update(@RequestBody GroupModel groupModel) throws Exception {
        return groupService.groupUpdate(groupModel);
    }

    @RequestMapping(value = "/join", method = RequestMethod.POST)
    public Object join(@RequestBody GroupModel groupModel) throws Exception {
        return groupService.groupJoin(groupModel);
    }

    @RequestMapping(value = "/batchJoin", method = RequestMethod.POST)
    public void batchJoin(@RequestBody List<GroupModel> groupModels) throws Exception {
        groupService.groupBatchJoin(groupModels);
    }

    @RequestMapping(value = "/quit", method = RequestMethod.POST)
    public Object quit(@RequestBody GroupModel groupModel) throws Exception {
        return groupService.groupQuit(groupModel);
    }

    @RequestMapping(value = "/batchQuit", method = RequestMethod.POST)
    public void batchQuit(@RequestBody List<GroupModel> groupModels) throws Exception {
        groupService.groupBatchQuit(groupModels);
    }

    @RequestMapping(value = "/dismiss", method = RequestMethod.POST)
    public Object dismiss(@RequestBody GroupModel groupModel) throws Exception {
        GroupMember[] groupMember = {new GroupMember("1",null,null)};
        groupModel.setMembers(groupMember);
        return groupService.groupDismiss(groupModel);
    }

    @RequestMapping(value = "/batchDismiss", method = RequestMethod.POST)
    public void batchDismiss(@RequestBody List<GroupModel> groupModels){
        groupService.groupBatchDismiss(groupModels);
    }
}
