#include <LibGC/Heap.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/FunctionObject.h>
#include <LibJS/Runtime/Value.h>
#include <LibJS/Runtime/ValueInlines.h>
#include <LibWeb/Bindings/DataTransferItem.h>
#include <LibWeb/Bindings/ExceptionOrUtils.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/EntriesAPI/FileSystemEntry.h>
#include <LibWeb/FileAPI/File.h>
#include <LibWeb/HTML/DataTransferItem.h>
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/WebIDL/AbstractOperations.h>
#include <LibWeb/WebIDL/CallbackType.h>
#include <LibWeb/WebIDL/Tracing.h>

namespace Web::Bindings {

void DataTransferItemConstructor::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, "DataTransferItem"_utf16), JS::Attribute::Configurable);
    object.define_direct_property(vm.names.prototype, &ensure_web_prototype<DataTransferItemPrototype>(realm, "DataTransferItem"_fly_string), 0);
}

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

void DataTransferItemPrototype::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());

    auto kind_id = "kind"_utf16_fly_string;
    auto native_kind_getter = JS::NativeFunction::create(realm, kind_getter, 0, kind_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_kind_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto kind_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(kind_id, native_kind_getter, native_kind_setter, kind_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 type_id = "type"_utf16_fly_string;
    auto native_type_getter = JS::NativeFunction::create(realm, type_getter, 0, type_id, &realm, "get"sv);
    GC::Ptr<JS::NativeFunction> native_type_setter;

    // 4. Let configurable be false if attr is unforgeable and true otherwise.
    auto type_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(type_id, native_type_getter, native_type_setter, type_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_native_function(realm, "getAsString"_utf16_fly_string, get_as_string, 1, default_attributes);

    object.define_native_function(realm, "getAsFile"_utf16_fly_string, get_as_file, 0, default_attributes);

    object.define_native_function(realm, "webkitGetAsEntry"_utf16_fly_string, webkit_get_as_entry, 0, default_attributes);

    object.define_direct_property(vm.well_known_symbol_to_string_tag(), JS::PrimitiveString::create(vm, "DataTransferItem"_utf16), JS::Attribute::Configurable);
}

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

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

[[maybe_unused]] static JS::ThrowCompletionOr<HTML::DataTransferItem*> 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(DataTransferItemPrototype::kind_getter)
{
    WebIDL::log_trace(vm, "DataTransferItemPrototype::kind_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->kind(); }));

    return WebIDL::primitive_string_from_string(vm, R);
}

JS_DEFINE_NATIVE_FUNCTION(DataTransferItemPrototype::type_getter)
{
    WebIDL::log_trace(vm, "DataTransferItemPrototype::type_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->type(); }));

    return WebIDL::primitive_string_from_string(vm, R);
}

JS_DEFINE_NATIVE_FUNCTION(DataTransferItemPrototype::get_as_string)
{
    WebIDL::log_trace(vm, "DataTransferItemPrototype::get_as_string");
    [[maybe_unused]] auto& realm = *vm.current_realm();
    [[maybe_unused]] HTML::DataTransferItem* idl_object = TRY(impl_from(vm));

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

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

        if (!arg0.is_nullish()) {

            value = TRY([&]() -> JS::ThrowCompletionOr<GC::Ref<WebIDL::CallbackType>> {

        if (!arg0.is_function())
            return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAFunction, arg0);

        return vm.heap().allocate<WebIDL::CallbackType>(arg0.as_object(),  HTML::incumbent_realm(), WebIDL::OperationReturnsPromise::No);
    }());
        }
        return value;
    }(); }));

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

JS_DEFINE_NATIVE_FUNCTION(DataTransferItemPrototype::get_as_file)
{
    WebIDL::log_trace(vm, "DataTransferItemPrototype::get_as_file");
    [[maybe_unused]] auto& realm = *vm.current_realm();
    [[maybe_unused]] HTML::DataTransferItem* idl_object = TRY(impl_from(vm));

    [[maybe_unused]] auto R = TRY(throw_dom_exception_if_needed(vm, [&] { return idl_object->get_as_file(); }));
    return [&]() -> JS::Value {
        // 1. If the IDL nullable type T? value is null, then the JavaScript value is null.
        if (!R)
            return JS::js_null();

        // 2. Otherwise, the JavaScript value is the result of converting the IDL nullable type value to the inner IDL type T.
        return JS::Value(JS::Value(R));
    }();
}

JS_DEFINE_NATIVE_FUNCTION(DataTransferItemPrototype::webkit_get_as_entry)
{
    WebIDL::log_trace(vm, "DataTransferItemPrototype::webkit_get_as_entry");
    [[maybe_unused]] auto& realm = *vm.current_realm();
    [[maybe_unused]] HTML::DataTransferItem* idl_object = TRY(impl_from(vm));

    [[maybe_unused]] auto R = TRY(throw_dom_exception_if_needed(vm, [&] { return idl_object->webkit_get_as_entry(); }));
    return [&]() -> JS::Value {
        // 1. If the IDL nullable type T? value is null, then the JavaScript value is null.
        if (!R)
            return JS::js_null();

        // 2. Otherwise, the JavaScript value is the result of converting the IDL nullable type value to the inner IDL type T.
        return JS::Value(JS::Value(R));
    }();
}

} // namespace Web::Bindings
