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.BraintreeGeneralConfigurationModel;
import de.hybris.platform.core.model.BraintreeHostedFieldsConfigurationModel;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.ext.Disable;

public class BrainTreeHostedFieldsConfigurationSavedAccountLimitEditor 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 BraintreeHostedFieldsConfigurationModel hostedFieldsConfigModel =
                (BraintreeHostedFieldsConfigurationModel) context.getParameter(PARENT_OBJECT);
        final BraintreeGeneralConfigurationModel generalConfigModel = hostedFieldsConfigModel.getPluginConfiguration()
                .getBraintreeGeneralConfiguration();
        if (!hostedFieldsConfigModel.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); }
}
