/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.apache.seatunnel.datasource.plugin.ftp;

import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.conf.Configuration;

import java.util.Map;

import static org.apache.hadoop.fs.FileSystem.FS_DEFAULT_NAME_KEY;

@Slf4j
public class HadoopFtpAConfiguration {

    /* S3 constants */
    private static final String HDFS_FTP_IMPL = "org.apache.hadoop.fs.ftp.FTPFileSystem";
    private static final String FTP_PROTOCOL = "192.168.1.174";
    private static final String DEFAULT_PROTOCOL = "ftp";
    private static final String FTP_FORMAT_KEY = "fs.%s.%s";
    private static final String HDFS_IMPL_KEY = "impl";

    public static Configuration getConfiguration(Map<String, String> FtpOption) {

        if (!FtpOption.containsKey(FtpOptionRule.HOST.key())) {
            throw new IllegalArgumentException(
                    "FtpRedshift datasource host is null, please check your config");
        }
        if (!FtpOption.containsKey(FtpOptionRule.PORT.key())) {
            throw new IllegalArgumentException(
                    "FtpRedshift datasource port is null, please check your config");
        }
        if (!FtpOption.containsKey(FtpOptionRule.USERNAME.key())) {
            throw new IllegalArgumentException(
                    "FtpRedshift datasource username is null, please check your config");
        }
        if (!FtpOption.containsKey(FtpOptionRule.PASSWORD.key())) {
            throw new IllegalArgumentException(
                    "FtpRedshift datasource password is null, please check your config");
        }
        if (!FtpOption.containsKey(FtpOptionRule.PATH.key())) {
            throw new IllegalArgumentException(
                    "FtpRedshift datasource path is null, please check your config");
        }
        if (!FtpOption.containsKey(FtpOptionRule.FILE_FORMAT_TYPE.key())) {
            throw new IllegalArgumentException(
                    "FtpRedshift datasource file_format_type is null, please check your config");
        }
        String host = FtpOption.get(FtpOptionRule.HOST.key());

        String protocol = DEFAULT_PROTOCOL;
        if (host.startsWith(FTP_PROTOCOL)) {
            protocol = FTP_PROTOCOL;
        }
        String fsImpl = protocol.equals(FTP_PROTOCOL) ? HDFS_FTP_IMPL : HDFS_FTP_IMPL;
        Configuration hadoopConf = new Configuration();
        hadoopConf.set(FS_DEFAULT_NAME_KEY, host);
        hadoopConf.set(formatKey(protocol, HDFS_IMPL_KEY), fsImpl);
        return hadoopConf;
    }

    private static String formatKey(String protocol, String key) {
        return String.format(FTP_FORMAT_KEY, protocol, key);
    }
}
