package com.braintree.customersupportbackoffice.editor;

import com.hybris.cockpitng.editor.defaultenum.DefaultEnumEditor;
import com.hybris.cockpitng.editors.EditorContext;
import com.hybris.cockpitng.editors.EditorListener;
import de.hybris.platform.core.model.BraintreeACHConfigurationModel;
import de.hybris.platform.core.model.BraintreeGeneralConfigurationModel;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.ext.Disable;

public class BrainTreeACHConfigurationSavedAccountLimitEditor extends DefaultEnumEditor {

    private static final String PARENT_OBJECT = "parentObject";

    @Override
    public void render(Component parent, EditorContext<Object> context, EditorListener<Object> listener) {
        callParentRender(parent, context, listener);
        disableConfig(parent, context);
    }

    protected void disableConfig(Component parent, EditorContext<Object> context) {
        final BraintreeACHConfigurationModel achConfigModel =
                (BraintreeACHConfigurationModel) context.getParameter(PARENT_OBJECT);
        final BraintreeGeneralConfigurationModel generalConfigModel = achConfigModel.getPluginConfiguration()
                .getBraintreeGeneralConfiguration();
        if (!achConfigModel.isEnabled() || !generalConfigModel.isStoreInVault() || generalConfigModel.isDropIn()) {
            Disable disable = (Disable) parent.getFirstChild();
            disable.setDisabled(true);
        }
    }

    protected void callParentRender(Component parent, EditorContext<Object> context, EditorListener<Object> listener) { super.render(parent, context, listener); }
}
