package com.cm.socket.service;

import com.alibaba.fastjson.JSONObject;
import com.cm.common.base.AbstractService;
import com.cm.common.utils.AesUtil;
import com.cm.socket.enums.SocketCodeEnum;
import com.cm.socket.enums.SocketTypeMessageEnum;
import com.cm.socket.pojo.Message;
import com.cm.socket.pojo.SocketResult;
import io.netty.channel.ChannelHandlerContext;

import java.util.List;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: BaseService
 * @Description: service
 * @Author: WangGeng
 * @Date: 2020/7/28 2:13 下午
 * @Version: 1.0
 **/
public abstract class AbstractSocketService extends AbstractService implements ISocketService {

    private static final String CONTENT_ENCODE_KEY = "Ts_Socket_Key_W_";

    /**
     * 读取信息业务
     *
     * @param channelHandlerContext
     * @param readMessage
     */
    protected abstract void readMessageService(ChannelHandlerContext channelHandlerContext, Message readMessage) throws Exception;

    /**
     * 输出信息
     *
     * @param channelHandlerContext
     * @param socketTypeMessageEnum
     * @param socketResult
     */
    protected void writeMessage(ChannelHandlerContext channelHandlerContext, SocketTypeMessageEnum socketTypeMessageEnum, SocketResult socketResult) {
        writeMessage(channelHandlerContext, socketTypeMessageEnum, JSONObject.toJSONString(socketResult));
    }

    /**
     * 输出信息
     *
     * @param channelHandlerContext
     * @param socketTypeMessageEnum
     * @param content
     */
    protected void writeMessage(ChannelHandlerContext channelHandlerContext, SocketTypeMessageEnum socketTypeMessageEnum, String content) {
        Message message = new Message();
        message.setStart(SocketTypeMessageEnum.MESSAGE_TYPE_START.getType());
        message.setType(socketTypeMessageEnum.getType());
        message.setToken(channelHandlerContext.channel().id().asShortText());
        message.setContent(content);
        write(channelHandlerContext, message);
    }

    /**
     * 输出错误信息
     *
     * @param channelHandlerContext
     * @param errorMessage
     */
    protected void writeError(ChannelHandlerContext channelHandlerContext, String errorMessage) {
        Message message = new Message();
        message.setStart(SocketTypeMessageEnum.MESSAGE_TYPE_START.getType());
        message.setType(SocketTypeMessageEnum.MESSAGE_TYPE_ERROR.getType());
        message.setContent(JSONObject.toJSONString(errorResult(errorMessage)));
        write(channelHandlerContext, message);
    }

    /**
     * 输出数据
     *
     * @param channelHandlerContext
     * @param writeMessage
     */
    protected void write(ChannelHandlerContext channelHandlerContext, Message writeMessage) {
        channelHandlerContext.channel().writeAndFlush(writeMessage);
    }

    /**
     * 成功对象结果
     *
     * @param data
     * @return
     */
    protected <T> SocketResult successResult(T data) {
        SocketResult<T> socketResult = new SocketResult<>();
        socketResult.setCode(SocketCodeEnum.SUCCESS.getCode());
        socketResult.setData(data);
        return socketResult;
    }

    /**
     * 成功列表结果
     *
     * @param list
     * @return
     */
    protected <T> SocketResult successResultList(List<T> list) {
        SocketResult<T> socketResult = new SocketResult<>();
        socketResult.setCode(SocketCodeEnum.SUCCESS.getCode());
        socketResult.setList(list);
        return socketResult;
    }

    /**
     * 错误结果
     *
     * @param errorMsg
     * @return
     */
    protected SocketResult errorResult(String errorMsg) {
        SocketResult<String> socketResult = new SocketResult<>();
        socketResult.setCode(SocketCodeEnum.ERROR.getCode());
        socketResult.setErrorMsg(errorMsg);
        return socketResult;
    }

    /**
     * 获取加密结果
     *
     * @param content
     * @return
     * @throws Exception
     */
    protected String getEncodeContent(String content) throws Exception {
        return AesUtil.aesCommonEncoder(CONTENT_ENCODE_KEY, content);
    }

    /**
     * 获取解密结果
     *
     * @param content
     * @return
     * @throws Exception
     */
    protected String getDecodeContent(String content) throws Exception {
        return AesUtil.aesCommonDecoder(CONTENT_ENCODE_KEY, content);
    }

}
