package com.mx.cneeds.server.wechat.websocket.message.handler.wechat;

import com.alibaba.fastjson.JSON;
import com.mx.cneeds.server.wechat.common.codec.Invocation;
import com.mx.cneeds.server.wechat.common.dispatcher.MessageHandler;
import com.mx.cneeds.server.wechat.websocket.config.NettyChannelManager;
import com.mx.cneeds.server.wechat.websocket.message.pojo.chat.ChatRedirectToUserRequest;
import com.mx.cneeds.server.wechat.websocket.message.pojo.wechat.WechatMessageRequest;
import com.mx.cneeds.server.wechat.websocket.message.pojo.wechat.WechatMessageResponse;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @ClassName WechatMessageHandler
 * @Author zzrdark
 * @Date 2020-06-23 18:22
 * @Description TODO
 **/
@Component
@Slf4j
public class WechatMessageHandler implements MessageHandler<WechatMessageRequest> {

    @Autowired
    private NettyChannelManager nettyChannelManager;


    @Override
    public void execute(Channel channel, WechatMessageRequest message) {
        WechatMessageResponse.DataBean responseDataBean = new WechatMessageResponse.DataBean();
        BeanUtils.copyProperties(message.getData(),responseDataBean);
        WechatMessageResponse wechatMessageResponse = new WechatMessageResponse(responseDataBean,message.getMessageType());
        nettyChannelManager.send(message.getWcId(), new Invocation(ChatRedirectToUserRequest.TYPE, wechatMessageResponse));
        log.debug("微控返回消息：{}", JSON.toJSONString(wechatMessageResponse));
    }

    @Override
    public String getType() {
        return WechatMessageRequest.TYPE;
    }
}
