package FTP;

import lombok.NonNull;
import org.apache.commons.net.ftp.FTPClient;
import org.tinylog.Logger;

@SuppressWarnings("unused")
public class FTPCheck {
    private final String host;
    private final int port;
    private final String user;
    private final String pass;
    private final String path;
    private final boolean passive;

    /**
     * Initialize FTP Check
     * @param host FTP Host
     * @param port FTP Port
     * @param user FTP User
     * @param pass FTP Password
     * @param path FTP Path
     * @param passive Use Passive Mode
     */
    public FTPCheck(String host, int port, String user, String pass, String path, boolean passive){
        this.host = host;
        this.port = port;
        this.user = user;
        this.pass = pass;
        this.path = path;
        this.passive = passive;
    }

    /**
     * Check if FTP is Correct and connectable
     * @return true if FTP is correct, false otherwise
     */
    public boolean IsCorrect(){
        FTPClient client = new FTPClient();
        try {
            boolean alliswell = true;
            client.connect(host, port);

            if (!client.login(user, pass)) {
                Logger.info("FTPCheck failed, Error: Login failed");
                alliswell = false;
            }
            if (passive) client.enterLocalPassiveMode();
            if (!client.changeWorkingDirectory(path)) {
                Logger.info("FTPCheck failed, Error: Change Directory failed");
                alliswell = false;
            }
            if (!client.logout()) {
                Logger.info("FTPCheck failed, Error: Logout failed");
                alliswell = false;
            }
            client.disconnect();
            Logger.info("IsCorrect: "+alliswell);
            return alliswell;
        } catch (Exception e) {
            Logger.error("FTPCheck failed, Error: " + e.getMessage());
            return false;
        }
    }

    /**
     * List Files in FTP
     * @return filenames found in FTP path, or empty array if failed
     */
    public @NonNull String[] ListFiles() {
        FTPClient client = new FTPClient();
        try {
            client.connect(host, port);
            if (!client.login(user, pass)) {
                Logger.info("FTPCheck failed, Error: Login failed");
                return new String[0];
            }
            if (passive) client.enterLocalPassiveMode();
            if (!client.changeWorkingDirectory(path)) {
                Logger.info("FTPCheck failed, Error: Change Directory failed");
                return new String[0];
            }
            String[] files = client.listNames();
            if (!client.logout()) {
                Logger.info("FTPCheck failed, Error: Logout failed");
                return new String[0];
            }
            client.disconnect();
            Logger.info("ListFiles: "+files.length);
            return files;
        } catch (Exception e) {
            Logger.error("FTPCheck failed, Error: " + e.getMessage());
            return new String[0];
        }
    }

}
