/*
 * Copyright 2017 the original author or authors.
 *
 * 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.
 */

package org.gradle.internal.enterprise.impl;

import com.google.common.base.Strings;
import org.gradle.api.Project;
import org.gradle.api.artifacts.ModuleIdentifier;
import org.gradle.api.artifacts.component.ModuleComponentSelector;
import org.gradle.api.initialization.Settings;
import org.gradle.api.internal.StartParameterInternal;
import org.gradle.api.internal.artifacts.DefaultModuleIdentifier;
import org.gradle.api.invocation.Gradle;
import org.gradle.internal.component.external.model.DefaultModuleComponentSelector;
import org.gradle.internal.service.scopes.Scope;
import org.gradle.internal.service.scopes.ServiceScope;
import org.gradle.plugin.management.internal.DefaultPluginRequest;
import org.gradle.plugin.management.internal.PluginCoordinates;
import org.gradle.plugin.management.internal.PluginRequestInternal;
import org.gradle.plugin.management.internal.PluginRequests;
import org.gradle.plugin.management.internal.autoapply.AutoAppliedDevelocityPlugin;
import org.gradle.plugin.management.internal.autoapply.AutoAppliedPluginRegistry;
import org.gradle.util.internal.VersionNumber;
import org.jspecify.annotations.Nullable;

import static org.gradle.initialization.StartParameterBuildOptions.BuildScanOption;
import static org.gradle.plugin.management.internal.PluginRequestInternal.Origin.AUTO_APPLIED;

@ServiceScope(Scope.BuildTree.class)
public class GradleEnterpriseAutoAppliedPluginRegistry implements AutoAppliedPluginRegistry {

    @Override
    public PluginRequests getAutoAppliedPlugins(Project target) {
        return PluginRequests.EMPTY;
    }

    @Override
    public PluginRequests getAutoAppliedPlugins(Settings target) {
        StartParameterInternal startParameter = (StartParameterInternal) target.getStartParameter();
        if (startParameter.isUseEmptySettings() || !shouldApplyDevelocityPlugin(target)) {
            return PluginRequests.EMPTY;
        } else {
            String develocityPluginVersion = startParameter.getDevelocityPluginVersion();
            boolean isDevelocityURLSpecified = !Strings.isNullOrEmpty(startParameter.getDevelocityUrl());
            if (isDevelocityURLSpecified && develocityPluginVersion != null) {
                // Validate that the version is at least 4.4.0
                if (VersionNumber.parse(develocityPluginVersion).compareTo(VersionNumber.version(4, 4)) < 0) {
                    throw new IllegalArgumentException(String.format(
                        "The specified Develocity plugin version '%s' is not supported. Version 4.4.0 or higher is required when using a custom Develocity URL.",
                        develocityPluginVersion
                    ));
                }
            }
            return PluginRequests.of(createDevelocityPluginRequest(develocityPluginVersion));
        }
    }

    private static boolean shouldApplyDevelocityPlugin(Settings settings) {
        Gradle gradle = settings.getGradle();
        StartParameterInternal startParameter = (StartParameterInternal) gradle.getStartParameter();
        return (startParameter.isBuildScan() || !Strings.isNullOrEmpty(startParameter.getDevelocityUrl())) && gradle.getParent() == null;
    }

    private static PluginRequestInternal createDevelocityPluginRequest(@Nullable String pluginVersion) {
        String versionToApply = pluginVersion == null ? AutoAppliedDevelocityPlugin.VERSION : pluginVersion;
        ModuleIdentifier moduleIdentifier = DefaultModuleIdentifier.newId(AutoAppliedDevelocityPlugin.GROUP, AutoAppliedDevelocityPlugin.NAME);
        ModuleComponentSelector selector = DefaultModuleComponentSelector.newSelector(moduleIdentifier, versionToApply);
        return new DefaultPluginRequest(
            AutoAppliedDevelocityPlugin.ID,
            true,
            AUTO_APPLIED,
            getScriptDisplayName(),
            null,
            versionToApply,
            selector,
            null,
            gradleEnterprisePluginCoordinates(versionToApply)
        );
    }

    private static PluginCoordinates gradleEnterprisePluginCoordinates(String versionToApply) {
        ModuleIdentifier moduleIdentifier = DefaultModuleIdentifier.newId(AutoAppliedDevelocityPlugin.GROUP, AutoAppliedDevelocityPlugin.GRADLE_ENTERPRISE_PLUGIN_ARTIFACT_NAME);
        ModuleComponentSelector selector = DefaultModuleComponentSelector.newSelector(moduleIdentifier, versionToApply);
        return new PluginCoordinates(AutoAppliedDevelocityPlugin.GRADLE_ENTERPRISE_PLUGIN_ID, selector);
    }

    private static String getScriptDisplayName() {
        return String.format("auto-applied by using --%s", BuildScanOption.LONG_OPTION);
    }
}
