/*
 * Copyright (C) 2010 Google Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#pragma once

#include "AutoFillButtonElement.h"
#include "DataListSuggestionPicker.h"
#include "DataListButtonElement.h"
#include "DataListSuggestionsClient.h"
#include "InputType.h"
#include "TextControlInnerElements.h"

namespace WebCore {

class FormDataList; 
class TextControlInnerTextElement;
class SpinButtonElement;

// The class represents types of which UI contain text fields.
// It supports not only the types for BaseTextInputType but also type=number.
class TextFieldInputType : public InputType, private SpinButtonElement::StepActionHandler, protected AutoFillButtonElement::AutoFillButtonOwner
#if ENABLE(DATALIST_ELEMENT)
    , private DataListSuggestionsClient, protected DataListButtonElement::DataListButtonOwner
#endif
{
protected:
    explicit TextFieldInputType(Type, HTMLInputElement*);
    virtual ~TextFieldInputType();
    virtual bool canSetSuggestedValue() OVERRIDE;
    virtual ShouldCallBaseEventHandler handleKeydownEvent(KeyboardEvent*) OVERRIDE;
    void handleKeydownEventForSpinButton(KeyboardEvent*);
#if ENABLE(DATALIST_ELEMENT)
    virtual void handleClickEvent(MouseEvent*) FINAL;
#endif

    virtual HTMLElement* containerElement() const FINAL;
    virtual HTMLElement* innerBlockElement() const FINAL;
    virtual RefPtr<TextControlInnerTextElement> innerTextElement() const FINAL;
    virtual HTMLElement* innerSpinButtonElement() const FINAL;
    virtual HTMLElement* autoFillButtonElement() const final;
#if ENABLE(INPUT_SPEECH)
    virtual HTMLElement* speechButtonElement() const FINAL;
#endif
#if ENABLE(DATALIST_ELEMENT)
    HTMLElement* dataListButtonElement() const FINAL;
#endif

    virtual bool needsContainer() const;
    virtual bool shouldHaveSpinButton() const;
    virtual void createShadowSubtreeAndUpdateInnerTextElementEditability(ContainerNode::ChildChangeSource, bool) override;
    virtual void destroyShadowSubtree() OVERRIDE;
    virtual void attributeChanged(const QualifiedName&) override;
    virtual void disabledStateChanged() final;
    virtual void readOnlyStateChanged() final;
    virtual bool supportsReadOnly() const override;
    virtual void handleBlurEvent() override;
    virtual void setValue(const String&, bool valueChanged, TextFieldEventBehavior) override;
    virtual void updateInnerTextValue() final;
    virtual bool valueMissing(const String&) const override;

    virtual String convertFromVisibleValue(const String&) const;
    virtual void didSetValueByUserEdit();

private:
    virtual bool isKeyboardFocusable(KeyboardEvent*) const final;
    virtual bool isMouseFocusable() const final;
    virtual void handleBeforeTextInsertedEvent(BeforeTextInsertedEvent*) final;
    virtual void forwardEvent(Event*) final;
    virtual bool shouldSubmitImplicitly(Event*) final;
    virtual RenderPtr<RenderElement> createInputRenderer(PassOwnPtr<RenderStyle>) const override;
    virtual bool shouldUseInputMethod() const override;
    virtual String sanitizeValue(const String&) const override;
    virtual bool shouldRespectListAttribute() override;
    virtual HTMLElement* placeholderElement() const final;
    virtual void updatePlaceholderText() final;
    virtual bool appendFormData(FormDataList&, bool multipart) const final;
    virtual void subtreeHasChanged() final;
    virtual void updateAutoFillButton() final;
    virtual void elementDidBlur() final;

    // SpinButtonElement::StepActionHandler functions.
    virtual void focusAndSelectSpinButtonOwner() final;
    virtual bool shouldSpinButtonRespondToMouseEvents() final;
    virtual bool shouldSpinButtonRespondToWheelEvents() final;
    virtual void spinButtonStepDown() final;
    virtual void spinButtonStepUp() final;

    // AutoFillButtonElement::AutoFillButtonOwner
    virtual void autoFillButtonElementWasClicked() final;

    bool shouldDrawAutoFillButton() const;

    void createContainer();
    void createAutoFillButton(AutoFillButtonType);

#if ENABLE(DATALIST_ELEMENT)
    virtual bool isPresentingAttachedView() const FINAL;
    virtual void dataListMayHaveChanged() FINAL;
    void displaySuggestions(DataListSuggestionActivationType);
    void closeSuggestions();

    // DataListSuggestionsClient
    virtual IntRect elementRectInRootViewCoordinates() const FINAL;
    virtual Vector<String> suggestions() FINAL;
    virtual void didSelectDataListOption(const String&) FINAL;
    virtual void didCloseSuggestions() FINAL;

    virtual void dataListButtonElementWasClicked() FINAL;
    RefPtr<DataListButtonElement> m_dataListDropdownIndicator;

    std::pair<String, Vector<String>> m_cachedSuggestions;
    OwnPtr<DataListSuggestionPicker> m_suggestionPicker;
#endif

    RefPtr<HTMLElement> m_container;
    RefPtr<HTMLElement> m_innerBlock;
    RefPtr<TextControlInnerTextElement> m_innerText;
    RefPtr<HTMLElement> m_placeholder;
    RefPtr<SpinButtonElement> m_innerSpinButton;
    RefPtr<HTMLElement> m_autoFillButton;
#if ENABLE(INPUT_SPEECH)
    RefPtr<HTMLElement> m_speechButton;
#endif
};

} // namespace WebCore
