/*
 * 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.paimon.table.format;

import org.apache.paimon.CoreOptions;
import org.apache.paimon.data.BinaryRow;
import org.apache.paimon.data.InternalRow;
import org.apache.paimon.format.FileFormat;
import org.apache.paimon.fs.FileIO;
import org.apache.paimon.fs.Path;
import org.apache.paimon.fs.TwoPhaseOutputStream;
import org.apache.paimon.table.sink.CommitMessage;
import org.apache.paimon.types.RowType;
import org.apache.paimon.utils.FileStorePathFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import static org.apache.paimon.format.FileFormat.fileFormat;
import static org.apache.paimon.utils.PartitionPathUtils.generatePartitionPathUtil;

/** File writer for format table. */
public class FormatTableFileWriter {

    private final FileIO fileIO;
    private RowType writeRowType;
    private final FileFormat fileFormat;
    private final FileStorePathFactory pathFactory;
    protected final Map<BinaryRow, FormatTableRecordWriter> writers;
    protected final CoreOptions options;

    public FormatTableFileWriter(
            FileIO fileIO, RowType writeRowType, CoreOptions options, RowType partitionType) {
        this.fileIO = fileIO;
        this.writeRowType = writeRowType;
        this.fileFormat = fileFormat(options);
        this.writers = new HashMap<>();
        this.options = options;
        this.pathFactory =
                new FileStorePathFactory(
                        options.path(),
                        partitionType,
                        options.partitionDefaultName(),
                        options.fileFormatString(),
                        options.dataFilePrefix(),
                        options.changelogFilePrefix(),
                        options.legacyPartitionName(),
                        options.fileSuffixIncludeCompression(),
                        options.formatTableFileCompression(),
                        options.dataFilePathDirectory(),
                        null,
                        CoreOptions.ExternalPathStrategy.NONE,
                        null,
                        options.indexFileInDataFileDir(),
                        null);
    }

    public void withWriteType(RowType writeType) {
        this.writeRowType = writeType;
    }

    public void write(BinaryRow partition, InternalRow data) throws Exception {
        FormatTableRecordWriter writer = writers.get(partition);
        if (writer == null) {
            writer = createWriter(partition.copy());
            writers.put(partition.copy(), writer);
        }
        writer.write(data);
    }

    public void close() throws Exception {
        writers.clear();
    }

    public List<CommitMessage> prepareCommit() throws Exception {
        List<CommitMessage> commitMessages = new ArrayList<>();
        for (FormatTableRecordWriter writer : writers.values()) {
            List<TwoPhaseOutputStream.Committer> commiters = writer.closeAndGetCommitters();
            for (TwoPhaseOutputStream.Committer committer : commiters) {
                TwoPhaseCommitMessage twoPhaseCommitMessage = new TwoPhaseCommitMessage(committer);
                commitMessages.add(twoPhaseCommitMessage);
            }
        }
        return commitMessages;
    }

    private FormatTableRecordWriter createWriter(BinaryRow partition) {
        Path parent = pathFactory.root();
        if (partition.getFieldCount() > 0) {
            LinkedHashMap<String, String> partValues =
                    pathFactory.partitionComputer().generatePartValues(partition);
            parent =
                    new Path(
                            parent,
                            generatePartitionPathUtil(
                                    partValues, options.formatTablePartitionOnlyValueInPath()));
        }
        return new FormatTableRecordWriter(
                fileIO,
                fileFormat,
                options.targetFileSize(false),
                pathFactory.createDataFilePathFactory(parent, null),
                writeRowType,
                options.formatTableFileCompression());
    }
}
