package org.jim.server.command.handler;

import org.jim.common.ImAio;
import org.jim.common.ImPacket;
import org.jim.common.packets.*;
import org.jim.server.command.AbstractCmdHandler;
import org.jim.server.util.EvaluationKit;
import org.jim.server.util.InputIndicatorKit;
import org.tio.core.Aio;
import org.tio.core.ChannelContext;

/**
 * 邀请评价
 * @author Darren
 * @date 2020/2/13 17:47
 */
public class EvaluationReqHandler extends AbstractCmdHandler {

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

    /**
     *{
     *   'from':String来源ID,
     *   'group_id':String群组id仅在chatType为1时需要,
     *   'chatType':Integer聊天类型{0:未知,1:群聊,2:私聊}
     *}
     * @param packet
     * @param channelContext
     * @return {@link {@link ImPacket}}
     * @author Darren
     * @date 2020/2/13 20:22
     */
    @Override
    public ImPacket handler(ImPacket packet, ChannelContext channelContext) throws Exception {
        if( null == packet.getBody() ){
            Aio.remove(channelContext, "body is null");
            return null;
        }
        EvaluationReqBody reqBody = EvaluationKit.toReqBody(packet.getBody());
        if( reqBody == null ){
            ImPacket respChatPacket = EvaluationKit.dataInCorrectRespPacket(channelContext);
            return respChatPacket;
        }
        ImPacket evaluationPacket = new ImPacket(Command.COMMAND_EVALUATION_REQ,new RespBody(Command.COMMAND_EVALUATION_RESP, reqBody).toByte());
        evaluationPacket.setSynSeq(packet.getSynSeq());
        if(ChatType.CHAT_TYPE_PUBLIC.getNumber() == reqBody.getChatType()){
            String group_id = reqBody.getGroup_id();
            //邀请评价消息
            ImAio.sendToGroup(group_id, evaluationPacket);
            //发送成功响应包
            return EvaluationKit.sendSuccessRespPacket(channelContext);
        }
        return null;
    }

}
