/*
 * 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.gh.bmd.jrt.android.v11.core.RoutineContext mContext;

    private final Object[] mFactoryArgs;

    private final String mShareGroup;

    private final Class<?> mTargetClass;

    @SuppressWarnings("ConstantConditions")
    public ${generatedClassName}(@javax.annotation.Nonnull final com.gh.bmd.jrt.android.v11.core.RoutineContext context,
            @javax.annotation.Nonnull final Class<?> targetClass,
            @javax.annotation.Nullable final Object[] factoryArgs,
            @javax.annotation.Nonnull final com.gh.bmd.jrt.builder.InvocationConfiguration invocationConfiguration,
            @javax.annotation.Nonnull final com.gh.bmd.jrt.builder.ProxyConfiguration proxyConfiguration,
            @javax.annotation.Nonnull final com.gh.bmd.jrt.android.builder.LoaderConfiguration loaderConfiguration) {

        if (context == null) {

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

        if (targetClass == null) {

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

        mContext = context;
        mTargetClass = targetClass;
        mFactoryArgs = (factoryArgs != null) ? factoryArgs : com.gh.bmd.jrt.util.Reflection.NO_ARGS;
        mShareGroup = proxyConfiguration.getShareGroupOr(com.gh.bmd.jrt.annotation.ShareGroup.ALL);
        ${routineFieldsInit}
    }

    @javax.annotation.Nonnull
    public static ${genericTypes} com.gh.bmd.jrt.android.proxy.builder.LoaderProxyBuilder<${interfaceFullName}> on(
            @javax.annotation.Nonnull final com.gh.bmd.jrt.android.v11.core.RoutineContext context,
            @javax.annotation.Nonnull final Class<?> targetClass) {

        return on(context, targetClass, (Object[]) null);
    }

    @javax.annotation.Nonnull
    public static ${genericTypes} com.gh.bmd.jrt.android.proxy.builder.LoaderProxyBuilder<${interfaceFullName}> on(
            @javax.annotation.Nonnull final com.gh.bmd.jrt.android.v11.core.RoutineContext context,
            @javax.annotation.Nonnull final Class<?> targetClass,
            @javax.annotation.Nullable final Object... factoryArgs) {

        return new LoaderProxyBuilder${genericTypes}(context, targetClass, factoryArgs);
    }

    @javax.annotation.Nonnull
    private static <OUTPUT> com.gh.bmd.jrt.android.builder.LoaderRoutineBuilder<Object, OUTPUT> getBuilder(
            @javax.annotation.Nonnull final com.gh.bmd.jrt.android.v11.core.RoutineContext context,
            @javax.annotation.Nonnull final com.gh.bmd.jrt.android.invocation.ContextInvocationFactory<Object, OUTPUT> factory) {

        return com.gh.bmd.jrt.android.v11.core.JRoutine.on(context, factory);
    }

    @javax.annotation.Nonnull
    @SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter")
    private static Object getInstance(@javax.annotation.Nonnull final android.content.Context context,
            @javax.annotation.Nonnull final Class<?> targetClass, @javax.annotation.Nonnull final Object[] args)
            throws IllegalAccessException, java.lang.reflect.InvocationTargetException, InstantiationException {

        Object target = null;

        if (context instanceof com.gh.bmd.jrt.android.builder.FactoryContext) {

            // the only safe way is to synchronize the factory using the very same instance
            synchronized (context) {

                target = ((com.gh.bmd.jrt.android.builder.FactoryContext) context).geInstance(targetClass, args);
            }
        }

        if (target == null) {

            target = com.gh.bmd.jrt.util.Reflection.findConstructor(targetClass, args).newInstance(args);

        } else if (!targetClass.isInstance(target)) {

            throw new InstantiationException();
        }

        return target;
    }

    @javax.annotation.Nullable
    private static Object getMutex(@javax.annotation.Nonnull final Object target,
            @javax.annotation.Nullable final String shareGroup) {

        if (com.gh.bmd.jrt.annotation.ShareGroup.NONE.equals(shareGroup)) {

            return null;
        }

        return com.gh.bmd.jrt.core.RoutineBuilders.getSharedMutex(target, shareGroup);
    }

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

        private final com.gh.bmd.jrt.android.v11.core.RoutineContext mContext;

        private final Object[] mFactoryArgs;

        private final Class<?> mTargetClass;

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

        @SuppressWarnings("ConstantConditions")
        private LoaderProxyBuilder(@javax.annotation.Nonnull final com.gh.bmd.jrt.android.v11.core.RoutineContext context,
                @javax.annotation.Nonnull final Class<?> targetClass, @javax.annotation.Nullable final Object... factoryArgs) {

            if (context == null) {

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

            if (targetClass == null) {

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

            mContext = context;
            mTargetClass = targetClass;
            mFactoryArgs = factoryArgs;
        }

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

            return mToken;
        }

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

            return mTargetClass;
        }

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

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