package rx.internal.operators;

import java.util.Arrays;

import rx.*;
import rx.exceptions.*;
import rx.functions.*;
import rx.plugins.RxJavaPlugins;

/**
 * Generates a resource, derives a Single from it and disposes that resource once the
 * Single terminates.
 * @param <T> the value type of the Single
 * @param <Resource> the resource type
 */
public final class SingleOnSubscribeUsing<T, Resource> implements Single.OnSubscribe<T> {
    final Func0<Resource> resourceFactory;
    final Func1<? super Resource, ? extends Single<? extends T>> singleFactory;
    final Action1<? super Resource> disposeAction; 
    final boolean disposeEagerly;

    public SingleOnSubscribeUsing(Func0<Resource> resourceFactory,
            Func1<? super Resource, ? extends Single<? extends T>> observableFactory,
            Action1<? super Resource> disposeAction, boolean disposeEagerly) {
        this.resourceFactory = resourceFactory;
        this.singleFactory = observableFactory;
        this.disposeAction = disposeAction;
        this.disposeEagerly = disposeEagerly;
    }
    
    @Override
    public void call(final SingleSubscriber<? super T> child) {
        final Resource resource;
        
        try {
            resource = resourceFactory.call();
        } catch (Throwable ex) {
            Exceptions.throwIfFatal(ex);
            child.onError(ex);
            return;
        }
        
        Single<? extends T> single;
        
        try {
            single = singleFactory.call(resource);
        } catch (Throwable ex) {
            handleSubscriptionTimeError(child, resource, ex);
            return;
        }
        
        if (single == null) {
            handleSubscriptionTimeError(child, resource, new NullPointerException("The single"));
            return;
        }
        
        SingleSubscriber<T> parent = new SingleSubscriber<T>() {
            @Override
            public void onSuccess(T value) {
                if (disposeEagerly) {
                    try {
                        disposeAction.call(resource);
                    } catch (Throwable ex) {
                        Exceptions.throwIfFatal(ex);
                        
                        child.onError(ex);
                        return;
                    }
                }
                
                child.onSuccess(value);
                
                if (!disposeEagerly) {
                    try {
                        disposeAction.call(resource);
                    } catch (Throwable ex2) {
                        Exceptions.throwIfFatal(ex2);
                        RxJavaPlugins.getInstance().getErrorHandler().handleError(ex2);
                    }
                }
            }
            
            @Override
            public void onError(Throwable error) {
                handleSubscriptionTimeError(child, resource, error);
            }
        };
        child.add(parent);
        
        single.subscribe(parent);
    }

    void handleSubscriptionTimeError(SingleSubscriber<? super T> t, Resource resource, Throwable ex) {
        Exceptions.throwIfFatal(ex);

        if (disposeEagerly) {
            try {
                disposeAction.call(resource);
            } catch (Throwable ex2) {
                Exceptions.throwIfFatal(ex2);
                ex = new CompositeException(Arrays.asList(ex, ex2));
            }
        }
        
        t.onError(ex);
        
        if (!disposeEagerly) {
            try {
                disposeAction.call(resource);
            } catch (Throwable ex2) {
                Exceptions.throwIfFatal(ex2);
                RxJavaPlugins.getInstance().getErrorHandler().handleError(ex2);
            }
        }
    }
}
