package org.jerkar.tool.builtins.repos;

import org.jerkar.api.depmanagement.JkRepo;
import org.jerkar.api.system.JkLog;
import org.jerkar.api.utils.JkUtilsString;
import org.jerkar.tool.JkRun;
import org.jerkar.tool.JkDoc;
import org.jerkar.tool.JkPlugin;
import org.jerkar.tool.JkRepoConfigOptionLoader;

import java.util.Map;

/**
 * Plugin for defining repositories.
 */
@JkDoc("Provides configured repositories to download or upload artifacts.\n" +
        "  To select a 'download' (respectively a 'publish') repository, this plugin check in order : \n" +
        "    - the 'repos#downloadRepoName' option which designate the name of the configured plugin\n" +
        "    - the 'repos#downloadUrl', 'repos#downloadUsername' and 'repos#downloadPassword' options to instantiate a repo based on these values\n" +
        "    - the 'Repos.download.url', 'Repos.download.username' and 'Repo.download.password' options to instantiate repo based on these values\n" +
        "    - the default repo returned by JkRepo#ofMavenCentral() for downloading and local repository for publishing.\n" +
        "  To postPluginSetup a named repository, add following properties into your [Jerkar_user_home]/options.properties file :\n" +
        "    'Repos.[name].url', 'Repos.[value].username' and 'Repos.[of].password'"
)
public class JkPluginRepo extends JkPlugin {

    // ------------------------------ options -------------------------------------------

    @JkDoc("Url for the publish repository.")
    public String publishUrl;

    @JkDoc("Url for the download repository.")
    public String downloadUrl;

    @JkDoc("Username credential for the publish repository (if needed).")
    public String publishUsername;

    @JkDoc("Password for the publish repository (if needed).")
    public String publishPassword;

    @JkDoc("Username credential for the download repository (if needed).")
    public String downloadUsername;

    @JkDoc("Password for the download repository (if needed).")
    public String downloadPassword;

    @JkDoc("Name of the configured repository to use for publishing artifacts. If null or empty, considered as not set.")
    public String publishRepoName;

    @JkDoc("\"Name of the configured repository to use for publishing artifacts. If null or empty, considered as not set.")
    public String downloadRepoName;

    // ----------------------------------------------------------------------------------

    protected JkPluginRepo(JkRun run) {
        super(run);
    }

    public JkRepo publishRepository() {
        final JkRepo result;
        if (!JkUtilsString.isBlank(publishRepoName)) {
            return JkRepoConfigOptionLoader.repoFromOptions(publishRepoName);
        }
        if (!JkUtilsString.isBlank(publishUrl)) {
            return JkRepo.of(publishUrl).withOptionalCredentials(publishUsername, publishPassword);
        }
        JkRepo optionRepo = JkRepoConfigOptionLoader.publishRepository();
        return optionRepo != null ? optionRepo : JkRepo.ofLocal();
    }

    public JkRepo downloadRepository() {
        if (!JkUtilsString.isBlank(downloadPassword)) {
            return JkRepoConfigOptionLoader.repoFromOptions(downloadRepoName);
        }
        if (!JkUtilsString.isBlank(downloadUrl)) {
            return JkRepo.of(publishUrl).withOptionalCredentials(downloadUsername, downloadPassword);
        }
        return JkRepoConfigOptionLoader.downloadRepository();
    }

    @JkDoc("Displays active and configured repositories.")
    public void display() {
        StringBuilder sb = new StringBuilder();

        JkRepo download = downloadRepository();

        sb.append("Download repository")
                .append("\n  url : " + download.getUrl());
        if (download.getCredential() != null) {
            String downloadPwd = JkUtilsString.isBlank(download.getCredential().getPassword()) ? "" : downloadPassword.substring(0, 1) + "*******";
            sb.append("\n  username : " + download.getCredential().getUserName())
                    .append("\n  password : " + downloadPwd);
        }

        for (Map.Entry<String, String> entry : JkRepoConfigOptionLoader.allRepositoryOptions().entrySet()) {
            sb.append("\n" + entry.getKey() + " : ").append(entry.getValue());
        }
        JkLog.info(sb.toString());
    }



}

@@ -899,32 +899,39 @@
 wnload.password'
+ global
  options to inst
@@ -1114,23 +1114,17 @@
  To 
-postPluginSetup
+configure
  a n
@@ -3521,15 +3521,12 @@
 oid 
-display
+info
 () %7B
@@ -3570,25 +3570,24 @@
 gBuilder();%0A
-%0A
         JkRe
@@ -3622,17 +3622,16 @@
 tory();%0A
-%0A
         
@@ -3869,16 +3869,36 @@
 ()) ? %22%22
+%0A                   
  : downl
@@ -4088,24 +4088,544 @@
 ;%0A        %7D%0A
+        JkRepo publish = publishRepository();%0A        sb.append(%22%5Cn%22).append(%22Publish repository%22)%0A                .append(%22%5Cn  url : %22 + publish.getUrl());%0A        if (publish.getCredential() != null) %7B%0A            String publishPwd = JkUtilsString.isBlank(publish.getCredential().getPassword()) ? %22%22%0A                    : publishPassword.substring(0, 1) + %22*******%22;%0A            sb.append(%22%5Cn  username : %22 + publish.getCredential().getUserName())%0A                    .append(%22%5Cn  password : %22 + publishPwd);%0A        %7D
 %0A        for
@@ -4853,12 +4853,10 @@
 %0A    %7D%0A%0A
-%0A%0A
 %7D%0A

Clean repoPlugin