package com.quemb.qmbform.descriptor;

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

/**
 * Created by tonimoeckel on 14.07.14.
 */
public class SectionDescriptor extends FormItemDescriptor {

    private FormDescriptor mFormDescriptor;
    private ArrayList<RowDescriptor> mRows;
    private Boolean mMultivalueSection = false;

    public static SectionDescriptor newInstance(String tag) {

        return SectionDescriptor.newInstance(tag, null);

    }

    public static SectionDescriptor newInstance(String tag, String title) {

        SectionDescriptor descriptor = new SectionDescriptor();
        descriptor.mTitle = title;
        descriptor.mTag = tag;
        return descriptor;

    }

    public SectionDescriptor() {

        mRows = new ArrayList<RowDescriptor>();

    }

    public FormDescriptor getFormDescriptor() {
        return mFormDescriptor;
    }

    public void setFormDescriptor(FormDescriptor formDescriptor) {
        mFormDescriptor = formDescriptor;
    }

    public String getTitle() {
        return mTitle;
    }

    public void addRow(RowDescriptor row) {
        insertRowAtIndex(row, mRows.size());
    }

    public void addRow(RowDescriptor row, int index) {
        insertRowAtIndex(row, index);
    }

    public void removeRow(RowDescriptor row) {
        int index = mRows.indexOf(row);
        removeRowAtIndex(index);
    }

    public int getRowCount() {
        return mRows.size();
    }

    public List<RowDescriptor> getRows() {
        return mRows;
    }

    private void insertRowAtIndex(RowDescriptor row, int index) {
        if (mRows.size() >= index) {
            row.setSectionDescriptor(this);
            mRows.add(index, row);
            if (getFormDescriptor() != null) {
                getFormDescriptor().didInsertRow(row, this);
            }

        }
    }

    private void removeRowAtIndex(int index) {
        RowDescriptor rowDescriptor = mRows.get(index);
        mRows.remove(index);
        if (getFormDescriptor() != null) {
            getFormDescriptor().didRemoveRow(rowDescriptor, this);
        }

    }

    public boolean hasTitle() {
        return getTitle() != null && getTitle().length() > 0;
    }

    public RowDescriptor findRowDescriptor(String tag) {
        RowDescriptor rowDescriptor = null;

        for (RowDescriptor iRowDescriptor : getRows()) {
            if (tag.equals(iRowDescriptor.getTag())) {
                rowDescriptor = iRowDescriptor;
                break;
            }
        }

        return rowDescriptor;
    }

    public int getIndexOfRowDescriptor(RowDescriptor rowDescriptor) {
        return mRows.indexOf(rowDescriptor);
    }

    public Boolean isMultivalueSection() {
        return mMultivalueSection;
    }

    public void setMultivalueSection(Boolean multivalueSection) {
        mMultivalueSection = multivalueSection;
    }

    public List getRowValues() {

        ArrayList<Object> values = new ArrayList<>();
        for (RowDescriptor rowDescriptor : mRows) {
            if (rowDescriptor.getValue() != null && rowDescriptor.getValue().getValue() != null) {
                values.add(rowDescriptor.getValue().getValue());
            }
        }
        return values;

    }
}

@@ -60,16 +60,42 @@
 ayList;%0A
+import java.util.HashMap;%0A
 import j
@@ -1131,32 +1131,22 @@
         
-insertRowAtIndex
+addRow
 (row, mR
@@ -1212,32 +1212,32 @@
 w, int index) %7B%0A
-
         insertRo
@@ -1253,24 +1253,219 @@
 row, index);
+%0A%0A        // Propagate the CellConfig from Section to Row%0A%0A        HashMap%3CString, Object%3E cellConfig = getCellConfig();%0A        if (cellConfig != null)%0A            row.setCellConfig(cellConfig);
 %0A    %7D%0A%0A    

Update SectionDescriptor.java