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

import com.google.auto.service.AutoService;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import org.apache.commons.lang3.StringUtils;
import org.apache.seatunnel.api.configuration.ReadonlyConfig;
import org.apache.seatunnel.api.configuration.util.ConfigValidator;
import org.apache.seatunnel.api.table.catalog.*;
import org.apache.seatunnel.api.table.type.*;
import org.apache.seatunnel.api.transform.SeaTunnelTransform;
import org.apache.seatunnel.shade.com.typesafe.config.Config;
import org.apache.seatunnel.transform.common.MultipleFieldOutputTransform;
import org.apache.seatunnel.transform.common.SeaTunnelRowAccessor;
import org.roaringbitmap.RoaringBitmap;

import java.lang.reflect.Array;
import java.util.*;

@AutoService(SeaTunnelTransform.class)
@NoArgsConstructor
public class DistinctTransform extends MultipleFieldOutputTransform {
    public static String PLUGIN_NAME = "Distinct";
    private DistinctTransformConfig config;
    private static RoaringBitmap rbm = new RoaringBitmap();


    public DistinctTransform(
            @NonNull DistinctTransformConfig config, @NonNull CatalogTable catalogTable) {
        super(catalogTable);
        this.config = config;
    }

    @Override
    public String getPluginName() {
        return PLUGIN_NAME;
    }

    @Override
    protected void setConfig(Config pluginConfig) {
        ConfigValidator.of(ReadonlyConfig.fromConfig(pluginConfig))
                .validate(new DistinctTransformFactory().optionRule());
        config = DistinctTransformConfig.of(ReadonlyConfig.fromConfig(pluginConfig));
    }



    @Override
    protected SeaTunnelRowType transformRowType(SeaTunnelRowType inputRowType) {
        return inputRowType;
    }


    @Override
    protected SeaTunnelRow transformRow(SeaTunnelRow inputRow) {
        List<String> fields = config.getFields();
        StringBuilder sb = new StringBuilder();
        fields.forEach(x->{
            String valueStr = String.valueOf(inputRow.getField(inputRowType.indexOf(x)));
            sb.append(valueStr).append("_");
        });
        String result=sb.deleteCharAt(sb.length() - 1).toString();
        if (!rbm.contains(result.hashCode() & Integer.MAX_VALUE)) {
            rbm.add(result.hashCode()& Integer.MAX_VALUE);
            return inputRow;
        }
        return null;

    }

    @Override
    protected void setInputRowType(SeaTunnelRowType inputRowType) {

    }

    @Override
    protected String[] getOutputFieldNames() {
        return new String[0];
    }

    @Override
    protected SeaTunnelDataType[] getOutputFieldDataTypes() {
        return new SeaTunnelDataType[0];
    }

    @Override
    protected Object[] getOutputFieldValues(SeaTunnelRowAccessor inputRow) {
        return new Object[0];
    }

    @Override
    protected TableSchema transformTableSchema() {
        return null;
    }

    @Override
    protected TableIdentifier transformTableIdentifier() {
        return inputCatalogTable.getTableId().copy();
    }

    @Override
    protected Column[] getOutputColumns() {
        return new Column[0];
    }



}
