/*-
 * ~~~~~~licensing~~~~~~
 * file-adapter
 * ==========
 * Copyright (C) 2020 - 2025 EmDev LLC
 * ==========
 * You may not use this file except in accordance with the License Terms of the Copyright
 * Holder located at: https://entaxy.ru/eula . All copyrights, all intellectual property
 * rights to the Software and any copies are the property of the Copyright Holder. Unless
 * it is explicitly allowed the Copyright Holder, the User is prohibited from using the
 * Software for commercial purposes to provide services to third parties.
 * 
 * The Copyright Holder hereby declares that the Software is provided on an "AS IS".
 * Under no circumstances does the Copyright Holder guarantee or promise that the
 * Software provided by him will be suitable or not suitable for the specific purposes
 * of the User, that the Software will meet all commercial and personal subjective
 * expectations of the User, that the Software will work properly, without technical
 * errors, quickly and uninterruptedly.
 * 
 * Under no circumstances shall the Copyright Holder or its Affiliates is not liable
 * to the User for any direct or indirect losses of the User, his expenses or actual
 * damage, including, downtime; loss of bussines; lost profit; lost earnings; loss
 * or damage to data, property, etc.
 * ~~~~~~/licensing~~~~~~
 */
package ru.entaxy.platform.runtime.connections.ftp;

import org.apache.camel.ExtendedCamelContext;
import org.apache.camel.component.file.GenericFileEndpoint;
import org.apache.camel.component.file.remote.FtpComponent;
import org.apache.camel.spi.PropertyConfigurer;
import org.apache.commons.net.ftp.FTPFile;
import ru.entaxy.platform.base.support.CommonUtils;

import java.util.Map;

public class FtpConnectionComponent extends FtpComponent {

    private static final String COMPONENT_CONFIGURER = "ftp-component-configurer";
    private static final String ENDPOINT_CONFIGURER = "ftp-endpoint-configurer";
    private static final String PATH_PLACEHOLDER = "PATH_PLACEHOLDER";

    protected String username = "";
    protected String password = "";
    protected String host = "";
    protected String port = "";
    protected String directoryName = "";

    private volatile PropertyConfigurer componentPropertyConfigurerLocal;
    private volatile PropertyConfigurer endpointPropertyConfigurerLocal;

    @Override
    protected void doBuild() {

        componentPropertyConfigurerLocal = getCamelContext().adapt(ExtendedCamelContext.class).getConfigurerResolver()
                .resolvePropertyConfigurer(COMPONENT_CONFIGURER, getCamelContext());
        log.debug("componentPropertyConfigurerLocal: {}", componentPropertyConfigurerLocal);

        endpointPropertyConfigurerLocal = getCamelContext().adapt(ExtendedCamelContext.class).getConfigurerResolver()
                .resolvePropertyConfigurer(ENDPOINT_CONFIGURER, getCamelContext());
        log.debug("endpointPropertyConfigurerLocal: {}", endpointPropertyConfigurerLocal);
    }

    protected GenericFileEndpoint<FTPFile> createEndpoint(String uri, String remaining, Map<String, Object> parameters)
            throws Exception {

        // doBuild method called to force configurer initialization (doBuild not invoked in some scenario)
        doBuild();
        uri = uri.replace(PATH_PLACEHOLDER, buildPath());
        log.debug("CREATING ENDPOINT FOR [{}]", uri);
        if (CommonUtils.isValid(username)) {
            parameters.put("username", username);
        }
        if (CommonUtils.isValid(password)) {
            parameters.put("password", password);
        }

        return super.createEndpoint(uri, remaining, parameters);
    }

    private String buildPath() {

        StringBuilder path = new StringBuilder();

        if (CommonUtils.isValid(host)) {
            path.append(host.trim());
        }
        if (CommonUtils.isValid(port)) {
            path.append(":");
            path.append(port.trim());
        }
        if (CommonUtils.isValid(directoryName)) {
            if (!directoryName.startsWith("/")) {
                path.append("/");
            }
            path.append(directoryName.trim());
        }
        return path.toString();
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public void setDirectoryName(String directoryName) {
        this.directoryName = directoryName;
    }

    @Override
    public PropertyConfigurer getComponentPropertyConfigurer() {
        return componentPropertyConfigurerLocal;
    }

    @Override
    public PropertyConfigurer getEndpointPropertyConfigurer() {
        return endpointPropertyConfigurerLocal;
    }
}
