/*
 * 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 "ConicGradientStyleValue.h"
#include <LibWeb/CSS/StyleValues/AngleStyleValue.h>
#include <LibWeb/CSS/StyleValues/PositionStyleValue.h>
#include <LibWeb/Layout/Node.h>
#include <LibWeb/Painting/DisplayListRecorder.h>

namespace Web::CSS {

void ConicGradientStyleValue::serialize(StringBuilder& builder, SerializationMode mode) const
{
    if (is_repeating())
        builder.append("repeating-"sv);
    builder.append("conic-gradient("sv);
    bool has_from_angle = m_properties.from_angle;
    bool has_at_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_from_angle) {
        builder.append("from "sv);
        m_properties.from_angle->serialize(builder, mode);
    }
    if (has_at_position) {
        if (has_from_angle)
            builder.append(' ');
        builder.append("at "sv);
        m_properties.position->serialize(builder, mode);
    }
    if (has_color_space) {
        if (has_from_angle || has_at_position)
            builder.append(' ');
        m_properties.color_interpolation_method->serialize(builder, mode);
    }
    if (has_from_angle || has_at_position || has_color_space)
        builder.append(", "sv);
    serialize_color_stop_list(builder, m_properties.color_stop_list, mode);
    builder.append(')');
}

void ConicGradientStyleValue::resolve_for_size(Layout::NodeWithStyle const& node, CSSPixelSize size) const
{
    ResolvedDataCacheKey cache_key {
        .length_resolution_context = Length::ResolutionContext::for_layout_node(node),
        .size = size,
    };
    if (m_resolved_data_cache_key != cache_key) {
        m_resolved_data_cache_key = move(cache_key);
        m_resolved = ResolvedData { Painting::resolve_conic_gradient_data(node, *this), {} };
    }
    m_resolved->position = m_properties.position->resolved(node, CSSPixelRect { { 0, 0 }, size });
}

void ConicGradientStyleValue::paint(DisplayListRecordingContext& context, DevicePixelRect const& dest_rect, CSS::ImageRendering) const
{
    VERIFY(m_resolved.has_value());
    auto destination_rect = dest_rect.to_type<int>();
    auto position = context.rounded_device_point(m_resolved->position).to_type<int>();
    context.display_list_recorder().fill_rect_with_conic_gradient(destination_rect, m_resolved->data, position);
}

ValueComparingNonnullRefPtr<StyleValue const> ConicGradientStyleValue::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));
    }
    RefPtr<StyleValue const> absolutized_from_angle;
    if (m_properties.from_angle)
        absolutized_from_angle = m_properties.from_angle->absolutized(context);
    ValueComparingNonnullRefPtr<PositionStyleValue const> 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(move(absolutized_from_angle), move(absolutized_position), move(absolutized_color_stops), m_properties.repeating, move(absolutized_color_interpolation_method));
}

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

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

float ConicGradientStyleValue::angle_degrees() const
{
    if (!m_properties.from_angle)
        return 0;
    return Angle::from_style_value(*m_properties.from_angle, {}).to_degrees();
}

}
