package com.bytesw.bytebot.routes;

import com.bytesw.xdf.multitenant.core.ThreadLocalStorage;
import org.apache.camel.builder.RouteBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class SFTPRoute extends RouteBuilder {

    @Value("${application.AVB-integration.ftp.username}")
    private String username;

    @Value("${application.AVB-integration.ftp.password}")
    private String password;

    @Value("${application.AVB-integration.ftp.directory:}")
    private String directory;

    @Value("${application.AVB-integration.ftp.host}")
    private String host;

    @Value("${application.AVB-integration.ftp.passiveMode:true}")
    private boolean passiveMode;

    @Value("${application.services.multi-tenant:false}")
    private boolean multitenant;

    @Override
    public void configure() throws Exception {
        from("direct:transferFile")
                .toF("ftp://%s@%s/%s?password=%s&binary=true&passiveMode=%s", username, host, getDirectory(), password, passiveMode)
                .log("Transfered file .${file:name} ");
    }

    private String getDirectory() {
        if (!multitenant || ThreadLocalStorage.getTenantName() == null) {
            return directory;
        }
        return ThreadLocalStorage.getTenantName() + "/" + directory;
    }
}
