/*
 * Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
 * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
 * Copyright (c) 2021-2025, Sam Atkins <sam@ladybird.org>
 * Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "RadialGradientStyleValue.h"
#include <LibWeb/CSS/StyleValues/PositionStyleValue.h>
#include <LibWeb/CSS/StyleValues/RadialSizeStyleValue.h>
#include <LibWeb/Layout/Node.h>
#include <LibWeb/Painting/DisplayListRecorder.h>

namespace Web::CSS {

void RadialGradientStyleValue::serialize(StringBuilder& builder, SerializationMode mode) const
{
    if (is_repeating())
        builder.append("repeating-"sv);
    builder.append("radial-gradient("sv);

    // AD-HOC: We need to check the serialized size to determine if it should be included.
    auto const& serialized_size = m_properties.size->to_string(mode);

    bool has_size = serialized_size != "farthest-corner"sv;
    bool has_position = !m_properties.position->is_center(mode);
    bool has_color_space = m_properties.color_interpolation_method && m_properties.color_interpolation_method->as_color_interpolation_method().color_interpolation_method() != ColorInterpolationMethodStyleValue::default_color_interpolation_method(m_properties.color_syntax);

    if (has_size)
        m_properties.size->serialize(builder, mode);

    if (has_position) {
        if (has_size)
            builder.append(' ');

        builder.append("at "sv);
        m_properties.position->serialize(builder, mode);
    }

    if (has_color_space) {
        if (has_size || has_position)
            builder.append(' ');

        m_properties.color_interpolation_method->serialize(builder, mode);
    }

    if (has_size || has_position || has_color_space)
        builder.append(", "sv);

    serialize_color_stop_list(builder, m_properties.color_stop_list, mode);
    builder.append(')');
}

CSSPixelSize RadialGradientStyleValue::resolve_size(CSSPixelPoint center, CSSPixelRect const& reference_box, Layout::NodeWithStyle const& node) const
{
    if (m_properties.ending_shape == EndingShape::Circle) {
        auto radius = m_properties.size->as_radial_size().resolve_circle_size(center, reference_box, node);
        return CSSPixelSize { radius, radius };
    }

    return m_properties.size->as_radial_size().resolve_ellipse_size(center, reference_box, node);
}

void RadialGradientStyleValue::resolve_for_size(Layout::NodeWithStyle const& node, CSSPixelSize paint_size) const
{
    CSSPixelRect gradient_box { { 0, 0 }, paint_size };
    auto center = m_properties.position->resolved(node, gradient_box);
    auto gradient_size = resolve_size(center, gradient_box, node);

    ResolvedDataCacheKey cache_key {
        .length_resolution_context = Length::ResolutionContext::for_layout_node(node),
        .size = paint_size,
    };
    if (m_resolved_data_cache_key != cache_key) {
        m_resolved_data_cache_key = move(cache_key);
        m_resolved = ResolvedData {
            Painting::resolve_radial_gradient_data(node, gradient_size, *this),
            gradient_size,
            center,
        };
    }
}

ValueComparingNonnullRefPtr<StyleValue const> RadialGradientStyleValue::absolutized(ComputationContext const& context) const
{
    Vector<ColorStopListElement> absolutized_color_stops;
    absolutized_color_stops.ensure_capacity(m_properties.color_stop_list.size());
    for (auto const& color_stop : m_properties.color_stop_list) {
        absolutized_color_stops.unchecked_append(color_stop.absolutized(context));
    }

    auto absolutized_size = m_properties.size->absolutized(context);
    NonnullRefPtr absolutized_position = m_properties.position->absolutized(context)->as_position();

    auto absolutized_color_interpolation_method = m_properties.color_interpolation_method ? ValueComparingRefPtr<StyleValue const> { m_properties.color_interpolation_method->absolutized(context) } : nullptr;

    return create(m_properties.ending_shape, move(absolutized_size), move(absolutized_position), move(absolutized_color_stops), m_properties.repeating, move(absolutized_color_interpolation_method));
}

bool RadialGradientStyleValue::equals(StyleValue const& other) const
{
    if (type() != other.type())
        return false;
    auto& other_gradient = other.as_radial_gradient();
    return m_properties == other_gradient.m_properties;
}

bool RadialGradientStyleValue::is_computationally_independent() const
{
    return m_properties.size->is_computationally_independent()
        && m_properties.position->is_computationally_independent()
        && all_of(m_properties.color_stop_list, [&](auto const& stop) { return stop.color_stop.color->is_computationally_independent(); })
        && (!m_properties.color_interpolation_method || m_properties.color_interpolation_method->is_computationally_independent());
}

void RadialGradientStyleValue::paint(DisplayListRecordingContext& context, DevicePixelRect const& dest_rect, CSS::ImageRendering) const
{
    VERIFY(m_resolved.has_value());
    auto center = context.rounded_device_point(m_resolved->center).to_type<int>();
    auto size = context.rounded_device_size(m_resolved->gradient_size).to_type<int>();
    context.display_list_recorder().fill_rect_with_radial_gradient(dest_rect.to_type<int>(), m_resolved->data, center, size);
}

}
