/*
 * Copyright (C) 2004 Zack Rusin <zack@kde.org>
 * Copyright (C) 2004, 2005, 2006, 2008, 2012 Apple Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */

#pragma once

#include "Animation.h"
#include "CSSStyleDeclaration.h"
#include "RenderStyleConstants.h"
#include "SVGRenderStyleDefs.h"
#include <wtf/Ref.h>
#include <wtf/RefPtr.h>
#include <wtf/text/WTFString.h>

namespace WebCore {

class CSSFontStyleValue;
class CSSPrimitiveValue;
class CSSValueList;
class Color;
class FilterOperations;
class FontSelectionValue;
class MutableStylePropertySet;
class Element;
class Node;
class RenderElement;
class RenderStyle;
class ShadowData;
class StylePropertySet;
class StylePropertyShorthand;

enum EUpdateLayout { DoNotUpdateLayout = false, UpdateLayout = true };

enum AdjustPixelValuesForComputedStyle { AdjustPixelValues, DoNotAdjustPixelValues };

class ComputedStyleExtractor {
public:
    ComputedStyleExtractor(Node*, bool allowVisitedStyle = false, PseudoId = PseudoId::None);
    ComputedStyleExtractor(Element*, bool allowVisitedStyle = false, PseudoId = PseudoId::None);

    enum class PropertyValueType : bool { Resolved, Computed };
    PassRefPtr<CSSValue> propertyValue(CSSPropertyID, EUpdateLayout = UpdateLayout, PropertyValueType = PropertyValueType::Resolved);
    PassRefPtr<CSSValue> valueForPropertyInStyle(const RenderStyle*, CSSPropertyID, RenderElement* = nullptr, PropertyValueType = PropertyValueType::Resolved);
    String customPropertyText(const String& propertyName);
    PassRefPtr<CSSValue> customPropertyValue(const String& propertyName);

    // Helper methods for HTML editing.
    Ref<MutableStylePropertySet> copyPropertiesInSet(const CSSPropertyID* set, unsigned length);
    Ref<MutableStylePropertySet> copyProperties();
    PassRefPtr<CSSPrimitiveValue> getFontSizeCSSValuePreferringKeyword();
    bool useFixedFontDefaultSize();
    bool propertyMatches(CSSPropertyID, const CSSValue*);

#if ENABLE(CSS_FILTERS)
    static Ref<CSSValue> valueForFilter(const RenderStyle* style, const FilterOperations& filterOperations, AdjustPixelValuesForComputedStyle = AdjustPixelValues);
#endif

	static void addValueForAnimationPropertyToList(CSSValueList&, CSSPropertyID, const Animation*);

private:
    // The renderer we should use for resolving layout-dependent properties.
    RenderElement* styledRenderer() const;

    PassRefPtr<CSSValue> svgPropertyValue(CSSPropertyID, EUpdateLayout);
    Ref<CSSValue> adjustSVGPaintForCurrentColor(SVGPaintType, const String& url, const Color&, const Color& currentColor) const;
    static Ref<CSSValue> valueForShadow(const ShadowData*, CSSPropertyID, const RenderStyle*, AdjustPixelValuesForComputedStyle = AdjustPixelValues);
    Ref<CSSPrimitiveValue> currentColorOrValidColor(const RenderStyle*, const Color&) const;

    Ref<CSSValueList> getCSSPropertyValuesForShorthandProperties(const StylePropertyShorthand&);
    PassRefPtr<CSSValueList> getCSSPropertyValuesFor2SidesShorthand(const StylePropertyShorthand&);
	PassRefPtr<CSSValueList> getCSSPropertyValuesFor4SidesShorthand(const StylePropertyShorthand&);

    size_t getLayerCount(CSSPropertyID);
    Ref<CSSValueList> getFillLayerPropertyShorthandValue(CSSPropertyID, const StylePropertyShorthand& propertiesBeforeSlashSeparator, const StylePropertyShorthand& propertiesAfterSlashSeparator, CSSPropertyID lastLayerProperty);
 	Ref<CSSValueList> getBackgroundShorthandValue();
    Ref<CSSValueList> getMaskShorthandValue();
    Ref<CSSValueList> getCSSPropertyValuesForGridShorthand(const StylePropertyShorthand&);

    RefPtr<Element> m_element;
    PseudoId m_pseudoElementSpecifier;
    bool m_allowVisitedStyle;
};

class CSSComputedStyleDeclaration FINAL : public CSSStyleDeclaration {
public:
    static PassRefPtr<CSSComputedStyleDeclaration> create(Element& element, bool allowVisitedStyle = false, const String& pseudoElementName = String())
    {
        return adoptRef(new CSSComputedStyleDeclaration(element, allowVisitedStyle, pseudoElementName));
    }
    virtual ~CSSComputedStyleDeclaration();

    virtual void ref() FINAL;
    virtual void deref() FINAL;

    String getPropertyValue(CSSPropertyID) const;

private:
    CSSComputedStyleDeclaration(Element&, bool allowVisitedStyle, const String&);

    // CSSOM functions. Don't make these public.
    virtual CSSRule* parentRule() const FINAL;
    virtual unsigned length() const FINAL;
    virtual String item(unsigned index) const FINAL;
    virtual PassRefPtr<DeprecatedCSSOMValue> getPropertyCSSValue(const String& propertyName) FINAL;
    virtual String getPropertyValue(const String& propertyName) FINAL;
    virtual String getPropertyPriority(const String& propertyName) FINAL;
    virtual String getPropertyShorthand(const String& propertyName) FINAL;
    virtual bool isPropertyImplicit(const String& propertyName) FINAL;
    virtual void setProperty(const String& propertyName, const String& value, const String& priority, ExceptionCode&) FINAL;
    virtual String removeProperty(const String& propertyName, ExceptionCode&) FINAL;
    virtual String cssText() const FINAL;
    virtual void setCssText(const String&, ExceptionCode&) FINAL;
    virtual PassRefPtr<CSSValue> getPropertyCSSValueInternal(CSSPropertyID) FINAL;
    virtual String getPropertyValueInternal(CSSPropertyID) FINAL;
    virtual bool setPropertyInternal(CSSPropertyID, const String& value, bool important, ExceptionCode&) FINAL;
    virtual PassRefPtr<MutableStylePropertySet> copyProperties() const FINAL;

	PassRefPtr<CSSValue> getPropertyCSSValue(CSSPropertyID, EUpdateLayout = UpdateLayout) const;

	mutable Ref<Element> m_element;
    PseudoId m_pseudoElementSpecifier;
    bool m_allowVisitedStyle;
    unsigned m_refCount;
};

} // namespace WebCore
