package kz.arta.synergy.workflow.mobile.android.forms.mobile;

import android.widget.LinearLayout;

import kz.arta.synergy.workflow.mobile.android.forms.mobile.components.FormListBox;
import kz.arta.synergy.workflow.mobile.android.forms.mobile.components.FormTextArea;
import kz.arta.synergy.workflow.mobile.android.forms.mobile.components.FormTextBox;
import kz.arta.synergy.workflow.mobile.android.forms.mobile.components.IMobileComponents;

/**
 * Created by neox on 16.07.15.
 * Утилиты для работы с формой
 */
public class FormUtils {

    /**
     * Метод возвращает данные по форме
     *
     * @return json
     */
    public static String getFormData(LinearLayout formLayout) {
        if (formLayout == null) {
            return "";
        }
        StringBuilder result = new StringBuilder("\"data\":[");
        for (int i = 0; i < formLayout.getChildCount(); i++) {
            if (formLayout.getChildAt(i) instanceof FormTextBox ||
                    formLayout.getChildAt(i) instanceof FormTextArea ||
                    formLayout.getChildAt(i) instanceof FormListBox) {
                result.append((((IMobileComponents) formLayout.getChildAt(i)).getJson())).append(",");
            }
        }
        if (result.length() > 1) {
            result.delete(result.length() - 1, result.length());
        }
        result.append("]");
        return result.toString();
    }
}
