/*
 * 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
 */

#pragma once

#include <AK/Vector.h>
#include <LibWeb/CSS/StyleValues/AbstractImageStyleValue.h>
#include <LibWeb/CSS/StyleValues/ColorInterpolationMethodStyleValue.h>
#include <LibWeb/Painting/GradientPainting.h>

namespace Web::CSS {

class RadialGradientStyleValue final : public AbstractImageStyleValue {
public:
    enum class EndingShape {
        Circle,
        Ellipse
    };

    static ValueComparingNonnullRefPtr<RadialGradientStyleValue const> create(EndingShape ending_shape, NonnullRefPtr<StyleValue const> size, ValueComparingNonnullRefPtr<PositionStyleValue const> position, Vector<ColorStopListElement> color_stop_list, GradientRepeating repeating, RefPtr<StyleValue const> color_interpolation_method)
    {
        VERIFY(!color_stop_list.is_empty());
        bool any_non_legacy = color_stop_list.find_first_index_if([](auto const& stop) { return !stop.color_stop.color->is_keyword() && stop.color_stop.color->as_color().color_syntax() == ColorSyntax::Modern; }).has_value();
        return adopt_ref(*new (nothrow) RadialGradientStyleValue(ending_shape, move(size), move(position), move(color_stop_list), repeating, move(color_interpolation_method), any_non_legacy ? ColorSyntax::Modern : ColorSyntax::Legacy));
    }

    virtual void serialize(StringBuilder&, SerializationMode) const override;

    void paint(DisplayListRecordingContext&, DOM::Document const&, DevicePixelRect const& dest_rect, CSS::ImageRendering) const override;

    virtual ValueComparingNonnullRefPtr<StyleValue const> absolutized(ComputationContext const&) const override;
    virtual bool equals(StyleValue const& other) const override;

    virtual bool is_computationally_independent() const override;

    Vector<ColorStopListElement> const& color_stop_list() const
    {
        return m_properties.color_stop_list;
    }

    ColorInterpolationMethodStyleValue::ColorInterpolationMethod interpolation_method() const
    {
        if (m_properties.color_interpolation_method)
            return m_properties.color_interpolation_method->as_color_interpolation_method().color_interpolation_method();

        return ColorInterpolationMethodStyleValue::default_color_interpolation_method(m_properties.color_syntax);
    }

    bool is_paintable(DOM::Document const&) const override { return true; }

    void resolve_for_size(Layout::NodeWithStyle const&, CSSPixelSize) const override;

    CSSPixelSize resolve_size(CSSPixelPoint, CSSPixelRect const&, Layout::NodeWithStyle const&) const;

    bool is_repeating() const { return m_properties.repeating == GradientRepeating::Yes; }

    virtual ~RadialGradientStyleValue() override = default;

private:
    RadialGradientStyleValue(EndingShape ending_shape, NonnullRefPtr<StyleValue const> size, ValueComparingNonnullRefPtr<PositionStyleValue const> position, Vector<ColorStopListElement> color_stop_list, GradientRepeating repeating, ValueComparingRefPtr<StyleValue const> color_interpolation_method, ColorSyntax color_syntax)
        : AbstractImageStyleValue(Type::RadialGradient)
        , m_properties { .ending_shape = ending_shape, .size = move(size), .position = move(position), .color_stop_list = move(color_stop_list), .repeating = repeating, .color_interpolation_method = move(color_interpolation_method), .color_syntax = color_syntax }
    {
    }

    struct Properties {
        EndingShape ending_shape;
        ValueComparingNonnullRefPtr<StyleValue const> size;
        ValueComparingNonnullRefPtr<PositionStyleValue const> position;
        Vector<ColorStopListElement> color_stop_list;
        GradientRepeating repeating;
        ValueComparingRefPtr<StyleValue const> color_interpolation_method;
        ColorSyntax color_syntax;
        bool operator==(Properties const&) const = default;
    } m_properties;

    mutable Optional<CSSPixelSize> m_resolved_size;

    struct ResolvedData {
        Painting::RadialGradientData data;
        CSSPixelSize gradient_size;
        CSSPixelPoint center;
    };
    mutable Optional<ResolvedData> m_resolved;
};

}
