// 
// Decompiled by Procyon v0.6.0
// 

package io.netty.handler.codec.http3;

import io.netty.channel.Channel;
import io.netty.channel.ChannelPipeline;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.util.internal.ObjectUtil;
import io.netty.handler.codec.quic.QuicStreamChannel;
import io.netty.channel.ChannelInitializer;

public abstract class Http3PushStreamServerInitializer extends ChannelInitializer<QuicStreamChannel>
{
    private final long pushId;
    
    protected Http3PushStreamServerInitializer(final long pushId) {
        this.pushId = ObjectUtil.checkPositiveOrZero(pushId, "pushId");
    }
    
    @Override
    protected final void initChannel(final QuicStreamChannel ch) {
        if (!Http3CodecUtils.isServerInitiatedQuicStream(ch)) {
            throw new IllegalArgumentException("Using server push stream initializer for client stream: " + ch.streamId());
        }
        Http3CodecUtils.verifyIsUnidirectional(ch);
        final ByteBuf buffer = ch.alloc().buffer(16);
        Http3CodecUtils.writeVariableLengthInteger(buffer, 1L);
        Http3CodecUtils.writeVariableLengthInteger(buffer, this.pushId);
        ch.write(buffer);
        final Http3ConnectionHandler connectionHandler = Http3CodecUtils.getConnectionHandlerOrClose(ch.parent());
        if (connectionHandler == null) {
            return;
        }
        final ChannelPipeline pipeline = ch.pipeline();
        final Http3RequestStreamEncodeStateValidator encodeStateValidator = new Http3RequestStreamEncodeStateValidator();
        pipeline.addLast(connectionHandler.newCodec(encodeStateValidator, Http3RequestStreamCodecState.NO_STATE));
        pipeline.addLast(encodeStateValidator);
        pipeline.addLast(connectionHandler.newPushStreamValidationHandler(ch, Http3RequestStreamCodecState.NO_STATE));
        this.initPushStream(ch);
    }
    
    protected abstract void initPushStream(final QuicStreamChannel p0);
}
