package com.braintree.customersupportbackoffice.editor;

import com.braintree.enums.BraintreeThreeDSecureFlow;
import com.hybris.cockpitng.components.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 org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.ext.Disable;

public class BrainTreeGeneralConfigurationEnumEditor extends DefaultEnumEditor {

    private static final String PARENT_OBJECT = "parentObject";
    private static final String BRAINTREE_THREEDSECURE_SCA_EXEMPTION = "braintreeThreeDSecureSCAExemption";

    @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) {
        BraintreeGeneralConfigurationModel generalConfigurationModel =
                (BraintreeGeneralConfigurationModel) context.getParameter(PARENT_OBJECT);
        if (((Editor) parent).getProperty().contains(BRAINTREE_THREEDSECURE_SCA_EXEMPTION)
                && (generalConfigurationModel.getBraintreeThreeDSecureFlow() == BraintreeThreeDSecureFlow.DISABLED
                || generalConfigurationModel.isBraintreeThreeDSecureChallengeRequested())) {
            Disable disable = (Disable) parent.getFirstChild();
            disable.setDisabled(true);
        }
    }

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