/**
 * 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.impl.destination.DestinationMatch;
import org.apache.activeblaze.wire.Buffer;
import org.apache.activeblaze.wire.BufferInputStream;
import org.apache.activeblaze.wire.BufferOutputStream;

import java.io.IOException;

/**
 * Subscription Info
 */
public class Subscription {
    private String channelName;
    private String selector;
    private String subscriberName;
    private boolean durable;
    private boolean noLocal;
    private int weight;
    private Destination destination;

    /**
     * Default Constructor
     */
    public Subscription() {
    }


    /**
     * Constructor - create a vanilla subscription
     */
    public Subscription(String destination, boolean topic) {
        this.destination = new Destination(new Buffer(destination), topic);
    }

    /**
     * Constructor - create a vanilla subscription
     */
    public Subscription(Buffer destination, boolean topic) {
        this.destination = new Destination(destination, topic);
    }


    /**
     * set the durability of the subscription
     */
    public void setDurable(boolean value) {
        this.durable = value;
    }

    /**
     * @return true if durable
     */
    public boolean isDurable() {
        return this.durable;
    }

    /**
     * Set the weight - for Queues - higher weighted subscribers take precedence
     */
    public void setWeight(int value) {
        this.weight = value;
    }

    /**
     * @return the weight
     */
    public int getWeight() {
        return this.weight;
    }

    /**
     * Set the channel name of the subscription
     */
    public void setChannelName(String value) {
        this.channelName = value;
    }

    /**
     * @return the channel name of the subscription
     */
    public String getChannelName() {
        return this.channelName;
    }

    /**
     * Set the name of the subscriber
     */
    public void setSubscriberName(String value) {
        this.subscriberName = value;
    }

    /**
     * @return the name of the subscriber
     */
    public String getSubscriberName() {
        return this.subscriberName;
    }

    /**
     * Set a SQL92 selector
     */
    public void setSelector(String selector) {
        this.selector = selector;
    }

    /**
     * @return the selector
     */
    public String getSelector() {
        return this.selector;
    }

    /**
     * Set the Destination
     */
    public void setDestination(Destination destination) {
        this.destination = destination;
    }

    /**
     * @return Destination
     */
    public Destination getDestination() {
        return this.destination;
    }


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


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


    /**
     * @return noLocal
     */
    public boolean isNoLocal() {
        return this.noLocal;
    }

    /**
     * @param noLocal
     */
    public void setNoLocal(boolean noLocal) {
        this.noLocal = noLocal;
    }

    /**
     * @return hash code
     * @see java.lang.Object#hashCode()
     */
    public int hashCode() {
        return this.destination.getName().hashCode();
    }

    /**
     * @return true if equals <Code>this</Code>
     * @see java.lang.Object#equals(java.lang.Object)
     */
    public boolean equals(Object obj) {
        if (obj == this)
            return true;

        if (obj == null || obj.getClass() != Subscription.class)
            return false;

        return equals((Subscription) obj);
    }

    /**
     * @return true if other is equivalent
     */
    public boolean equals(Subscription other) {
        if (other != null) {
            return this.destination.getName().equals(other.destination.getName()) && this.destination.isTopic() == other.destination.isTopic();
        }
        return false;
    }

    /**
     * Does this subscription match a destination
     *
     * @return true if it matches a destination
     */
    public boolean matches(final Destination dest) {
        return matches(dest.getName());
    }

    /**
     * Does this subscription match a destination
     *
     * @return true if it matches a destination
     */
    public boolean matches(Buffer destinationName) {
        return DestinationMatch.isMatch(this.destination.getName(), destinationName);
    }


    public void write(BufferOutputStream out) throws IOException {
        out.writeBoolean(this.durable);
        out.writeBoolean(this.noLocal);
        out.writeString(getChannelName());
        out.writeString(getSelector());
        out.writeInt(getWeight());
        this.destination.write(out);
    }

    public void read(BufferInputStream in) throws IOException {

        this.durable = in.readBoolean();
        this.noLocal = in.readBoolean();
        this.channelName = in.readString();
        this.selector = in.readString();
        this.weight = in.readInt();
        this.destination = new Destination();
        this.destination.read(in);
    }


}
