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.InputIndicatorReqBody;
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;

/**
 * @author Darren
 * @date 2020/2/13 19:24
 */
public class InputIndicatorKit {

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

    /**
     * 转换为消息结构;
     * @param body
     * @return
     */
    public static InputIndicatorReqBody toInputIndicatorReqBody(byte[] body){
        InputIndicatorReqBody inputIndicatorReqBody = parseInputIndicatorReqBody(body);
        if( null != inputIndicatorReqBody ){
            if( validateInputIndicatorReqBody(inputIndicatorReqBody) ){
                return inputIndicatorReqBody;
            }
        }
        return null;
    }

    /**
     * 验证消息体中的值
     * @param inputIndicatorReqBody
     * @return
     */
    private static boolean validateInputIndicatorReqBody(InputIndicatorReqBody inputIndicatorReqBody) {
        boolean b1 = StringUtils.isNotEmpty(inputIndicatorReqBody.getGroup_id());
        boolean b2 = StringUtils.isNotEmpty(inputIndicatorReqBody.getFrom());
        boolean b3 = null != inputIndicatorReqBody.getChatType();
        return (b1 && b2 && b3);
    }

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

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

    /**
     * 成功响应包
     * @param channelContext
     * @return
     * @throws Exception
     */
    public static ImPacket sendSuccessRespPacket(ChannelContext channelContext) {
        RespBody respBody = new RespBody(Command.COMMAND_INPUT_POINTER_RESP,ImStatus.C10027);
        ImPacket respPacket = ImKit.ConvertRespPacket(respBody, channelContext);
        respPacket.setStatus(ImStatus.C10027);
        return respPacket;
    }

}
