/*
 * Copyright 2010-2013 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.subscription.api.user;

import java.lang.reflect.Field;
import java.util.UUID;

import org.joda.time.DateTime;

import com.ning.billing.catalog.api.ProductCategory;
import com.ning.billing.subscription.exceptions.SubscriptionBaseError;

public class SubscriptionBuilder {
    private UUID id;
    private UUID bundleId;
    private DateTime createdDate;
    private DateTime updatedDate;
    private DateTime alignStartDate;
    private DateTime bundleStartDate;
    private Long activeVersion;
    private ProductCategory category;
    private DateTime chargedThroughDate;
    private DateTime paidThroughDate;

    public SubscriptionBuilder() {
        this.activeVersion = SubscriptionEvents.INITIAL_VERSION;
    }

    public SubscriptionBuilder(final DefaultSubscriptionBase original) {
        this.id = original.getId();
        this.bundleId = original.getBundleId();
        this.alignStartDate = original.getAlignStartDate();
        this.bundleStartDate = original.getBundleStartDate();
        this.category = original.getCategory();
        this.activeVersion = original.getActiveVersion();
        this.chargedThroughDate = original.getChargedThroughDate();
        this.paidThroughDate = original.getPaidThroughDate();
    }

    public SubscriptionBuilder setId(final UUID id) {
        this.id = id;
        return this;
    }

    public SubscriptionBuilder setCreatedDate(final DateTime createdDate) {
        this.createdDate = createdDate;
        return this;
    }

    public SubscriptionBuilder setUpdatedDate(final DateTime updatedDate) {
        this.updatedDate = updatedDate;
        return this;
    }

    public SubscriptionBuilder setBundleId(final UUID bundleId) {
        this.bundleId = bundleId;
        return this;
    }

    public SubscriptionBuilder setAlignStartDate(final DateTime alignStartDate) {
        this.alignStartDate = alignStartDate;
        return this;
    }

    public SubscriptionBuilder setBundleStartDate(final DateTime bundleStartDate) {
        this.bundleStartDate = bundleStartDate;
        return this;
    }

    public SubscriptionBuilder setActiveVersion(final long activeVersion) {
        this.activeVersion = activeVersion;
        return this;
    }

    public SubscriptionBuilder setChargedThroughDate(final DateTime chargedThroughDate) {
        this.chargedThroughDate = chargedThroughDate;
        return this;
    }

    public SubscriptionBuilder setPaidThroughDate(final DateTime paidThroughDate) {
        this.paidThroughDate = paidThroughDate;
        return this;
    }

    public SubscriptionBuilder setCategory(final ProductCategory category) {
        this.category = category;
        return this;
    }

    public UUID getId() {
        return id;
    }

    public DateTime getCreatedDate() {
        return createdDate;
    }

    public DateTime getUpdatedDate() {
        return updatedDate;
    }

    public UUID getBundleId() {
        return bundleId;
    }

    public DateTime getAlignStartDate() {
        return alignStartDate;
    }

    public DateTime getBundleStartDate() {
        return bundleStartDate;
    }

    public Long getActiveVersion() {
        return activeVersion;
    }

    public ProductCategory getCategory() {
        return category;
    }

    public DateTime getChargedThroughDate() {
        return chargedThroughDate;
    }

    public DateTime getPaidThroughDate() {
        return paidThroughDate;
    }

    private void checkAllFieldsSet() {
        for (final Field cur : SubscriptionBuilder.class.getDeclaredFields()) {
            try {
                final Object value = cur.get(this);
                if (value == null) {
                    throw new SubscriptionBaseError(String.format("Field %s has not been set for SubscriptionBase",
                                                             cur.getName()));
                }
            } catch (IllegalAccessException e) {
                throw new SubscriptionBaseError(String.format("Failed to access value for field %s for SubscriptionBase",
                                                         cur.getName()), e);
            }
        }
    }

}
