#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/Value.h>
#include <LibJS/Runtime/ValueInlines.h>
#include <LibWeb/Bindings/ExceptionOrUtils.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Bindings/SVGGradientElement.h>
#include <LibWeb/Bindings/SVGRadialGradientElement.h>
#include <LibWeb/SVG/SVGAnimatedLength.h>
#include <LibWeb/SVG/SVGRadialGradientElement.h>
#include <LibWeb/WebIDL/Tracing.h>

namespace Web::Bindings {

void SVGRadialGradientElementConstructor::initialize(JS::Realm& realm, JS::NativeFunction& object)
{
    auto& vm = realm.vm();
    [[maybe_unused]] u8 default_attributes = JS::Attribute::Enumerable;

    object.set_prototype(&ensure_web_constructor<SVGGradientElementPrototype>(realm, "SVGGradientElement"_fly_string));
    object.define_direct_property(vm.names.length, JS::Value(0), JS::Attribute::Configurable);
    object.define_direct_property(vm.names.name, JS::PrimitiveString::create(vm, "SVGRadialGradientElement"_utf16), JS::Attribute::Configurable);
    object.define_direct_property(vm.names.prototype, &ensure_web_prototype<SVGRadialGradientElementPrototype>(realm, "SVGRadialGradientElement"_fly_string), 0);
}

JS::ThrowCompletionOr<GC::Ref<JS::Object>> SVGRadialGradientElementConstructor::construct([[maybe_unused]] InterfaceConstructor& constructor, [[maybe_unused]] JS::FunctionObject& new_target)
{
    WebIDL::log_trace(constructor.vm(), "SVGRadialGradientElementConstructor::construct");
    return constructor.vm().throw_completion<JS::TypeError>(JS::ErrorType::NotAConstructor, "SVGRadialGradientElement");
}

void SVGRadialGradientElementPrototype::initialize(JS::Realm& realm, JS::Object& object)
{
    [[maybe_unused]] auto& vm = realm.vm();
    [[maybe_unused]] u8 default_attributes = JS::Attribute::Enumerable | JS::Attribute::Configurable | JS::Attribute::Writable;

    object.set_prototype(GC::Ref { ensure_web_prototype<SVGGradientElementPrototype>(realm, "SVGGradientElement"_fly_string) });

    auto cx_id = "cx"_utf16_fly_string;
    auto native_cx_getter = JS::NativeFunction::create(realm, cx_getter, 0, cx_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_cx_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto cx_attributes = default_attributes;

    // 5. Let desc be the PropertyDescriptor{[[Get]]: getter, [[Set]]: setter, [[Enumerable]]: true, [[Configurable]]: configurable}.

    // 7. Perform ! DefinePropertyOrThrow(target, id, desc).
    object.define_direct_accessor(cx_id, native_cx_getter, native_cx_setter, cx_attributes);

    // 8. FIXME: If attr’s type is an observable array type with type argument T, then set target’s backing observable array exotic object for attr to the result of creating an observable array exotic object in realm, given T, attr’s set an indexed value algorithm, and attr’s delete an indexed value algorithm.
    auto cy_id = "cy"_utf16_fly_string;
    auto native_cy_getter = JS::NativeFunction::create(realm, cy_getter, 0, cy_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_cy_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto cy_attributes = default_attributes;

    // 5. Let desc be the PropertyDescriptor{[[Get]]: getter, [[Set]]: setter, [[Enumerable]]: true, [[Configurable]]: configurable}.

    // 7. Perform ! DefinePropertyOrThrow(target, id, desc).
    object.define_direct_accessor(cy_id, native_cy_getter, native_cy_setter, cy_attributes);

    // 8. FIXME: If attr’s type is an observable array type with type argument T, then set target’s backing observable array exotic object for attr to the result of creating an observable array exotic object in realm, given T, attr’s set an indexed value algorithm, and attr’s delete an indexed value algorithm.
    auto r_id = "r"_utf16_fly_string;
    auto native_r_getter = JS::NativeFunction::create(realm, r_getter, 0, r_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_r_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto r_attributes = default_attributes;

    // 5. Let desc be the PropertyDescriptor{[[Get]]: getter, [[Set]]: setter, [[Enumerable]]: true, [[Configurable]]: configurable}.

    // 7. Perform ! DefinePropertyOrThrow(target, id, desc).
    object.define_direct_accessor(r_id, native_r_getter, native_r_setter, r_attributes);

    // 8. FIXME: If attr’s type is an observable array type with type argument T, then set target’s backing observable array exotic object for attr to the result of creating an observable array exotic object in realm, given T, attr’s set an indexed value algorithm, and attr’s delete an indexed value algorithm.
    auto fx_id = "fx"_utf16_fly_string;
    auto native_fx_getter = JS::NativeFunction::create(realm, fx_getter, 0, fx_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_fx_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto fx_attributes = default_attributes;

    // 5. Let desc be the PropertyDescriptor{[[Get]]: getter, [[Set]]: setter, [[Enumerable]]: true, [[Configurable]]: configurable}.

    // 7. Perform ! DefinePropertyOrThrow(target, id, desc).
    object.define_direct_accessor(fx_id, native_fx_getter, native_fx_setter, fx_attributes);

    // 8. FIXME: If attr’s type is an observable array type with type argument T, then set target’s backing observable array exotic object for attr to the result of creating an observable array exotic object in realm, given T, attr’s set an indexed value algorithm, and attr’s delete an indexed value algorithm.
    auto fy_id = "fy"_utf16_fly_string;
    auto native_fy_getter = JS::NativeFunction::create(realm, fy_getter, 0, fy_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_fy_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto fy_attributes = default_attributes;

    // 5. Let desc be the PropertyDescriptor{[[Get]]: getter, [[Set]]: setter, [[Enumerable]]: true, [[Configurable]]: configurable}.

    // 7. Perform ! DefinePropertyOrThrow(target, id, desc).
    object.define_direct_accessor(fy_id, native_fy_getter, native_fy_setter, fy_attributes);

    // 8. FIXME: If attr’s type is an observable array type with type argument T, then set target’s backing observable array exotic object for attr to the result of creating an observable array exotic object in realm, given T, attr’s set an indexed value algorithm, and attr’s delete an indexed value algorithm.
    auto fr_id = "fr"_utf16_fly_string;
    auto native_fr_getter = JS::NativeFunction::create(realm, fr_getter, 0, fr_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_fr_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto fr_attributes = default_attributes;

    // 5. Let desc be the PropertyDescriptor{[[Get]]: getter, [[Set]]: setter, [[Enumerable]]: true, [[Configurable]]: configurable}.

    // 7. Perform ! DefinePropertyOrThrow(target, id, desc).
    object.define_direct_accessor(fr_id, native_fr_getter, native_fr_setter, fr_attributes);

    // 8. FIXME: If attr’s type is an observable array type with type argument T, then set target’s backing observable array exotic object for attr to the result of creating an observable array exotic object in realm, given T, attr’s set an indexed value algorithm, and attr’s delete an indexed value algorithm.
    object.define_direct_property(vm.well_known_symbol_to_string_tag(), JS::PrimitiveString::create(vm, "SVGRadialGradientElement"_utf16), JS::Attribute::Configurable);
}

void SVGRadialGradientElementPrototype::define_unforgeable_attributes(JS::Realm& realm, [[maybe_unused]] JS::Object& object)
{
    [[maybe_unused]] auto& vm = realm.vm();
    [[maybe_unused]] u8 default_attributes = JS::Attribute::Enumerable;
}

[[maybe_unused]] static JS::ThrowCompletionOr<SVG::SVGRadialGradientElement*> impl_from(JS::VM& vm, JS::Value js_value)
{

    if (auto impl = js_value.as_if<SVG::SVGRadialGradientElement>())
        return impl.ptr();
    return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "SVGRadialGradientElement");
}

[[maybe_unused]] static JS::ThrowCompletionOr<SVG::SVGRadialGradientElement*> impl_from(JS::VM& vm)
{
    auto this_value = vm.this_value();
    if (this_value.is_nullish())
        this_value = &vm.current_realm()->global_object();
    return impl_from(vm, this_value);
}

JS_DEFINE_NATIVE_FUNCTION(SVGRadialGradientElementPrototype::cx_getter)
{
    WebIDL::log_trace(vm, "SVGRadialGradientElementPrototype::cx_getter");
    [[maybe_unused]] auto& realm = *vm.current_realm();

    auto* idl_object = TRY(impl_from(vm));


    auto R = TRY(throw_dom_exception_if_needed(vm, [&] { return idl_object->cx(); }));

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(SVGRadialGradientElementPrototype::cy_getter)
{
    WebIDL::log_trace(vm, "SVGRadialGradientElementPrototype::cy_getter");
    [[maybe_unused]] auto& realm = *vm.current_realm();

    auto* idl_object = TRY(impl_from(vm));


    auto R = TRY(throw_dom_exception_if_needed(vm, [&] { return idl_object->cy(); }));

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(SVGRadialGradientElementPrototype::r_getter)
{
    WebIDL::log_trace(vm, "SVGRadialGradientElementPrototype::r_getter");
    [[maybe_unused]] auto& realm = *vm.current_realm();

    auto* idl_object = TRY(impl_from(vm));


    auto R = TRY(throw_dom_exception_if_needed(vm, [&] { return idl_object->r(); }));

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(SVGRadialGradientElementPrototype::fx_getter)
{
    WebIDL::log_trace(vm, "SVGRadialGradientElementPrototype::fx_getter");
    [[maybe_unused]] auto& realm = *vm.current_realm();

    auto* idl_object = TRY(impl_from(vm));


    auto R = TRY(throw_dom_exception_if_needed(vm, [&] { return idl_object->fx(); }));

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(SVGRadialGradientElementPrototype::fy_getter)
{
    WebIDL::log_trace(vm, "SVGRadialGradientElementPrototype::fy_getter");
    [[maybe_unused]] auto& realm = *vm.current_realm();

    auto* idl_object = TRY(impl_from(vm));


    auto R = TRY(throw_dom_exception_if_needed(vm, [&] { return idl_object->fy(); }));

    return JS::Value(R);
}

JS_DEFINE_NATIVE_FUNCTION(SVGRadialGradientElementPrototype::fr_getter)
{
    WebIDL::log_trace(vm, "SVGRadialGradientElementPrototype::fr_getter");
    [[maybe_unused]] auto& realm = *vm.current_realm();

    auto* idl_object = TRY(impl_from(vm));


    auto R = TRY(throw_dom_exception_if_needed(vm, [&] { return idl_object->fr(); }));

    return JS::Value(R);
}

} // namespace Web::Bindings
