/*
 * Copyright 2017 Luca Boldrin.
 *
 * 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
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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 jtools.telnet.option;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import jtools.telnet.TelnetCommand;
import jtools.telnet.event.SubNegotiateListener;

/**
 *
 * @author Luca Boldrin
 * @version %G%,%I%
 */
public abstract class AbstractTelnetOptionHandler implements TelnetOptionHandler {

    protected boolean initLocal;
    protected boolean acceptRemote;
    protected boolean active;
    protected int code;
    protected ArrayList<SubNegotiateListener> listeners;
    
    public AbstractTelnetOptionHandler(int Code, boolean InitLocal, boolean AcceptRemote) {
        this.code = Code;
        this.initLocal = InitLocal;
        this.acceptRemote = AcceptRemote;
        this.active = false;
        listeners = new ArrayList<>();
    }
    
    @Override
    public boolean acceptRemote() {
        return acceptRemote;
    }

    @Override
    public boolean initLocal() {
        return initLocal;
    }

    @Override
    public boolean isActive() {
        return active;
    }

    @Override
    public int getCode() {
        return code;
    }

    @Override
    public int processCommand(int Command) {
        int response=0;
        if (acceptRemote) {
            switch(Command) {
                case TelnetCommand.DO :
                    response = TelnetCommand.WILL;
                    active = true;
                    break;
                case TelnetCommand.WILL :
                    response = TelnetCommand.DO;
                    active = true;
                    break;
                case TelnetCommand.DONT :
                    response = TelnetCommand.WONT;
                    active = false;
                    break;
                case TelnetCommand.WONT :
                    response = TelnetCommand.DONT;
                    active = false;
                    break;
            }
        } else {
            switch(Command) {
                case TelnetCommand.DO :
                    response =  TelnetCommand.WONT;
                    active = false;
                    break;
                case TelnetCommand.WILL :
                    response = TelnetCommand.DONT;
                    active = false;
                    break;
                case TelnetCommand.DONT :
                    response = TelnetCommand.WONT;
                    active = false;
                    break;
                case TelnetCommand.WONT :
                    response = TelnetCommand.DONT;
                    active = false;
                    break;
            }
        }
        return response;
    }
    
    public void addListener(SubNegotiateListener listener) {
        listeners.add(listener);
    }
    
    public void removeListener(SubNegotiateListener listener) {
        listeners.remove(listener);
    }
    
    @Override
    abstract public ByteBuffer getSubnegotiationMessage();
    
}
