/*
 Source File : ObjectByteReaderWithPosition.h
 
 
 Copyright 2013 Gal Kahana HummusJS
 
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
 
 http://www.apache.org/licenses/LICENSE-2.0
 
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 
 */
#include "ObjectByteReaderWithPosition.h"

using namespace v8;

ObjectByteReaderWithPosition::ObjectByteReaderWithPosition(Local<Object> inObject)
{
	CREATE_ISOLATE_CONTEXT;

	SET_PERSISTENT_OBJECT(mObject, Object, inObject);
}

ObjectByteReaderWithPosition::~ObjectByteReaderWithPosition()
{
	DISPOSE_PERSISTENT(mObject);
}

IOBasicTypes::LongBufferSizeType ObjectByteReaderWithPosition::Read(IOBasicTypes::Byte* inBuffer,IOBasicTypes::LongBufferSizeType inBufferSize)
{
	CREATE_ISOLATE_CONTEXT;
	CREATE_ESCAPABLE_SCOPE;

	Local<Value> value = OBJECT_FROM_PERSISTENT(mObject)->Get(GET_CURRENT_CONTEXT, NEW_STRING("read")).ToLocalChecked();
    if(value->IsUndefined())
        return 0;
    Local<Function> func = Local<Function>::Cast(value);
    
    Local<Value> args[1];
    args[0] = NEW_NUMBER(inBufferSize);
    
	Local<Value> result = func->Call(GET_CURRENT_CONTEXT, OBJECT_FROM_PERSISTENT(mObject), 1, args).ToLocalChecked();
    
    if(!result->IsArray())
        return 0;
    
    IOBasicTypes::LongBufferSizeType bufferLength = result->TO_OBJECT()->Get(GET_CURRENT_CONTEXT, NEW_STRING("length")).ToLocalChecked()->TO_UINT32Value();
    for(IOBasicTypes::LongBufferSizeType i=0;i < bufferLength;++i)
        inBuffer[i] = (IOBasicTypes::Byte)(TO_UINT32(result->TO_OBJECT()->Get(GET_CURRENT_CONTEXT, (uint32_t)i).ToLocalChecked())->Value());
    
    return bufferLength;
    
}

bool ObjectByteReaderWithPosition::NotEnded()
{
	CREATE_ISOLATE_CONTEXT;
	CREATE_ESCAPABLE_SCOPE;

	Local<Value> value = OBJECT_FROM_PERSISTENT(mObject)->Get(GET_CURRENT_CONTEXT, NEW_STRING("notEnded")).ToLocalChecked();
    if(value->IsUndefined())
        return true;
    Local<Function> func = Local<Function>::Cast(value);
    
	return (func->Call(GET_CURRENT_CONTEXT, OBJECT_FROM_PERSISTENT(mObject), 0, NULL).ToLocalChecked()->TO_BOOLEAN()->Value());
}

void ObjectByteReaderWithPosition::SetPosition(LongFilePositionType inOffsetFromStart)
{
	CREATE_ISOLATE_CONTEXT;
	CREATE_ESCAPABLE_SCOPE;

	Local<Value> value = OBJECT_FROM_PERSISTENT(mObject)->Get(GET_CURRENT_CONTEXT, NEW_STRING("setPosition")).ToLocalChecked();
    if(value->IsUndefined())
        return;
    Local<Function> func = Local<Function>::Cast(value);
    
    Local<Value> args[1];
    args[0] = NEW_NUMBER(inOffsetFromStart);
	func->Call(GET_CURRENT_CONTEXT, OBJECT_FROM_PERSISTENT(mObject), 1, args).ToLocalChecked();
}

void ObjectByteReaderWithPosition::SetPositionFromEnd(LongFilePositionType inOffsetFromStart)
{
	CREATE_ISOLATE_CONTEXT;
	CREATE_ESCAPABLE_SCOPE;

	Local<Value> value = OBJECT_FROM_PERSISTENT(mObject)->Get(GET_CURRENT_CONTEXT, NEW_STRING("setPositionFromEnd")).ToLocalChecked();
    if(value->IsUndefined())
        return;
    Local<Function> func = Local<Function>::Cast(value);
    
    Local<Value> args[1];
    args[0] = NEW_NUMBER(inOffsetFromStart);
	func->Call(GET_CURRENT_CONTEXT, OBJECT_FROM_PERSISTENT(mObject), 1, args).ToLocalChecked();
}

LongFilePositionType ObjectByteReaderWithPosition::GetCurrentPosition()
{
	CREATE_ISOLATE_CONTEXT;
	CREATE_ESCAPABLE_SCOPE;

	Local<Value> value = OBJECT_FROM_PERSISTENT(mObject)->Get(GET_CURRENT_CONTEXT, NEW_STRING("getCurrentPosition")).ToLocalChecked();
    if(value->IsUndefined())
        return true;
    Local<Function> func = Local<Function>::Cast(value);
    
	return TO_NUMBER(func->Call(GET_CURRENT_CONTEXT,  OBJECT_FROM_PERSISTENT(mObject), 0, NULL).ToLocalChecked())->Value();
}

void ObjectByteReaderWithPosition::Skip(LongBufferSizeType inSkipSize)
{
	CREATE_ISOLATE_CONTEXT;
	CREATE_ESCAPABLE_SCOPE;

	Local<Value> value = OBJECT_FROM_PERSISTENT(mObject)->Get(GET_CURRENT_CONTEXT, NEW_STRING("skip")).ToLocalChecked();
    if(value->IsUndefined())
        return;
    Local<Function> func = Local<Function>::Cast(value);
    
    Local<Value> args[1];
    args[0] = NEW_NUMBER(inSkipSize);
	func->Call(GET_CURRENT_CONTEXT, OBJECT_FROM_PERSISTENT(mObject), 1, args).ToLocalChecked();
}

void ObjectByteReaderWithPosition::MoveStartPosition(LongFilePositionType inStartPosition)
{
	CREATE_ISOLATE_CONTEXT;
	CREATE_ESCAPABLE_SCOPE;

	Local<Value> value = OBJECT_FROM_PERSISTENT(mObject)->Get(GET_CURRENT_CONTEXT, NEW_STRING("moveStartPosition")).ToLocalChecked();
		if(value->IsUndefined())
				return;
		Local<Function> func = Local<Function>::Cast(value);

		Local<Value> args[1];
		args[0] = NEW_NUMBER(inStartPosition);
	func->Call(GET_CURRENT_CONTEXT, OBJECT_FROM_PERSISTENT(mObject), 1, args).ToLocalChecked();
}
