/*
 * Copyright (c) 2022-2025, Sam Atkins <sam@ladybird.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/String.h>
#include <AK/StringBuilder.h>
#include <AK/Variant.h>
#include <LibWeb/CSS/Angle.h>
#include <LibWeb/CSS/Frequency.h>
#include <LibWeb/CSS/Length.h>
#include <LibWeb/CSS/Number.h>
#include <LibWeb/CSS/Percentage.h>
#include <LibWeb/CSS/StyleValues/CalculatedStyleValue.h>
#include <LibWeb/CSS/StyleValues/LengthStyleValue.h>
#include <LibWeb/CSS/StyleValues/PercentageStyleValue.h>
#include <LibWeb/CSS/Time.h>

// FIXME: LengthPercentage is the only remaining derived class of PercentageOr so we can just merge them together. It
//        should also probably instead be CSSPixelsPercentage since it is only used after computation where we resolve
//        all relative lengths.
namespace Web::CSS {

template<typename T>
class PercentageOr {
public:
    PercentageOr(T t)
        : m_value(move(t))
    {
    }

    PercentageOr(Percentage percentage)
        : m_value(move(percentage))
    {
    }

    PercentageOr(NonnullRefPtr<CalculatedStyleValue const> calculated)
        : m_value(move(calculated))
    {
    }

    ~PercentageOr() = default;

    PercentageOr<T>& operator=(T t)
    {
        m_value = move(t);
        return *this;
    }

    PercentageOr<T>& operator=(Percentage percentage)
    {
        m_value = move(percentage);
        return *this;
    }

    bool is_percentage() const { return m_value.template has<Percentage>(); }
    bool is_calculated() const { return m_value.template has<NonnullRefPtr<CalculatedStyleValue const>>(); }

    bool contains_percentage() const
    {
        return m_value.visit(
            [&](T const&) {
                return false;
            },
            [&](Percentage const&) {
                return true;
            },
            [&](NonnullRefPtr<CalculatedStyleValue const> const& calculated) {
                return calculated->contains_percentage();
            });
    }

    Percentage const& percentage() const
    {
        VERIFY(is_percentage());
        return m_value.template get<Percentage>();
    }

    NonnullRefPtr<CalculatedStyleValue const> const& calculated() const
    {
        VERIFY(is_calculated());
        return m_value.template get<NonnullRefPtr<CalculatedStyleValue const>>();
    }

    CSSPixels to_px(Layout::Node const& layout_node, CSSPixels reference_value) const
    {
        if constexpr (IsSame<T, Length>) {
            if (auto const* length = m_value.template get_pointer<Length>()) {
                if (length->is_absolute())
                    return length->absolute_length_to_px();
            }
        }
        return resolved(layout_node, reference_value).to_px(layout_node);
    }

    T resolved(Layout::Node const& layout_node, T reference_value) const
    requires(!IsSame<T, Length>)
    {
        return m_value.visit(
            [&](T const& t) {
                return t;
            },
            [&](Percentage const& percentage) {
                return reference_value.percentage_of(percentage);
            },
            [&](NonnullRefPtr<CalculatedStyleValue const> const& calculated) {
                return T::resolve_calculated(calculated, layout_node, reference_value);
            });
    }

    T resolved(Layout::Node const& layout_node, CSSPixels reference_value) const
    {
        return m_value.visit(
            [&](T const& t) {
                return t;
            },
            [&](Percentage const& percentage) {
                // FIXME: This doesn't necessarily round correctly
                return Length::make_px(CSSPixels(percentage.value() * reference_value) / 100);
            },
            [&](NonnullRefPtr<CalculatedStyleValue const> const& calculated) {
                return T::resolve_calculated(calculated, layout_node, reference_value);
            });
    }

    void serialize(StringBuilder& builder, SerializationMode mode) const
    {
        if (is_calculated()) {
            m_value.template get<NonnullRefPtr<CalculatedStyleValue const>>()->serialize(builder, mode);
        } else if (is_percentage()) {
            m_value.template get<Percentage>().serialize(builder, mode);
        } else {
            m_value.template get<T>().serialize(builder, mode);
        }
    }

    String to_string(SerializationMode mode) const
    {
        StringBuilder builder;
        serialize(builder, mode);
        return builder.to_string_without_validation();
    }

    bool operator==(PercentageOr<T> const& other) const
    {
        if (is_calculated() != other.is_calculated())
            return false;
        if (is_percentage() != other.is_percentage())
            return false;
        if (is_calculated())
            return (*m_value.template get<NonnullRefPtr<CalculatedStyleValue const>>() == *other.m_value.template get<NonnullRefPtr<CalculatedStyleValue const>>());
        if (is_percentage())
            return (m_value.template get<Percentage>() == other.m_value.template get<Percentage>());
        return (m_value.template get<T>() == other.m_value.template get<T>());
    }

protected:
    bool is_t() const { return m_value.template has<T>(); }
    T const& get_t() const { return m_value.template get<T>(); }

private:
    Variant<T, Percentage, NonnullRefPtr<CalculatedStyleValue const>> m_value;
};

template<typename T>
bool operator==(PercentageOr<T> const& percentage_or, T const& t)
{
    return percentage_or == PercentageOr<T> { t };
}

template<typename T>
bool operator==(T const& t, PercentageOr<T> const& percentage_or)
{
    return t == percentage_or;
}

template<typename T>
bool operator==(PercentageOr<T> const& percentage_or, Percentage const& percentage)
{
    return percentage_or == PercentageOr<T> { percentage };
}

template<typename T>
bool operator==(Percentage const& percentage, PercentageOr<T> const& percentage_or)
{
    return percentage == percentage_or;
}

class LengthPercentage : public PercentageOr<Length> {
public:
    using PercentageOr<Length>::PercentageOr;

    static LengthPercentage from_style_value(NonnullRefPtr<StyleValue const> const& style_value)
    {
        if (style_value->is_percentage())
            return LengthPercentage { style_value->as_percentage().percentage() };
        if (style_value->is_length())
            return LengthPercentage { style_value->as_length().length() };
        if (style_value->is_calculated())
            return LengthPercentage { style_value->as_calculated() };

        VERIFY_NOT_REACHED();
    }

    bool is_length() const { return is_t(); }
    Length const& length() const { return get_t(); }
};

class LengthPercentageOrAuto {
public:
    LengthPercentageOrAuto(LengthPercentage length_percentage)
        : m_length_percentage(move(length_percentage))
    {
    }

    LengthPercentageOrAuto(Length length)
        : m_length_percentage(move(length))
    {
    }

    LengthPercentageOrAuto(Percentage percentage)
        : m_length_percentage(move(percentage))
    {
    }

    static LengthPercentageOrAuto make_auto()
    {
        return LengthPercentageOrAuto();
    }

    static LengthPercentageOrAuto from_style_value(NonnullRefPtr<StyleValue const> const& style_value)
    {
        if (style_value->has_auto())
            return LengthPercentageOrAuto::make_auto();

        return LengthPercentage::from_style_value(style_value);
    }

    bool is_auto() const { return !m_length_percentage.has_value(); }
    bool is_length() const { return m_length_percentage.has_value() && m_length_percentage->is_length(); }
    bool is_percentage() const { return m_length_percentage.has_value() && m_length_percentage->is_percentage(); }
    bool is_calculated() const { return m_length_percentage.has_value() && m_length_percentage->is_calculated(); }

    bool contains_percentage() const { return m_length_percentage.has_value() && m_length_percentage->contains_percentage(); }

    LengthPercentage const& length_percentage() const { return m_length_percentage.value(); }
    Length const& length() const { return m_length_percentage->length(); }
    Percentage const& percentage() const { return m_length_percentage->percentage(); }
    NonnullRefPtr<CalculatedStyleValue const> const& calculated() const { return m_length_percentage->calculated(); }

    LengthOrAuto resolved_or_auto(Layout::Node const& layout_node, CSSPixels reference_value) const
    {
        if (is_auto())
            return LengthOrAuto::make_auto();
        return length_percentage().resolved(layout_node, reference_value);
    }

    CSSPixels to_px_or_zero(Layout::Node const& layout_node, CSSPixels reference_value) const
    {
        if (is_auto())
            return 0;
        return length_percentage().to_px(layout_node, reference_value);
    }

    void serialize(StringBuilder& builder, SerializationMode mode) const
    {
        if (is_auto())
            builder.append("auto"sv);
        else
            m_length_percentage->serialize(builder, mode);
    }

    String to_string(SerializationMode mode) const
    {
        StringBuilder builder;
        serialize(builder, mode);
        return builder.to_string_without_validation();
    }

    bool operator==(LengthPercentageOrAuto const&) const = default;

private:
    LengthPercentageOrAuto() = default;

    Optional<LengthPercentage> m_length_percentage;
};

}

template<>
struct AK::Formatter<Web::CSS::LengthPercentage> : Formatter<StringView> {
    ErrorOr<void> format(FormatBuilder& builder, Web::CSS::LengthPercentage const& length_percentage)
    {
        return Formatter<StringView>::format(builder, length_percentage.to_string(Web::CSS::SerializationMode::Normal));
    }
};

template<>
struct AK::Formatter<Web::CSS::LengthPercentageOrAuto> : Formatter<StringView> {
    ErrorOr<void> format(FormatBuilder& builder, Web::CSS::LengthPercentageOrAuto const& length_percentage_or_auto)
    {
        return Formatter<StringView>::format(builder, length_percentage_or_auto.to_string(Web::CSS::SerializationMode::Normal));
    }
};
