package com.jpsoft.shinestar.lapi.handler;

import cn.hutool.json.JSONObject;
import com.jpsoft.shinestar.lapi.dto.LapiResponse;
import com.jpsoft.shinestar.modules.uniview.channel.LapiAttrKeys;
import com.jpsoft.shinestar.modules.uniview.utils.LapiSessionUtil;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Map;
import java.util.concurrent.BlockingQueue;

@Slf4j
@Component
@ChannelHandler.Sharable
public class ResponseDataHandler extends SimpleChannelInboundHandler<LapiResponse> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, LapiResponse msg) throws Exception {
        JSONObject jsonObject = msg.getData();

        String uri = jsonObject.getJSONObject("Response").get("ResponseURL").toString();
        String deviceNo = ctx.channel().attr(LapiAttrKeys.DEVICE_NO).get();

        BlockingQueue<String> queue = LapiSessionUtil.get(deviceNo).getReceivePacketQueue(uri);

        queue.put(jsonObject.toString());
    }
}
