package org.jim.server.util;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.jim.common.ImPacket;
import org.jim.common.ImStatus;
import org.jim.common.http.HttpConst;
import org.jim.common.packets.CancelMessageReqBody;
import org.jim.common.packets.Command;
import org.jim.common.packets.RespBody;
import org.jim.common.utils.ImKit;
import org.jim.common.utils.JsonKit;
import org.tio.core.ChannelContext;

import java.io.UnsupportedEncodingException;

/**
 * IM撤销消息 命令工具类
 * @author Darren
 * @date 2020/2/12 17:26
 */
public class CancelMessageKit {

    private static Logger log = Logger.getLogger(CancelMessageKit.class);

    /**
     * 转换为消息结构;
     * @param body
     * @return
     */
    private static CancelMessageReqBody parseCancelMessageBody(byte[] body){
        if(body == null) {
            return null;
        }
        CancelMessageReqBody cancelMessageReqBody = null;
        try {
            String text = new String(body, HttpConst.CHARSET_NAME);
            cancelMessageReqBody = JsonKit.toBean(text, CancelMessageReqBody.class);
            if( null != cancelMessageReqBody){
                return cancelMessageReqBody;
            }
        } catch (UnsupportedEncodingException e) {
            log.error(e.toString());
        }
        return cancelMessageReqBody;
    }

    /**
     * 验证消息体中的值
     * @param cancelMessageReqBody
     * @return
     */
    private static boolean validateCancelMessageBody(CancelMessageReqBody cancelMessageReqBody){
        boolean b1 = StringUtils.isNotEmpty(cancelMessageReqBody.getId());
        boolean b2 = StringUtils.isNotEmpty(cancelMessageReqBody.getGroup_id());
        boolean b3 = StringUtils.isNotEmpty(cancelMessageReqBody.getFrom());
        boolean b4 = null != cancelMessageReqBody.getMsgState();
        boolean b5 = null != cancelMessageReqBody.getChatType();
        return (b1 && b2 && b3 && b4 && b5);
    }

    /**
     * 转换为消息结构;
     * @param body
     * @return
     */
    public static CancelMessageReqBody toCancelMessageBody(byte[] body){
        CancelMessageReqBody cancelMessageReqBody = parseCancelMessageBody(body);
        if( null != cancelMessageReqBody ){
            if( validateCancelMessageBody(cancelMessageReqBody) ){
                return cancelMessageReqBody;
            }
        }
        return null;
    }

    /**
     * 消息数据格式不正确响应包
     * @param channelContext
     * @return imPacket
     * @throws Exception
     */
    public static ImPacket dataInCorrectRespPacket(ChannelContext channelContext) {
        RespBody chatDataInCorrectRespPacket = new RespBody(Command.COMMAND_CANCEL_MSG_RESP, ImStatus.C10023);
        ImPacket respPacket = ImKit.ConvertRespPacket(chatDataInCorrectRespPacket, channelContext);
        respPacket.setStatus(ImStatus.C10023);
        return respPacket;
    }

    /**
     * 消息撤销失败,当前撤销消息不是您发的消息,不可以被撤销!
     * @param channelContext
     * @return imPacket
     * @throws Exception
     */
    public static ImPacket fromIdInCorrectRespPacket(ChannelContext channelContext) {
        RespBody chatDataInCorrectRespPacket = new RespBody(Command.COMMAND_CANCEL_MSG_RESP, ImStatus.C10025);
        ImPacket respPacket = ImKit.ConvertRespPacket(chatDataInCorrectRespPacket, channelContext);
        respPacket.setStatus(ImStatus.C10025);
        return respPacket;
    }

    /**
     * 消息撤销成功响应包
     * @param channelContext
     * @return
     * @throws Exception
     */
    public static ImPacket sendSuccessRespPacket(ChannelContext channelContext) {
        RespBody chatDataInCorrectRespPacket = new RespBody(Command.COMMAND_CANCEL_MSG_RESP,ImStatus.C10024);
        ImPacket respPacket = ImKit.ConvertRespPacket(chatDataInCorrectRespPacket, channelContext);
        respPacket.setStatus(ImStatus.C10024);
        return respPacket;
    }
}
