package org.talend.dataprep.api.dataset;

import java.util.ArrayList;
import java.util.List;

import org.talend.dataprep.api.dataset.diff.Flag;

/**
 * Models metadata information for a row of a data set.
 */
public class RowMetadata {

    /** List of row metadata. */
    private List<ColumnMetadata> columnMetadatas = new ArrayList<>();

    /**
     * Default constructor.
     * 
     * @param columnMetadata the list of column metadata.
     */
    public RowMetadata(List<ColumnMetadata> columnMetadata) {
        this.columnMetadatas = columnMetadata;
    }

    /**
     * @return The metadata of this row's columns.
     */
    public List<ColumnMetadata> getColumns() {
        return columnMetadatas;
    }

    /**
     * @param columnMetadata the metadata to set.
     */
    public void setColumns(List<ColumnMetadata> columnMetadata) {
        this.columnMetadatas = columnMetadata;
    }

    /**
     * @return the row size.
     */
    public int size() {
        return columnMetadatas.size();
    }

    /**
     * @param wantedId the wanted column id.
     * @return return the wanted columnMetadata or null if not found.
     */
    protected ColumnMetadata getById(String wantedId) {
        // defensive programming
        if (wantedId == null) {
            return null;
        }
        for (ColumnMetadata column : columnMetadatas) {
            if (wantedId.equals(column.getId())) {
                return column;
            }
        }
        return null;
    }

    /**
     * Compute the diff from the given reference to this and update the diffFlag on each columnMetadatas.
     * 
     * @param reference the starting point to compute the diff.
     */
    public void diff(RowMetadata reference) {

        // process the new columns
        columnMetadatas.forEach(column -> {
            if (reference.getById(column.getId()) == null) {
                column.setDiffFlagValue(Flag.NEW.getValue());
            }
        });

        // process the updated columns
        columnMetadatas.forEach(column -> {
            ColumnMetadata referenceColumn = reference.getById(column.getId());
            if (referenceColumn != null && !column.getName().equals(referenceColumn.getName())) {
                column.setDiffFlagValue(Flag.UPDATE.getValue());
            }
        });

        // process the deleted columns (add the deleted ones)
        reference.getColumns().forEach(referenceColumn -> {
            if (getById(referenceColumn.getId()) == null) {
                referenceColumn.setDiffFlagValue(Flag.DELETE.getValue());
                columnMetadatas.add(referenceColumn);
            }
        });

        // sort the columns so that the deleted ones get placed where the were
        columnMetadatas.sort((col1, col2) -> col1.getId().compareTo(col2.getId()));
    }

    @Override
    public String toString() {
        return "RowMetadata{" + "columnMetadatas=" + columnMetadatas + '}';
    }
}

@@ -310,17 +310,16 @@
 Metadata
-s
  = new A
@@ -524,33 +524,32 @@
 s.columnMetadata
-s
  = columnMetadat
@@ -701,17 +701,16 @@
 Metadata
-s
 ;%0A    %7D%0A
@@ -869,17 +869,16 @@
 Metadata
-s
  = colum
@@ -993,17 +993,16 @@
 Metadata
-s
 .size();
@@ -1347,17 +1347,16 @@
 Metadata
-s
 ) %7B%0A    
@@ -1596,17 +1596,16 @@
 Metadata
-s
 .%0A     *
@@ -1774,33 +1774,32 @@
   columnMetadata
-s
 .forEach(column 
@@ -2014,17 +2014,16 @@
 Metadata
-s
 .forEach
@@ -2591,17 +2591,16 @@
 Metadata
-s
 .add(ref
@@ -2742,17 +2742,16 @@
 Metadata
-s
 .sort((c
@@ -2902,17 +2902,16 @@
 Metadata
-s
 =%22 + col
@@ -2921,17 +2921,16 @@
 Metadata
-s
  + '%7D';%0A

change the name back to its previous value so that previous dataset can be loaded from mongodb.