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

#pragma once

#include <AK/Optional.h>
#include <LibGfx/Color.h>
#include <LibGfx/Cursor.h>
#include <LibWeb/CSS/Length.h>
#include <LibWeb/CSS/StyleValues/StyleValue.h>
#include <LibWeb/Forward.h>

namespace Web::CSS {

class CursorStyleValue final : public StyleValueWithDefaultOperators<CursorStyleValue> {
public:
    static ValueComparingNonnullRefPtr<CursorStyleValue const> create(ValueComparingNonnullRefPtr<AbstractImageStyleValue const> image, RefPtr<StyleValue const> x, RefPtr<StyleValue const> y)
    {
        // We require either both or neither the X and Y parameters
        VERIFY((!x && !y) || (x && y));
        return adopt_ref(*new (nothrow) CursorStyleValue(move(image), move(x), move(y)));
    }
    virtual ~CursorStyleValue() override = default;

    Optional<Gfx::ImageCursor> make_image_cursor(Layout::NodeWithStyle const&) const;

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

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

    bool properties_equal(CursorStyleValue const& other) const { return m_properties == other.m_properties; }

    virtual bool is_computationally_independent() const override;

private:
    CursorStyleValue(ValueComparingNonnullRefPtr<AbstractImageStyleValue const> image,
        RefPtr<StyleValue const> x,
        RefPtr<StyleValue const> y)
        : StyleValueWithDefaultOperators(Type::Cursor)
        , m_properties { .image = move(image), .x = move(x), .y = move(y) }
    {
    }

    struct Properties {
        ValueComparingNonnullRefPtr<AbstractImageStyleValue const> image;
        ValueComparingRefPtr<StyleValue const> x;
        ValueComparingRefPtr<StyleValue const> y;
        bool operator==(Properties const&) const = default;
    } m_properties;

    mutable Optional<Color> m_cached_bitmap_color;
    mutable Optional<Gfx::ShareableBitmap> m_cached_bitmap;
};

}
