package com.nimblestorage.nimbus.client.widgets.product.manage.initiator.initiatorgroup;
/*
 * Copyright 2012, Nimble Storage, Inc.
 */

import com.google.gwt.event.shared.HandlerRegistration;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.BlurEvent;
import com.smartgwt.client.widgets.form.fields.events.BlurHandler;
import com.smartgwt.client.widgets.form.validator.RegExpValidator;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

public class TstTooltipFactory {
    /*
     * Run this method to reproduce grab focus & keep it issue on Chrome.
     * Doesn't happen on Firefox & IE.
     */
    public static void showTstErrWin() {
        TstTooltipFactory tstToolTooltipFactory = new TstTooltipFactory();
        tstToolTooltipFactory.showTstErrWindow();
    }

    public static HLayout getValidationWrapper(
            final Canvas canvas) {
        canvas.setHeight100();
        canvas.setWidth100();

        final HLayout validationWrapper = new ValidationWrapper(canvas);
        return validationWrapper;
    }

    public static HLayout getValidationWrapper(
            final FormItem formItem) {
        final DynamicForm form = new DynamicForm();
        shrinkWrap(form);
        form.setFields(formItem);
        return getValidationWrapper(form);
    }

    private static class ValidationWrapper extends HLayout {

        private static final int VALIDATION_ERROR_BORDER_WIDTH = 2;

        ValidationWrapper(final Canvas contentToWrap) {
            this.setBorder(VALIDATION_ERROR_BORDER_WIDTH + "px transparent");
            this.setMembers(contentToWrap);
        }

    }

    public interface IValidationComponent {
        HandlerRegistration addBlurHandler(BlurHandler blurHandler);
    }

    public static void shrinkWrap(final Canvas canvas) {
        canvas.setAutoHeight();
        canvas.setAutoWidth();
        canvas.setOverflow(Overflow.VISIBLE);
    }

    public class TstErrWindow extends Window {

        private final VLayout mBodyHLayout;
        protected HLayout initiatorsListAddRemovePanel = new HLayout();
        private final HLayout mButtonsHLayout = new HLayout();
        private final IButton mOkButton;
        private final TextBoxWithTooltipError initiatorIPPanel = new TextBoxWithTooltipError();
        private final TextBoxWithTooltipError initiatorIQNPanel = new TextBoxWithTooltipError();

        private final ButtonClickHandler mButtonClickHandler = new ButtonClickHandler();
        private static final String sVALID_NAME_PATTERN = "^[0-9a-zA-Z][0-9a-zA-Z\\-\\.:]{0,63}$";
        private final RegExpValidator NameValidator = new RegExpValidator(
                sVALID_NAME_PATTERN);

        protected TstErrWindow() {
            mBodyHLayout = new VLayout();
            mOkButton = new IButton("ok");
            mOkButton.addClickHandler(mButtonClickHandler);
            initiatorIPPanel.setValidator(NameValidator);
            initiatorIQNPanel.setValidator(NameValidator);
            initiatorIPPanel.setRequired(true);
            initiatorIQNPanel.setRequired(true);
            mButtonsHLayout.setWidth(410);
            setWidth(500);
            setHeight(300);
            mButtonsHLayout.addMember(mOkButton);
            initiatorsListAddRemovePanel.setMembers(initiatorIPPanel,
                    initiatorIQNPanel);
            mBodyHLayout.setMembers(initiatorsListAddRemovePanel);
            addItem(mBodyHLayout);
            addItem(mButtonsHLayout);
        }

        private final boolean validateForms() {
            Boolean isIqnValid = initiatorIQNPanel.validate();
            Boolean isIpValid = initiatorIPPanel.validate();
            return isIqnValid && isIpValid;
        }

        private final class ButtonClickHandler implements ClickHandler {
            @Override
            public void onClick(ClickEvent event) {
                validateForms();
            }
        }

        private class TextBoxWithTooltipError extends HLayout implements
                IValidationComponent {
            private final TextItem mTextItem;
            private final DynamicForm mContainerForm;
            private final HLayout mValidationFieldWrapper;

            public TextBoxWithTooltipError() {
                this(true);
            }

            public TextBoxWithTooltipError(boolean enableValidatorOnBlur) {
                super();
                setShowTitle(false);
                mTextItem = new TextItem();
                mTextItem.setRequired(false);
                mTextItem.setShowTitle(false);
                mContainerForm = new DynamicForm();
                mContainerForm.setBrowserSpellCheck(Boolean.FALSE);
                mContainerForm.setNumCols(1);
                this.setAutoHeight();
                this.setOverflow(Overflow.VISIBLE);
                mContainerForm.setFields(mTextItem);
                mValidationFieldWrapper = TstTooltipFactory
                        .getValidationWrapper(mContainerForm);
                this.setMembers(mValidationFieldWrapper);

                if (enableValidatorOnBlur) {
                    addBlurHandler(onBlurValidationHandler());
                }
            }

            protected BlurHandler onBlurValidationHandler() {
                return new BlurHandler() {
                    @Override
                    public void onBlur(BlurEvent event) {
                        validate();
                    }
                };
            }

            public void setValidator(RegExpValidator validator) {
                mTextItem.setValidators(validator);
            }

            public void setRequired(boolean required) {
                mTextItem.setRequired(required);
            }

            public boolean validate() {
                boolean valid = mContainerForm.validate();
                if (valid
                        && mTextItem.getRequired()
                        && (mTextItem.getValueAsString() == null || mTextItem
                                .getValueAsString().length() == 0)) {
                    valid = false;
                }
                return valid;
            }

            @Override
            public HandlerRegistration addBlurHandler(BlurHandler blurHandler) {
                return mTextItem.addBlurHandler(blurHandler);
            }

        }
    }

    public void showTstErrWindow() {
        new TstErrWindow().show();
    }

}
