/*
 * Licensed 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.
 */

// GENERATED CODE - PLEASE DO NOT EDIT

${generatedPackage}

public final class ${generatedClassName}${genericTypes} implements ${interfaceFullName} {

    private final com.github.dm.jrt.android.v4.core.LoaderContext mContext;

    @SuppressWarnings("ConstantConditions")
    public ${generatedClassName}(@javax.annotation.Nonnull final com.github.dm.jrt.android.v4.core.LoaderContext context,
            @javax.annotation.Nonnull final com.github.dm.jrt.android.core.ContextInvocationTarget target,
            @javax.annotation.Nonnull final com.github.dm.jrt.builder.InvocationConfiguration invocationConfiguration,
            @javax.annotation.Nonnull final com.github.dm.jrt.builder.ProxyConfiguration proxyConfiguration,
            @javax.annotation.Nonnull final com.github.dm.jrt.android.builder.LoaderConfiguration loaderConfiguration) {

        if (context == null) {

            throw new NullPointerException("the routine context must not be null");
        }

        if (!${classErasure}.class.isAssignableFrom(target.getTargetClass())) {

            throw new IllegalArgumentException("the invocation target must be the class or an instance of " +
                    "${classFullName}");
        }

        mContext = context;
        ${routineFieldsInit}
    }

    @javax.annotation.Nonnull
    public static ContextBuilder on(@javax.annotation.Nonnull final com.github.dm.jrt.android.v4.core.LoaderContext context) {

        return new ContextBuilder(context);
    }

    @javax.annotation.Nonnull
    private static <OUT> com.github.dm.jrt.android.builder.LoaderRoutineBuilder<Object, OUT> getBuilder(
            @javax.annotation.Nonnull final com.github.dm.jrt.android.v4.core.LoaderContext context,
            @javax.annotation.Nonnull final com.github.dm.jrt.android.invocation.ContextInvocationFactory<Object, OUT> factory) {

        return com.github.dm.jrt.android.v4.core.JRoutine.on(context).with(factory);
    }

    public static class ContextBuilder {

        private final com.github.dm.jrt.android.v4.core.LoaderContext mContext;

        @SuppressWarnings("ConstantConditions")
        private ContextBuilder(@javax.annotation.Nonnull final com.github.dm.jrt.android.v4.core.LoaderContext context) {

            if (context == null) {

                throw new NullPointerException("the context must not be null");
            }

            mContext = context;
        }

        @javax.annotation.Nonnull
        public ${genericTypes} com.github.dm.jrt.android.proxy.builder.LoaderProxyBuilder<${interfaceFullName}> with(
                @javax.annotation.Nonnull final com.github.dm.jrt.android.core.ContextInvocationTarget target) {

            return new LoaderProxyBuilder${genericTypes}(mContext, target);
        }
    }

    private static class LoaderProxyBuilder${genericTypes} extends com.github.dm.jrt.android.proxy.builder.AbstractLoaderProxyBuilder<${interfaceFullName}> {

        private final com.github.dm.jrt.android.v4.core.LoaderContext mContext;

        private final com.github.dm.jrt.android.core.ContextInvocationTarget mTarget;

        private final com.github.dm.jrt.util.ClassToken<${interfaceFullName}> mToken = new com.github.dm.jrt.util.ClassToken<${interfaceFullName}>() {};

        @SuppressWarnings("ConstantConditions")
        private LoaderProxyBuilder(@javax.annotation.Nonnull final com.github.dm.jrt.android.v4.core.LoaderContext context,
                @javax.annotation.Nonnull final com.github.dm.jrt.android.core.ContextInvocationTarget target) {

            if (context == null) {

                throw new NullPointerException("the routine context must not be null");
            }

            if (target == null) {

                throw new NullPointerException("the invocation target must not be null");
            }

            mContext = context;
            mTarget = target;
        }

        @Override
        @javax.annotation.Nonnull
        protected com.github.dm.jrt.util.ClassToken<${interfaceFullName}> getInterfaceToken() {

            return mToken;
        }

        @Override
        @javax.annotation.Nullable
        protected Object getInvocationContext() {

            return mContext.getComponent();
        }

        @Override
        @javax.annotation.Nonnull
        protected Class<?> getTargetClass() {

            return mTarget.getTargetClass();
        }

        @Override
        @javax.annotation.Nonnull
        protected ${interfaceFullName} newProxy(
                @javax.annotation.Nonnull final com.github.dm.jrt.builder.InvocationConfiguration invocationConfiguration,
                @javax.annotation.Nonnull final com.github.dm.jrt.builder.ProxyConfiguration proxyConfiguration,
                @javax.annotation.Nonnull final com.github.dm.jrt.android.builder.LoaderConfiguration loaderConfiguration) {

            return new ${generatedClassName}${genericTypes}(mContext, mTarget, invocationConfiguration, proxyConfiguration, loaderConfiguration);
        }
    }
