package com.keao.edu.im.controller;

import io.rong.messages.TxtMessage;
import io.rong.models.message.PrivateMessage;
import io.rong.models.message.RecallMessage;
import io.rong.models.message.TemplateMessage;

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 com.keao.edu.im.api.entity.ImPrivateMessage;
import com.keao.edu.im.api.entity.ImTxtMessage;
import com.keao.edu.im.service.MessageService;

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

    @Autowired
    MessageService messageService;

    @RequestMapping(value = "/send", method = RequestMethod.POST)
    public Object send(@RequestBody ImPrivateMessage imPrivateMessage) throws Exception {
        PrivateMessage privateMessage = new PrivateMessage();
        ImTxtMessage content = (ImTxtMessage)imPrivateMessage.getContent();
        TxtMessage txtMessage = new TxtMessage(content.getContent(),content.getExtra());
        privateMessage.setContent(txtMessage);
        privateMessage.setTargetId(imPrivateMessage.getTargetId());
        privateMessage.setSenderId(imPrivateMessage.getSenderId());
        privateMessage.setObjectName(imPrivateMessage.getObjectName());
        privateMessage.setIsIncludeSender(1);
        return messageService.privateSend(privateMessage);
    }

    /*@RequestMapping(value = "/sendCustom", method = RequestMethod.POST)
    public Object sendCustom(@RequestBody ImGroupMessage imGroupMessage) throws Exception {
        GroupMessage groupMessage = new GroupMessage();
        ImTxtMessage content = (ImTxtMessage)imGroupMessage.getContent();
        CustomMessage customMessage = new CustomMessage(content.getContent(),content.getExtra());
        customMessage.setTYPE(imGroupMessage.getType());
        groupMessage.setContent(customMessage);
        groupMessage.setTargetId(imGroupMessage.getTargetId());
        groupMessage.setSenderId(imGroupMessage.getSenderId());
        groupMessage.setObjectName(imGroupMessage.getType());
        return messageService.groupSend(groupMessage);
    }*/

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

    @RequestMapping(value = "/sendTemplate", method = RequestMethod.POST)
    public Object sendTemplate(@RequestBody TemplateMessage templateMessage) throws Exception {
        return messageService.privateSendTemplate(templateMessage);
    }
}
