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

import com.google.auto.service.AutoService;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import org.apache.seatunnel.api.configuration.ReadonlyConfig;
import org.apache.seatunnel.api.configuration.util.ConfigValidator;
import org.apache.seatunnel.api.table.catalog.CatalogTable;
import org.apache.seatunnel.api.table.catalog.Column;
import org.apache.seatunnel.api.table.catalog.PhysicalColumn;
import org.apache.seatunnel.api.table.type.BasicType;
import org.apache.seatunnel.api.table.type.SeaTunnelDataType;
import org.apache.seatunnel.api.table.type.SeaTunnelRow;
import org.apache.seatunnel.api.table.type.SeaTunnelRowType;
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.apache.seatunnel.transform.utils.SnowFlakeUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

@AutoService(SeaTunnelTransform.class)
@NoArgsConstructor
public class FieldSelectTransform extends MultipleFieldOutputTransform {
    public static String PLUGIN_NAME = "FieldSelect";
    private FieldSelectTransformConfig config;
    private List<Integer> needReaderColIndex;

    private List<String> fieldNames;
    private List<SeaTunnelDataType> fieldTypes;

    public FieldSelectTransform(
            @NonNull FieldSelectTransformConfig 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 FieldSelectTransformFactory().optionRule());
        config = FieldSelectTransformConfig.of(ReadonlyConfig.fromConfig(pluginConfig));
    }

    @Override
    protected void setInputRowType(SeaTunnelRowType inputRowType) {
    }



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

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

    @Override
    protected Column[] getOutputColumns() {
        Column[] columns = new Column[fieldNames.size()];
        for (int i = 0; i < fieldNames.size(); i++) {
            columns[i] =
                    PhysicalColumn.of(fieldNames.get(i), fieldTypes.get(i), 200, true, "", "");
        }
        return columns;
    }


    @Override
    protected SeaTunnelRowType transformRowType(SeaTunnelRowType inputRowType) {
        List<FieldSelectEntity> fields = config.getFields();
        needReaderColIndex = new ArrayList<>(fields.size());
        List<String> fieldNames = new ArrayList<>();
        List<SeaTunnelDataType> fieldsType = new ArrayList<>();
        List<String> rowFieldNames= Arrays.asList(inputRowType.getFieldNames());
        List<SeaTunnelDataType<?>> seaTunnelDataTypes = Arrays.asList(inputRowType.getFieldTypes());
        fields.forEach(f->{
            if (rowFieldNames.contains(f.getField())){
                int index = rowFieldNames.indexOf(f.getField());
                needReaderColIndex.add(index);
                fieldsType.add(seaTunnelDataTypes.get(index));
                fieldNames.add(f.getNewFiled());
            }
        });
        SeaTunnelRowType outputRowType = new SeaTunnelRowType(fieldNames.toArray(new String[0]), fieldsType.toArray(new SeaTunnelDataType[0]));
        this.fieldNames = fieldNames;
        this.fieldTypes = fieldsType;
        return outputRowType;
    }


    @Override
    protected SeaTunnelRow transformRow(SeaTunnelRow inputRow) {
        Object[] fieldValues = new Object[fieldNames.size()];
        for (int i = 0; i < fieldNames.size(); i++) {
            fieldValues[i] = inputRow.getField(needReaderColIndex.get(i));
        }
        return new SeaTunnelRow(fieldValues);
    }


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


}
