package org.jim.server.command.handler;

import org.apache.commons.collections4.CollectionUtils;
import org.jim.common.ImAio;
import org.jim.common.ImConst;
import org.jim.common.ImPacket;
import org.jim.common.ImStatus;
import org.jim.common.packets.*;
import org.jim.server.command.AbstractCmdHandler;
import org.jim.server.command.handler.processor.chat.IMCancelMessageProcessor;
import org.jim.server.enums.CancelMessageTypeEnum;
import org.jim.server.util.CancelMessageKit;
import org.tio.core.Aio;
import org.tio.core.ChannelContext;

import java.util.List;

/**
 * 消息撤销处理器
 * @author Darren
 * @date 2020/2/12 10:00
 */
public class IMCancelMessageReqHandler extends AbstractCmdHandler {

    @Override
    public Command command() {
        return Command.COMMAND_CANCEL_MSG_REQ;
    }

    /**
     * 消息撤销处理方法
     * {
     *   'id':String消息id,
     *   'from':String来源ID,
     *   'msgState':Integer消息状态{0:正常消息,1:撤销消息},
     *   'group_id':String群组id仅在chatType为1时需要,
     *   'chatType':Integer聊天类型{0:未知,1:群聊,2:私聊}
     * }
     * @param packet
     * @param channelContext
     * @return
     * @throws Exception
     */
    @Override
    public ImPacket handler(ImPacket packet, ChannelContext channelContext) throws Exception {
        if( null == packet.getBody() ){
            Aio.remove(channelContext, "body is null");
            return null;
        }
        //调用 CancelMessageKit.toCancelMessageBody() 方法 转换消息结构 并验证 参数
        CancelMessageReqBody cancelMessageReqBody = CancelMessageKit.toCancelMessageBody(packet.getBody());
        if( cancelMessageReqBody == null ){
            ImPacket respChatPacket = CancelMessageKit.dataInCorrectRespPacket(channelContext);
            return respChatPacket;
        }
        List<IMCancelMessageProcessor> cancelMessageProcessor = this.getProcessor(ImConst.IM_CANCEL_MESSAGE_PROCESSOR, IMCancelMessageProcessor.class);
        if(CollectionUtils.isEmpty(cancelMessageProcessor)){
            Aio.remove(channelContext, "no cancel message serviceHandler processor!");
            return null;
        }
        if( CancelMessageTypeEnum.CANCEL.getKey() == cancelMessageReqBody.getMsgState() ){
            ImPacket respPacket = cancelMessageProcessor.get(0).handler(cancelMessageReqBody,channelContext);
            if( null == respPacket){
                return null;
            }
            if( respPacket.getStatus().getCode() == ImStatus.C10025.getCode() ){
                return respPacket;
            }
            ImPacket cancelMessagePacket = new ImPacket(Command.COMMAND_CANCEL_MSG_REQ,new RespBody(Command.COMMAND_CANCEL_MSG_REQ, cancelMessageReqBody).toByte());
            cancelMessagePacket.setSynSeq(packet.getSynSeq());
            if(ChatType.CHAT_TYPE_PUBLIC.getNumber() == cancelMessageReqBody.getChatType()){
                String group_id = cancelMessageReqBody.getGroup_id();
                //告知 群组 此消息 撤销
                ImAio.sendToGroup(group_id, cancelMessagePacket);
                //发送成功响应包
                return CancelMessageKit.sendSuccessRespPacket(channelContext);
            }
        }
        return null;
    }
}
