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

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

@AutoService(SeaTunnelTransform.class)
@NoArgsConstructor
public class PrimaryAddTransform extends MultipleFieldOutputTransform {
    public static String PLUGIN_NAME = "PrimaryAdd";
    private PrimaryAddTransformConfig config;

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

    public PrimaryAddTransform(
            @NonNull PrimaryAddTransformConfig 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 PrimaryAddTransformFactory().optionRule());
        config = PrimaryAddTransformConfig.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<String> fieldNames = new ArrayList<>();
        List<SeaTunnelDataType> fieldsType = new ArrayList<>();
        List<String> rowFieldNames= Arrays.asList(inputRowType.getFieldNames());
        List<SeaTunnelDataType<?>> seaTunnelDataTypes = Arrays.asList(inputRowType.getFieldTypes());
        fieldNames.addAll(rowFieldNames);
        fieldsType.addAll(seaTunnelDataTypes);
        String primary = config.getPrimary();
        String genMethod = config.getGenMethod();
        fieldNames.add(primary);
        if ("UUID".equals(genMethod)){
            fieldsType.add(BasicType.STRING_TYPE);
        }else {
            fieldsType.add(BasicType.LONG_TYPE);
        }

        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) {
        String genMethod = config.getGenMethod();
        Object[] fieldValues = new Object[fieldNames.size()];
        String primary = config.getPrimary();
        for (int i = 0; i < fieldNames.size(); i++) {
            if (primary.equals(fieldNames.get(i))){
                if ("UUID".equals(genMethod)){
                    fieldValues[i] = UUID.randomUUID().toString().replaceAll("\\-", "");
                }else {
                    fieldValues[i] = SnowFlakeUtil.getSnowFlakeId();
                }
            }else {
                fieldValues[i] = inputRow.getField(i);
            }
        }
        return new SeaTunnelRow(fieldValues);
    }


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


}
