/*******************************************************************************
 * Copyright (c) 2024-2025 R3944Realms. All rights reserved.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 *     http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package com.r3944realms.dg_lab.websocket.handler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

/**
 * The type Abstract dg lab power box handler.
 */
public abstract class AbstractDgLabPowerBoxHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {

    /**
     * The Context wrapper.
     */
    protected final AbstractDgLabPowerBoxHandlerContextWrapper contextWrapper;

    /**
     * Instantiates a new Abstract dg lab power box handler.
     *
     * @param contextWrapper the context wrapper
     */
    public AbstractDgLabPowerBoxHandler(AbstractDgLabPowerBoxHandlerContextWrapper contextWrapper) {
        this.contextWrapper = contextWrapper;
    }

    @Override
    public void handlerAdded(ChannelHandlerContext session) throws Exception {
        super.handlerAdded(session);
        contextWrapper.SessionBuildInHandle(session);
    }

    @Override
    public void channelActive(ChannelHandlerContext session) throws Exception {
        super.channelActive(session);
        contextWrapper.ActiveSessionHandle(session);
    }

    @Override
    public void handlerRemoved(ChannelHandlerContext session) throws Exception {
        super.handlerRemoved(session);
        contextWrapper.SessionCloseHandle(session);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext session, Throwable cause) {
        contextWrapper.ErrorHandler(session, cause);
    }

    @Override
    protected void channelRead0(ChannelHandlerContext session, TextWebSocketFrame msg) {
        contextWrapper.ReadMsgHandle(session, msg);
    }


}
