#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/OESVertexArrayObject.h>
#include <LibWeb/WebGL/Extensions/OESVertexArrayObject.h>
#include <LibWeb/WebGL/Extensions/WebGLVertexArrayObjectOES.h>
#include <LibWeb/WebIDL/Tracing.h>
#include <LibWeb/WebIDL/Types.h>

namespace Web::Bindings {

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

    
    object.define_direct_property(vm.names.length, JS::Value(0), JS::Attribute::Configurable);
    object.define_direct_property(vm.names.name, JS::PrimitiveString::create(vm, "OESVertexArrayObject"_utf16), JS::Attribute::Configurable);
    object.define_direct_property(vm.names.prototype, &ensure_web_prototype<OESVertexArrayObjectPrototype>(realm, "OESVertexArrayObject"_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("VERTEX_ARRAY_BINDING_OES"_utf16_fly_string, JS::Value(static_cast<WebIDL::UnsignedLong>(0x85B5)), JS::Attribute::Enumerable);
}

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

void OESVertexArrayObjectPrototype::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(realm.intrinsics().object_prototype());
    object.define_native_function(realm, "createVertexArrayOES"_utf16_fly_string, create_vertex_array_oes, 0, default_attributes);

    object.define_native_function(realm, "deleteVertexArrayOES"_utf16_fly_string, delete_vertex_array_oes, 1, default_attributes);

    object.define_native_function(realm, "isVertexArrayOES"_utf16_fly_string, is_vertex_array_oes, 1, default_attributes);

    object.define_native_function(realm, "bindVertexArrayOES"_utf16_fly_string, bind_vertex_array_oes, 1, default_attributes);


    // 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("VERTEX_ARRAY_BINDING_OES"_utf16_fly_string, JS::Value(static_cast<WebIDL::UnsignedLong>(0x85B5)), JS::Attribute::Enumerable);
    object.define_direct_property(vm.well_known_symbol_to_string_tag(), JS::PrimitiveString::create(vm, "OESVertexArrayObject"_utf16), JS::Attribute::Configurable);
}

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

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

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

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

JS_DEFINE_NATIVE_FUNCTION(OESVertexArrayObjectPrototype::delete_vertex_array_oes)
{
    WebIDL::log_trace(vm, "OESVertexArrayObjectPrototype::delete_vertex_array_oes");
    [[maybe_unused]] auto& realm = *vm.current_realm();
    [[maybe_unused]] WebGL::OESVertexArrayObject* idl_object = TRY(impl_from(vm));

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

    auto arg0 = vm.argument(0);
    auto array_object = TRY(throw_dom_exception_if_needed(vm, [&] { return [&]() -> JS::ThrowCompletionOr<GC::Ptr<WebGL::WebGLVertexArrayObjectOES>> {
        GC::Ptr<WebGL::WebGLVertexArrayObjectOES> value;

        if (!arg0.is_nullish()) {

            value = TRY([&]() -> JS::ThrowCompletionOr<GC::Ref<WebGL::WebGLVertexArrayObjectOES>> {
        if (auto impl = arg0.as_if<WebGL::WebGLVertexArrayObjectOES>())
            return *impl;
        return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "WebGLVertexArrayObjectOES");
    }());
        }
        return value;
    }(); }));

    [[maybe_unused]] auto R = TRY(throw_dom_exception_if_needed(vm, [&] { return idl_object->delete_vertex_array_oes(array_object); }));
    return JS::js_undefined();
}

JS_DEFINE_NATIVE_FUNCTION(OESVertexArrayObjectPrototype::is_vertex_array_oes)
{
    WebIDL::log_trace(vm, "OESVertexArrayObjectPrototype::is_vertex_array_oes");
    [[maybe_unused]] auto& realm = *vm.current_realm();
    [[maybe_unused]] WebGL::OESVertexArrayObject* idl_object = TRY(impl_from(vm));

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

    auto arg0 = vm.argument(0);
    auto array_object = TRY(throw_dom_exception_if_needed(vm, [&] { return [&]() -> JS::ThrowCompletionOr<GC::Ptr<WebGL::WebGLVertexArrayObjectOES>> {
        GC::Ptr<WebGL::WebGLVertexArrayObjectOES> value;

        if (!arg0.is_nullish()) {

            value = TRY([&]() -> JS::ThrowCompletionOr<GC::Ref<WebGL::WebGLVertexArrayObjectOES>> {
        if (auto impl = arg0.as_if<WebGL::WebGLVertexArrayObjectOES>())
            return *impl;
        return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "WebGLVertexArrayObjectOES");
    }());
        }
        return value;
    }(); }));

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

JS_DEFINE_NATIVE_FUNCTION(OESVertexArrayObjectPrototype::bind_vertex_array_oes)
{
    WebIDL::log_trace(vm, "OESVertexArrayObjectPrototype::bind_vertex_array_oes");
    [[maybe_unused]] auto& realm = *vm.current_realm();
    [[maybe_unused]] WebGL::OESVertexArrayObject* idl_object = TRY(impl_from(vm));

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

    auto arg0 = vm.argument(0);
    auto array_object = TRY(throw_dom_exception_if_needed(vm, [&] { return [&]() -> JS::ThrowCompletionOr<GC::Ptr<WebGL::WebGLVertexArrayObjectOES>> {
        GC::Ptr<WebGL::WebGLVertexArrayObjectOES> value;

        if (!arg0.is_nullish()) {

            value = TRY([&]() -> JS::ThrowCompletionOr<GC::Ref<WebGL::WebGLVertexArrayObjectOES>> {
        if (auto impl = arg0.as_if<WebGL::WebGLVertexArrayObjectOES>())
            return *impl;
        return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "WebGLVertexArrayObjectOES");
    }());
        }
        return value;
    }(); }));

    [[maybe_unused]] auto R = TRY(throw_dom_exception_if_needed(vm, [&] { return idl_object->bind_vertex_array_oes(array_object); }));
    return JS::js_undefined();
}

} // namespace Web::Bindings
