/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.activeblaze;

import org.apache.activeblaze.wire.Buffer;
import org.apache.activeblaze.wire.BufferInputStream;
import org.apache.activeblaze.wire.BufferOutputStream;
import org.apache.activeblaze.wire.ByteBool;

import java.io.IOException;

/**
 * Holds information about a Destination
 */
public class Destination {
    private Buffer name;
    private boolean topic;
    private boolean temporary;


    /**
     * Constructor
     */
    public Destination() {
    }

    /**
     * Constructor
     */
    public Destination(Buffer name, boolean topic) {
        this.name = name;
        this.topic = topic;
    }

    /**
     * Constructor
     */
    public Destination(String name, boolean topic) {
        this.name = new Buffer(name);
        this.topic = topic;
    }

    /**
     * Get the name
     *
     * @return the name
     */
    public Buffer getName() {
        return this.name;
    }

    /**
     * Set the name
     *
     * @param name the name to set
     */
    public void setName(Buffer name) {
        this.name = name;
    }

    /**
     * Get the topic
     *
     * @return the topic
     */
    public boolean isTopic() {
        return this.topic;
    }

    /**
     * Set the topic
     *
     * @param topic the topic to set
     */
    public void setTopic(boolean topic) {
        this.topic = topic;
    }

    /**
     * Get the queue
     *
     * @return the queue
     */
    public boolean isQueue() {
        return !isTopic();
    }

    /**
     * Get the temporary
     *
     * @return the temporary
     */
    public boolean isTemporary() {
        return this.temporary;
    }

    /**
     * Set the temporary
     *
     * @param temporary the temporary to set
     */
    public void setTemporary(boolean temporary) {
        this.temporary = temporary;
    }

    public void write(BufferOutputStream out) throws IOException {
        ByteBool byteBool = new ByteBool();
        byteBool.writeBoolean(this.topic);
        byteBool.writeBoolean(this.temporary);
        out.writeByte(byteBool.getData());
        out.write(this.name);
    }

    public void read(BufferInputStream in) throws IOException {
        ByteBool byteBool = new ByteBool();
        byteBool.setData(in.readByte());
        this.topic = byteBool.readBoolean();
        this.temporary = byteBool.readBoolean();
        this.name = in.readBuffer();
    }

    public String toString() {
        return "Destination{" + (isTopic() ? "Topic" : "Queue") + ": " + getName() + "}";
    }
}
