package org.apache.seatunnel.datasource.plugin.csv;

import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.conf.Configuration;
import org.apache.seatunnel.shade.com.typesafe.config.Config;
import org.apache.seatunnel.shade.com.typesafe.config.ConfigFactory;

import java.util.Map;

@Slf4j
public class CSVAConfiguration {

    /* S3 constants */
    private static final String S3A_SCHEMA = "s3a";
    private static final String HDFS_S3N_IMPL = "org.apache.hadoop.fs.s3native.NativeS3FileSystem";
    private static final String HDFS_S3A_IMPL = "org.apache.hadoop.fs.s3a.S3AFileSystem";
    private static final String S3A_PROTOCOL = "s3a";
    private static final String DEFAULT_PROTOCOL = "s3n";
    private static final String S3_FORMAT_KEY = "fs.%s.%s";
    private static final String HDFS_IMPL_KEY = "impl";

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

        if (!s3Options.containsKey(CSVOptionRule.BUCKET.key())) {
            throw new IllegalArgumentException(
                    "S3 datasource bucket is null, please check your config");
        }
        if (!s3Options.containsKey(CSVOptionRule.FS_S3A_ENDPOINT.key())) {
            throw new IllegalArgumentException(
                    "S3 datasource endpoint is null, please check your config");
        }
        String bucket = s3Options.get( CSVOptionRule.BUCKET.key());

        String protocol = DEFAULT_PROTOCOL;
        if (bucket.startsWith(S3A_PROTOCOL)) {
            protocol = S3A_PROTOCOL;
        }
        String fsImpl = protocol.equals(S3A_PROTOCOL) ? HDFS_S3A_IMPL : HDFS_S3N_IMPL;
        Configuration hadoopConf = new Configuration();
        hadoopConf.set("fs.defaut.name", bucket);
        hadoopConf.set(
                CSVOptionRule.FS_S3A_ENDPOINT.key(),
                s3Options.get(CSVOptionRule.FS_S3A_ENDPOINT.key()));
        hadoopConf.set(formatKey(protocol, HDFS_IMPL_KEY), fsImpl);
        if (s3Options.containsKey(CSVOptionRule.HADOOP_S3_PROPERTIES.key())) {
            Config configObject =
                    ConfigFactory.parseString(
                            s3Options.get(CSVOptionRule.HADOOP_S3_PROPERTIES.key()));
            configObject
                    .entrySet()
                    .forEach(
                            entry -> {
                                hadoopConf.set(
                                        entry.getKey(), entry.getValue().unwrapped().toString());
                            });
        }
        if (CSVOptionRule.S3aAwsCredentialsProvider.SimpleAWSCredentialsProvider.getProvider()
                .equals(s3Options.get(CSVOptionRule.S3A_AWS_CREDENTIALS_PROVIDER.key()))) {
            hadoopConf.set(
                    CSVOptionRule.S3A_AWS_CREDENTIALS_PROVIDER.key(),
                    s3Options.get(CSVOptionRule.S3A_AWS_CREDENTIALS_PROVIDER.key()));
            hadoopConf.set("fs.s3a.access.key", s3Options.get(CSVOptionRule.ACCESS_KEY.key()));
            hadoopConf.set("fs.s3a.secret.key", s3Options.get(CSVOptionRule.SECRET_KEY.key()));
        } else {
            hadoopConf.set(
                    CSVOptionRule.S3A_AWS_CREDENTIALS_PROVIDER.key(),
                    s3Options.get(CSVOptionRule.S3A_AWS_CREDENTIALS_PROVIDER.key()));
        }
        return hadoopConf;
    }

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