#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/PropertyDescriptor.h>
#include <LibJS/Runtime/Value.h>
#include <LibJS/Runtime/ValueInlines.h>
#include <LibWeb/Bindings/ExceptionOrUtils.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Bindings/SVGGraphicsElement.h>
#include <LibWeb/Bindings/SVGTextContentElement.h>
#include <LibWeb/Geometry/DOMPoint.h>
#include <LibWeb/SVG/SVGTextContentElement.h>
#include <LibWeb/WebIDL/AbstractOperations.h>
#include <LibWeb/WebIDL/Tracing.h>
#include <LibWeb/WebIDL/Types.h>

namespace Web::Bindings {

void SVGTextContentElementConstructor::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<SVGGraphicsElementPrototype>(realm, "SVGGraphicsElement"_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, "SVGTextContentElement"_utf16), JS::Attribute::Configurable);
    object.define_direct_property(vm.names.prototype, &ensure_web_prototype<SVGTextContentElementPrototype>(realm, "SVGTextContentElement"_fly_string), 0);

    // 1. FIXME: If const is not exposed in realm, then continue.
    // 2. Let value be the result of converting const’s IDL value to a JavaScript value.
    // 3. Let desc be the PropertyDescriptor{[[Writable]]: false, [[Enumerable]]: true, [[Configurable]]: false, [[Value]]: value}.
    // 4. Let id be const’s identifier.
    // 5. Perform ! DefinePropertyOrThrow(target, id, desc).
    object.define_direct_property("LENGTHADJUST_UNKNOWN"_utf16_fly_string, JS::Value(static_cast<WebIDL::UnsignedShort>(0)), JS::Attribute::Enumerable);
    // 1. FIXME: If const is not exposed in realm, then continue.
    // 2. Let value be the result of converting const’s IDL value to a JavaScript value.
    // 3. Let desc be the PropertyDescriptor{[[Writable]]: false, [[Enumerable]]: true, [[Configurable]]: false, [[Value]]: value}.
    // 4. Let id be const’s identifier.
    // 5. Perform ! DefinePropertyOrThrow(target, id, desc).
    object.define_direct_property("LENGTHADJUST_SPACING"_utf16_fly_string, JS::Value(static_cast<WebIDL::UnsignedShort>(1)), JS::Attribute::Enumerable);
    // 1. FIXME: If const is not exposed in realm, then continue.
    // 2. Let value be the result of converting const’s IDL value to a JavaScript value.
    // 3. Let desc be the PropertyDescriptor{[[Writable]]: false, [[Enumerable]]: true, [[Configurable]]: false, [[Value]]: value}.
    // 4. Let id be const’s identifier.
    // 5. Perform ! DefinePropertyOrThrow(target, id, desc).
    object.define_direct_property("LENGTHADJUST_SPACINGANDGLYPHS"_utf16_fly_string, JS::Value(static_cast<WebIDL::UnsignedShort>(2)), JS::Attribute::Enumerable);
}

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

void SVGTextContentElementPrototype::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<SVGGraphicsElementPrototype>(realm, "SVGGraphicsElement"_fly_string) });
    object.define_native_function(realm, "getNumberOfChars"_utf16_fly_string, get_number_of_chars, 0, default_attributes);

    object.define_native_function(realm, "getStartPositionOfChar"_utf16_fly_string, get_start_position_of_char, 1, default_attributes);

    object.define_direct_property("getComputedTextLength"_utf16_fly_string, JS::js_undefined(), default_attributes | JS::Attribute::Unimplemented);

    object.define_direct_property("getSubStringLength"_utf16_fly_string, JS::js_undefined(), default_attributes | JS::Attribute::Unimplemented);

    object.define_direct_property("getEndPositionOfChar"_utf16_fly_string, JS::js_undefined(), default_attributes | JS::Attribute::Unimplemented);

    object.define_direct_property("getExtentOfChar"_utf16_fly_string, JS::js_undefined(), default_attributes | JS::Attribute::Unimplemented);

    object.define_direct_property("getRotationOfChar"_utf16_fly_string, JS::js_undefined(), default_attributes | JS::Attribute::Unimplemented);

    object.define_direct_property("getCharNumAtPosition"_utf16_fly_string, JS::js_undefined(), default_attributes | JS::Attribute::Unimplemented);

    object.define_direct_property("selectSubString"_utf16_fly_string, JS::js_undefined(), default_attributes | JS::Attribute::Unimplemented);


    // 1. FIXME: If const is not exposed in realm, then continue.
    // 2. Let value be the result of converting const’s IDL value to a JavaScript value.
    // 3. Let desc be the PropertyDescriptor{[[Writable]]: false, [[Enumerable]]: true, [[Configurable]]: false, [[Value]]: value}.
    // 4. Let id be const’s identifier.
    // 5. Perform ! DefinePropertyOrThrow(target, id, desc).
    object.define_direct_property("LENGTHADJUST_UNKNOWN"_utf16_fly_string, JS::Value(static_cast<WebIDL::UnsignedShort>(0)), JS::Attribute::Enumerable);
    // 1. FIXME: If const is not exposed in realm, then continue.
    // 2. Let value be the result of converting const’s IDL value to a JavaScript value.
    // 3. Let desc be the PropertyDescriptor{[[Writable]]: false, [[Enumerable]]: true, [[Configurable]]: false, [[Value]]: value}.
    // 4. Let id be const’s identifier.
    // 5. Perform ! DefinePropertyOrThrow(target, id, desc).
    object.define_direct_property("LENGTHADJUST_SPACING"_utf16_fly_string, JS::Value(static_cast<WebIDL::UnsignedShort>(1)), JS::Attribute::Enumerable);
    // 1. FIXME: If const is not exposed in realm, then continue.
    // 2. Let value be the result of converting const’s IDL value to a JavaScript value.
    // 3. Let desc be the PropertyDescriptor{[[Writable]]: false, [[Enumerable]]: true, [[Configurable]]: false, [[Value]]: value}.
    // 4. Let id be const’s identifier.
    // 5. Perform ! DefinePropertyOrThrow(target, id, desc).
    object.define_direct_property("LENGTHADJUST_SPACINGANDGLYPHS"_utf16_fly_string, JS::Value(static_cast<WebIDL::UnsignedShort>(2)), JS::Attribute::Enumerable);
    object.define_direct_property(vm.well_known_symbol_to_string_tag(), JS::PrimitiveString::create(vm, "SVGTextContentElement"_utf16), JS::Attribute::Configurable);
}

void SVGTextContentElementPrototype::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::SVGTextContentElement*> impl_from(JS::VM& vm, JS::Value js_value)
{

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

[[maybe_unused]] static JS::ThrowCompletionOr<SVG::SVGTextContentElement*> 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(SVGTextContentElementPrototype::get_number_of_chars)
{
    WebIDL::log_trace(vm, "SVGTextContentElementPrototype::get_number_of_chars");
    [[maybe_unused]] auto& realm = *vm.current_realm();
    [[maybe_unused]] SVG::SVGTextContentElement* idl_object = TRY(impl_from(vm));

    [[maybe_unused]] auto R = TRY(throw_dom_exception_if_needed(vm, [&] { return idl_object->get_number_of_chars(); }));
    return JS::Value(static_cast<WebIDL::Long>(R));
}

JS_DEFINE_NATIVE_FUNCTION(SVGTextContentElementPrototype::get_start_position_of_char)
{
    WebIDL::log_trace(vm, "SVGTextContentElementPrototype::get_start_position_of_char");
    [[maybe_unused]] auto& realm = *vm.current_realm();
    [[maybe_unused]] SVG::SVGTextContentElement* idl_object = TRY(impl_from(vm));

    if (vm.argument_count() < 1)
        return vm.throw_completion<JS::TypeError>(JS::ErrorType::BadArgCountOne, "getStartPositionOfChar");

    auto arg0 = vm.argument(0);
    auto charnum = TRY(throw_dom_exception_if_needed(vm, [&] { return WebIDL::convert_to_int<WebIDL::UnsignedLong>(vm, arg0, WebIDL::EnforceRange::No, WebIDL::Clamp::No); }));

    [[maybe_unused]] auto R = TRY(throw_dom_exception_if_needed(vm, [&] { return idl_object->get_start_position_of_char(charnum); }));
    return JS::Value(R);
}

} // namespace Web::Bindings
