/*
 * 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.core.ServiceContext mContext;

    @SuppressWarnings("ConstantConditions")
    public ${generatedClassName}(@org.jetbrains.annotations.NotNull final com.github.dm.jrt.android.core.ServiceContext context,
            @org.jetbrains.annotations.NotNull final com.github.dm.jrt.android.core.ContextInvocationTarget<?> target,
            @org.jetbrains.annotations.NotNull final com.github.dm.jrt.builder.InvocationConfiguration invocationConfiguration,
            @org.jetbrains.annotations.NotNull final com.github.dm.jrt.builder.ProxyConfiguration proxyConfiguration,
            @org.jetbrains.annotations.NotNull final com.github.dm.jrt.android.builder.ServiceConfiguration serviceConfiguration) {

        if (context == null) {

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

        if (target.getTargetClass().isPrimitive()) {

            // The parceling of primitive classes is broken...
            throw new IllegalArgumentException("the target class cannot be primitive");
        }

        if (!target.isSameTypeOf(${classErasure}.class)) {

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

        if (invocationConfiguration == null) {

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

        if (proxyConfiguration == null) {

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

        if (serviceConfiguration == null) {

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

        mContext = context;
        ${routineFieldsInit}
    }

    @org.jetbrains.annotations.NotNull
    public static ContextBuilder with(@org.jetbrains.annotations.NotNull final com.github.dm.jrt.android.core.ServiceContext context) {

        return new ContextBuilder(context);
    }

    @org.jetbrains.annotations.NotNull
    private static <OUT> com.github.dm.jrt.android.builder.ServiceRoutineBuilder<Object, OUT> getBuilder(
            @org.jetbrains.annotations.NotNull final com.github.dm.jrt.android.core.ServiceContext context,
            @org.jetbrains.annotations.NotNull final com.github.dm.jrt.android.core.TargetInvocationFactory<Object, OUT> target) {

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

    public static class ContextBuilder {

        private final com.github.dm.jrt.android.core.ServiceContext mContext;

        @SuppressWarnings("ConstantConditions")
        private ContextBuilder(@org.jetbrains.annotations.NotNull final com.github.dm.jrt.android.core.ServiceContext context) {

            if (context == null) {

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

            mContext = context;
        }

        @org.jetbrains.annotations.NotNull
        public ${genericTypes} com.github.dm.jrt.android.proxy.builder.ServiceProxyObjectBuilder<${interfaceFullName}> on(@org.jetbrains.annotations.NotNull final com.github.dm.jrt.android.core.ContextInvocationTarget.ObjectContextInvocationTarget<? extends ${classFullName}> target) {

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

        @org.jetbrains.annotations.NotNull
        public ${genericTypes} com.github.dm.jrt.android.proxy.builder.ServiceProxyObjectBuilder<${interfaceFullName}> onClass() {

            return new ServiceProxyObjectBuilder${genericTypes}(mContext, com.github.dm.jrt.android.core.ContextInvocationTarget.classOfType(${classErasure}.class));
        }
    }

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

        private final com.github.dm.jrt.android.core.ServiceContext mContext;

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

        @SuppressWarnings("ConstantConditions")
        private ServiceProxyObjectBuilder(@org.jetbrains.annotations.NotNull final com.github.dm.jrt.android.core.ServiceContext context,
                @org.jetbrains.annotations.NotNull final com.github.dm.jrt.android.core.ContextInvocationTarget<?> target) {

            if (context == null) {

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

            if (target.getTargetClass().isPrimitive()) {

                // The parceling of primitive classes is broken...
                throw new IllegalArgumentException("the target class cannot be primitive");
            }

            mContext = context;
            mTarget = target;
        }

        @Override
        @org.jetbrains.annotations.NotNull
        protected Class<? super ${interfaceFullName}> getInterfaceClass() {

            return ${interfaceErasure}.class;
        }

        @Override
        @org.jetbrains.annotations.Nullable
        protected android.content.Context getInvocationContext() {

            return mContext.getServiceContext();
        }

        @Override
        @org.jetbrains.annotations.NotNull
        protected Class<?> getTargetClass() {

            return mTarget.getTargetClass();
        }

        @Override
        @org.jetbrains.annotations.NotNull
        protected ${interfaceFullName} newProxy(
                @org.jetbrains.annotations.NotNull final com.github.dm.jrt.builder.InvocationConfiguration invocationConfiguration,
                @org.jetbrains.annotations.NotNull final com.github.dm.jrt.builder.ProxyConfiguration proxyConfiguration,
                @org.jetbrains.annotations.NotNull final com.github.dm.jrt.android.builder.ServiceConfiguration serviceConfiguration) {

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