package com.braintree.customersupportbackoffice.editor;

import com.hybris.cockpitng.editor.bool.DefaultBooleanEditor;
import com.hybris.cockpitng.editors.EditorContext;
import com.hybris.cockpitng.editors.EditorListener;
import com.hybris.cockpitng.util.UITools;
import de.hybris.platform.core.model.BraintreeGooglePayConfigurationModel;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.HtmlBasedComponent;
import org.zkoss.zul.Div;
import org.zkoss.zul.Radiogroup;

public class BrainTreeGooglePayConfigurationBooleanEditor extends DefaultBooleanEditor {

    private static final String DISABLED_DIV = "disabled-div";

    private static final String PARENT_OBJECT = "parentObject";

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

    protected void disableConfig(Component parent, EditorContext<Boolean> editorContext) {
        BraintreeGooglePayConfigurationModel braintreeGooglePayConfigurationModel =
                (BraintreeGooglePayConfigurationModel) editorContext.getParameter(PARENT_OBJECT);
        if  (!braintreeGooglePayConfigurationModel.isEnabled()){
            Radiogroup radiogroup = (Radiogroup) parent.getFirstChild();
            setDisabled((Div) radiogroup.getChildren().get(0));
        }
    }

    protected void setDisabled(Div div) {
        UITools.modifySClass((HtmlBasedComponent) div, DISABLED_DIV, true);
    }

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