/*
 * 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.s3;

import io.minio.MinioClient;
import org.apache.commons.lang3.StringUtils;
import org.apache.seatunnel.api.configuration.util.OptionRule;
import org.apache.seatunnel.common.utils.SeaTunnelException;
import org.apache.seatunnel.datasource.plugin.api.DataSourceChannel;
import org.apache.seatunnel.datasource.plugin.api.DataSourcePluginException;
import org.apache.seatunnel.datasource.plugin.api.model.TableField;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import lombok.NonNull;

import java.io.IOException;
import java.util.List;
import java.util.Map;

public class S3DatasourceChannel implements DataSourceChannel {
    public static class Holder {
        private static final S3DatasourceChannel INSTANCE = new S3DatasourceChannel();
    }

    public static S3DatasourceChannel getInstance() {
        return Holder.INSTANCE;
    }

    @Override
    public OptionRule getDataSourceOptions(@NonNull String pluginName) {
        return S3OptionRule.optionRule();
    }

    @Override
    public OptionRule getDatasourceMetadataFieldsByDataSourceName(@NonNull String pluginName) {
        return S3OptionRule.metadataRule();
    }

    @Override
    public List<String> getTables(
            @NonNull String pluginName,
            Map<String, String> requestParams,
            String database,
            Map<String, String> options) {
        throw new UnsupportedOperationException("getTables is not supported for S3 datasource");
    }

    @Override
    public List<String> getDatabases(
            @NonNull String pluginName, @NonNull Map<String, String> requestParams) {
        throw new UnsupportedOperationException("getDatabases is not supported for S3 datasource");
    }

    @Override
    public boolean checkDataSourceConnectivity(
            @NonNull String pluginName, @NonNull Map<String, String> requestParams) {
        if (requestParams.isEmpty()) {
            throw new SeaTunnelException("requestParmas 为空！");
        }
        try {
            S3ClientService s3Client = createS3Client(requestParams);
            return true;
        } catch (Exception ex) {
            throw new DataSourcePluginException(
                    "check s3 connectivity failed, " + ex.getMessage(), ex);
        }
//        try (FileSystem fs = FileSystem.get(conf)) {
////            fs.listStatus(new Path("/"));
//
//            return true;
//        } catch (IOException e) {
//            throw new DataSourcePluginException(
//                    String.format("check s3 connectivity failed, config is: %s", requestParams), e);
//        }
    }

    @Override
    public List<TableField> getTableFields(
            @NonNull String pluginName,
            @NonNull Map<String, String> requestParams,
            @NonNull String database,
            @NonNull String table) {
        throw new UnsupportedOperationException(
                "getTableFields is not supported for S3 datasource");
    }

    @Override
    public Map<String, List<TableField>> getTableFields(
            @NonNull String pluginName,
            @NonNull Map<String, String> requestParams,
            @NonNull String database,
            @NonNull List<String> tables) {
        throw new UnsupportedOperationException(
                "getTableFields is not supported for S3 datasource");
    }

    private S3ClientService createS3Client(Map<String, String> requestParams) {
        String endpoint = requestParams.get("fs.s3a.endpoint").split(":")[0] + "";
        Integer port = Integer.valueOf(requestParams.get("fs.s3a.endpoint").split(":")[1]);
        String provider = requestParams.get("fs.s3a.aws.credentials.provider") + "";
        String username = requestParams.get("access_key") + "";
        String password = requestParams.get("secret_key") + "";
        String bucket = requestParams.get("bucket") + "";
        try {
            S3ClientService s3ClientService = new S3ClientService(endpoint, provider, username, password, bucket, port);

            return s3ClientService;
        } catch (Exception e) {
            throw new SeaTunnelException("创建Mqtt客户端错误！");
        }
    }
}
