package com.ym.controller;

import com.ym.mec.common.entity.ImGroupMessage;
import com.ym.mec.common.entity.ImTxtMessage;
import com.ym.service.GroupService;
import com.ym.service.MessageService;
import io.rong.messages.TxtMessage;
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.PrivateMessage;
import io.rong.models.message.RecallMessage;
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.List;

/**
 * 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 ImGroupMessage imGroupMessage) throws Exception {
        GroupMessage groupMessage = new GroupMessage();
        ImTxtMessage content = (ImTxtMessage)imGroupMessage.getContent();
        TxtMessage txtMessage = new TxtMessage(content.getContent(),content.getExtra());
        groupMessage.setContent(txtMessage);
        groupMessage.setTargetId(imGroupMessage.getTargetId());
        groupMessage.setSenderId(imGroupMessage.getSenderId());
        groupMessage.setObjectName(imGroupMessage.getObjectName());
        groupMessage.setIsIncludeSender(1);
        return messageService.groupSend(groupMessage);
    }

    @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 = "/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);
    }
}
