package cn.axzo.workflow.form.engine.model;

import org.flowable.form.model.FormContainer;
import org.flowable.form.model.FormField;
import org.flowable.form.model.SimpleFormModel;

import java.util.List;

import static cn.axzo.workflow.common.constant.FormConstants.FORM_FIELD_TYPE_CUSTOM_COMPONENT;

/**
 * @author wangli
 * @since 2025-01-24 15:49
 */
public class CustomSimpleFormModel extends SimpleFormModel {
    public CustomSimpleFormModel(SimpleFormModel simpleFormModel) {
        this.setName(simpleFormModel.getName());
        this.setKey(simpleFormModel.getKey());
        this.setVersion(simpleFormModel.getVersion());
        this.setDescription(simpleFormModel.getDescription());
        this.setFields(simpleFormModel.getFields());
        this.setOutcomes(simpleFormModel.getOutcomes());
        this.setOutcomeVariableName(simpleFormModel.getOutcomeVariableName());
    }

    private static final long serialVersionUID = 1L;

    @Override
    protected void collectSubFields(List<FormField> fields, List<FormField> listOfAllFields) {
        if (fields != null && fields.size() > 0) {
            for (FormField field : fields) {
                listOfAllFields.add(field);
                if (field instanceof FormContainer && !FORM_FIELD_TYPE_CUSTOM_COMPONENT.equals(field.getType())) {
                    FormContainer container = (FormContainer) field;
                    List<List<FormField>> subFields = container.getFields();
                    if (subFields != null) {
                        for (List<FormField> subFieldDefinitions : subFields) {
                            if (subFieldDefinitions != null) {
                                collectSubFields(subFieldDefinitions, listOfAllFields);
                            }
                        }
                    }
                }
            }
        }
    }
}
