/*
 * Copyright 2010-2011 Ning, Inc.
 *
 * Ning 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 com.ning.billing.entitlement.events;

import java.util.UUID;

import org.joda.time.DateTime;

import com.ning.billing.entitlement.events.user.ApiEvent;

public abstract class EventBase implements EntitlementEvent {

    private final long totalOrdering;
    private final UUID uuid;
    private final UUID subscriptionId;
    private final DateTime requestedDate;
    private final DateTime effectiveDate;
    private final DateTime processedDate;

    private long activeVersion;
    private boolean isActive;

    public EventBase(final EventBaseBuilder<?> builder) {
        this.totalOrdering = builder.getTotalOrdering();
        this.uuid = builder.getUuid();
        this.subscriptionId = builder.getSubscriptionId();
        this.requestedDate = builder.getRequestedDate();
        this.effectiveDate = builder.getEffectiveDate();
        this.processedDate = builder.getProcessedDate();

        this.activeVersion = builder.getActiveVersion();
        this.isActive = builder.isActive();
    }
/*
    public EventBase(UUID subscriptionId, DateTime requestedDate,
            DateTime effectiveDate, DateTime processedDate,
            long activeVersion, boolean isActive) {
        this(UUID.randomUUID(), subscriptionId, requestedDate, effectiveDate, processedDate, activeVersion, isActive);
    }

    public EventBase(UUID id, UUID subscriptionId, DateTime requestedDate,
            DateTime effectiveDate, DateTime processedDate,
            long activeVersion, boolean isActive) {
        this.uuid = id;
        this.subscriptionId = subscriptionId;
        this.requestedDate = requestedDate;
        this.effectiveDate = effectiveDate;
        this.processedDate = processedDate;

        this.activeVersion = activeVersion;
        this.isActive = isActive;
    }
*/

    @Override
    public DateTime getRequestedDate() {
        return requestedDate;
    }

    @Override
    public DateTime getEffectiveDate() {
        return effectiveDate;
    }

    @Override
    public DateTime getProcessedDate() {
        return processedDate;
    }

    @Override
    public UUID getSubscriptionId() {
        return subscriptionId;
    }

    @Override
    public long getTotalOrdering() {
        return totalOrdering;
    }


    @Override
    public UUID getId() {
        return uuid;
    }

    @Override
    public long getActiveVersion() {
        return activeVersion;
    }

    @Override
    public void setActiveVersion(final long activeVersion) {
        this.activeVersion = activeVersion;
    }

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

    @Override
    public void deactivate() {
        this.isActive = false;
    }

    @Override
    public void reactivate() {
        this.isActive = true;
    }


    //
    // Really used for unit tests only as the sql implementation relies on date first and then event insertion
    //
    // Order first by:
    // - effectiveDate, followed by processedDate, requestedDate
    // - if all dates are equal-- unlikely, we first return PHASE EVENTS
    // - If both events are User events, return the first CREATE, CHANGE,... as specified by ApiEventType
    // - If all that is not enough return consistent by random ordering based on UUID
    //
    @Override
    public int compareTo(final EntitlementEvent other) {
        if (other == null) {
            throw new NullPointerException("IEvent is compared to a null instance");
        }

        if (effectiveDate.isBefore(other.getEffectiveDate())) {
            return -1;
        } else if (effectiveDate.isAfter(other.getEffectiveDate())) {
            return 1;
        } else if (processedDate.isBefore(other.getProcessedDate())) {
            return -1;
        } else if (processedDate.isAfter(other.getProcessedDate())) {
            return 1;
        } else if (requestedDate.isBefore(other.getRequestedDate())) {
            return -1;
        } else if (requestedDate.isAfter(other.getRequestedDate())) {
            return 1;
        } else if (getType() != other.getType()) {
            return (getType() == EventType.PHASE) ? -1 : 1;
        } else if (getType() == EventType.API_USER) {
            return ((ApiEvent) this).getEventType().compareTo(((ApiEvent) other).getEventType());
        } else {
            return uuid.compareTo(other.getId());
        }
    }


    @Override
    public boolean equals(final Object other) {
        if (!(other instanceof EntitlementEvent)) {
            return false;
        }
        return (this.compareTo((EntitlementEvent) other) == 0);
    }

    @Override
    public abstract EventType getType();


}

@@ -840,17 +840,16 @@
 Event %7B%0A
-%0A
     priv
@@ -1520,17 +1520,16 @@
 Date();%0A
-%0A
         
@@ -1630,792 +1630,8 @@
    %7D
-%0A/*%0A    public EventBase(UUID subscriptionId, DateTime requestedDate,%0A            DateTime effectiveDate, DateTime processedDate,%0A            long activeVersion, boolean isActive) %7B%0A        this(UUID.randomUUID(), subscriptionId, requestedDate, effectiveDate, processedDate, activeVersion, isActive);%0A    %7D%0A%0A    public EventBase(UUID id, UUID subscriptionId, DateTime requestedDate,%0A            DateTime effectiveDate, DateTime processedDate,%0A            long activeVersion, boolean isActive) %7B%0A        this.uuid = id;%0A        this.subscriptionId = subscriptionId;%0A        this.requestedDate = requestedDate;%0A        this.effectiveDate = effectiveDate;%0A        this.processedDate = processedDate;%0A%0A        this.activeVersion = activeVersion;%0A        this.isActive = isActive;%0A    %7D%0A*/
 %0A%0A  
@@ -2074,33 +2074,32 @@
 rdering;%0A    %7D%0A%0A
-%0A
     @Override%0A  
@@ -2603,25 +2603,24 @@
 rue;%0A    %7D%0A%0A
-%0A
     //%0A    /
@@ -3218,19 +3218,23 @@
 new 
-NullPointer
+IllegalArgument
 Exce
@@ -4190,17 +4190,16 @@
 %0A    %7D%0A%0A
-%0A
     @Ove
@@ -4466,12 +4466,10 @@
 Type();%0A
-%0A%0A
 %7D%0A

throw IllegalArgumentException, not NPE