/*
 * Copyright (C) 2015 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"
#if !HAVE(BINDINGS_ALL_IN_ONE)
#include "JSMediaStreamTrack.h"

#if ENABLE(MEDIA_STREAM)

#include "ExceptionCode.h"
#include "JSDOMBinding.h"
#include "MediaSourceSettings.h"
#include <runtime/JSObject.h>
#include <runtime/ObjectConstructor.h>

using namespace JSC;

namespace WebCore {

JSC::JSValue JSMediaStreamTrack::getSettings(ExecState* state)
{
    JSGlobalObject* globalObject = state->lexicalGlobalObject();
    VM& vm = globalObject->vm();

    JSValue thisValue = state->thisValue();
    JSMediaStreamTrack* castedThis = jsDynamicCast<JSMediaStreamTrack*>(thisValue);
    if (UNLIKELY(!castedThis))
        return JSValue::decode(throwThisTypeError(*state, "MediaStreamTrack", "getSettings"));

    JSObject* object = constructEmptyObject(globalObject);
    auto& impl = castedThis->wrapped();
    RefPtr<MediaSourceSettings> settings = WTF::getPtr(impl.getSettings());

    if (settings->supportsWidth())
        object->putDirect(vm, Identifier::fromString(vm, "width"), jsNumber(settings->width()), DontDelete | ReadOnly);
    if (settings->supportsHeight())
        object->putDirect(vm, Identifier::fromString(vm, "height"), jsNumber(settings->height()), DontDelete | ReadOnly);
    if (settings->supportsAspectRatio())
        object->putDirect(vm, Identifier::fromString(vm, "aspectRatio"), jsDoubleNumber(settings->aspectRatio()), DontDelete | ReadOnly);
    if (settings->supportsFrameRate())
        object->putDirect(vm, Identifier::fromString(vm, "frameRate"), jsDoubleNumber(settings->frameRate()), DontDelete | ReadOnly);
    if (settings->supportsFacingMode())
        object->putDirect(vm, Identifier::fromString(vm, "facingMode"), jsStringWithCache(vm, settings->facingMode()), DontDelete | ReadOnly);
    if (settings->supportsVolume())
        object->putDirect(vm, Identifier::fromString(vm, "volume"), jsDoubleNumber(settings->volume()), DontDelete | ReadOnly);
    if (settings->supportsSampleRate())
        object->putDirect(vm, Identifier::fromString(vm, "sampleRate"), jsNumber(settings->sampleRate()), DontDelete | ReadOnly);
    if (settings->supportsSampleSize())
        object->putDirect(vm, Identifier::fromString(vm, "sampleSize"), jsNumber(settings->sampleSize()), DontDelete | ReadOnly);
    if (settings->supportsEchoCancellation())
        object->putDirect(vm, Identifier::fromString(vm, "echoCancellation"), jsBoolean(settings->echoCancellation()), DontDelete | ReadOnly);
    if (settings->supportsDeviceId())
        object->putDirect(vm, Identifier::fromString(vm, "deviceId"), jsStringWithCache(vm, settings->deviceId()), DontDelete | ReadOnly);
    if (settings->supportsGroupId())
        object->putDirect(vm, Identifier::fromString(vm, "groupId"), jsStringWithCache(vm, settings->groupId()), DontDelete | ReadOnly);
    
    return object;
}

} // namespace WebCore

#endif
#endif
