/*
 * Copyright (C) 2004, 2005, 2006, 2008 Nikolas Zimmermann <zimmermann@kde.org>
 * Copyright (C) 2004, 2005, 2006 Rob Buis <buis@kde.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#pragma once

#include "AnimationUtilities.h"
#include "ExceptionCode.h"
#include "SVGParsingError.h"
#include "SVGPropertyTraits.h"

namespace WebCore {

class CSSPrimitiveValue;
class CSSToLengthConversionData;
class Element;
class QualifiedName;
class SVGLengthContext;

enum class SVGLengthType : uint8_t {
    Unknown = 0,
    Number,
    Percentage,
    Ems,
    Exs,
    Pixels,
    Centimeters,
    Millimeters,
    Inches,
    Points,
    Picas
};

enum class SVGLengthMode : uint8_t {
    Width,
    Height,
    Other
};

enum SVGLengthNegativeValuesMode {
    AllowNegativeLengths,
    ForbidNegativeLengths
};

enum class ShouldConvertNumberToPxLength : bool { No, Yes };

class SVGLengthValue {
    WTF_MAKE_FAST_ALLOCATED;
public:
    // FIXME: Once all SVGLengthValue users use Length internally, we make this a wrapper for Length.
    SVGLengthValue(SVGLengthMode = SVGLengthMode::Other, const String& valueAsString = String());
    SVGLengthValue(float valueInSpecifiedUnits, SVGLengthType, SVGLengthMode = SVGLengthMode::Other);
    SVGLengthValue(const SVGLengthContext&, float, SVGLengthMode = SVGLengthMode::Other, SVGLengthType = SVGLengthType::Number);

    static SVGLengthValue construct(SVGLengthMode, const String&, SVGParsingError&, SVGLengthNegativeValuesMode = AllowNegativeLengths);
    static SVGLengthValue blend(const SVGLengthValue& from, const SVGLengthValue& to, float progress);

    float value(const SVGLengthContext&) const;
    float valueForBindings(const SVGLengthContext&, ExceptionCode&) const;
    void setValue(float, const SVGLengthContext&, ExceptionCode&);
    void setValue(const SVGLengthContext&, float, SVGLengthMode, SVGLengthType, ExceptionCode&);

    float valueInSpecifiedUnits() const { return m_valueInSpecifiedUnits; }
    void setValueInSpecifiedUnits(float value) { m_valueInSpecifiedUnits = value; }

    float valueAsPercentage() const;

    String valueAsString() const;
    AtomicString valueAsAtomicString() const;

    void setValueAsString(const String&, ExceptionCode&);
    void setValueAsString(const String&, SVGLengthMode, ExceptionCode&);
    
    void convertToSpecifiedUnits(const SVGLengthContext&, SVGLengthType, ExceptionCode&);

    SVGLengthType lengthType() const { return m_lengthType; }
    SVGLengthMode lengthMode() const { return m_lengthMode; }

    // Helper functions
    bool isRelative() const
    {
        auto type = lengthType();
        return type == SVGLengthType::Percentage || type == SVGLengthType::Ems || type == SVGLengthType::Exs;
    }

    bool isZero() const 
    {
        return !m_valueInSpecifiedUnits;
    }

    static SVGLengthValue fromCSSPrimitiveValue(const CSSPrimitiveValue&, const CSSToLengthConversionData&, ShouldConvertNumberToPxLength = ShouldConvertNumberToPxLength::No);
    Ref<CSSPrimitiveValue> toCSSPrimitiveValue(const Element* = nullptr) const;
    static SVGLengthMode lengthModeForAnimatedLengthAttribute(const QualifiedName&);

private:
    float m_valueInSpecifiedUnits { 0 };
    SVGLengthType m_lengthType { SVGLengthType::Number };
    SVGLengthMode m_lengthMode { SVGLengthMode::Other };
};

inline bool operator==(const SVGLengthValue& a, const SVGLengthValue& b)
{
    return a.valueInSpecifiedUnits() == b.valueInSpecifiedUnits() && a.lengthType() == b.lengthType() && a.lengthMode() == b.lengthMode();
}

inline bool operator!=(const SVGLengthValue& a, const SVGLengthValue& b)
{
    return a.valueInSpecifiedUnits() != b.valueInSpecifiedUnits() || a.lengthType() != b.lengthType() || a.lengthMode() != b.lengthMode();
}

template<> struct SVGPropertyTraits<SVGLengthValue> {
    static SVGLengthValue initialValue() { return SVGLengthValue(); }
    static Optional<SVGLengthValue> parse(const QualifiedName& attrName, const String& string)
	{
		SVGLengthValue length;
		ExceptionCode ec = 0;
		length.setValueAsString(string, SVGLengthValue::lengthModeForAnimatedLengthAttribute(attrName), ec);
		return length;
	}
	static String toString(const SVGLengthValue& length) { return length.valueAsString(); }
};

TextStream& operator<<(TextStream&, const SVGLengthValue&);

} // namespace WebCore
