/*******************************************************************************
 * Copyright (c) 2004 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Actuate Corporation  - initial API and implementation
 *******************************************************************************/

package org.eclipse.birt.core.script;

import java.util.logging.Logger;

import org.mozilla.javascript.Context;
import org.mozilla.javascript.ScriptableObject;

/**
 * 
 * NativeFinance.
 * 
 * This class implements the Finance native object. See ECMA 15.8.
 *  
 */
public class NativeFinance extends ScriptableObject
{

	/**
	 * 
	 */
	private static final long serialVersionUID = -5653141543095782014L;
	/**
	 * logger used to log syntax errors.
	 */
	static protected Logger logger = Logger.getLogger( NativeFinance.class
			.getName( ) );

	/**
	 * The zero-argument constructor used by Rhino runtime to create instances
	 */
	public NativeFinance( )
	{
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.mozilla.javascript.Scriptable#getClassName()
	 */
	public String getClassName( )
	{
		return "Finance";
	}

	/**
	 * convert java script object to double
	 * 
	 * @param value
	 *            js object
	 * @return double value
	 */
	protected static double toDouble( Object value )
	{
		return Context.toNumber( value );
	}

	/**
	 * convert jsObject to boolean
	 * 
	 * @param value
	 *            js object
	 * @return boolean value
	 */
	protected static boolean toBoolean( Object value )
	{
		return Context.toBoolean( value );
	}

	/**
	 * convert java script object to int
	 * 
	 * @param value
	 *            js object
	 * @return int value
	 */
	protected static int toInt( Object value )
	{
		return (int) Context.toNumber( value );
	}

	/**
	 * convert js object to double[]
	 * 
	 * @param value
	 *            js object
	 * @return double[]
	 */
	protected static double[] toDoubleArray( Object value )
	{
		return (double[]) Context.toType( value, double[].class );
	}

	protected static Object wrap_double( double x )
	{
		return new Double( x );
	}

	public static double jsStaticFunction_ddb( double cost, double salvage,
			double life, int period )
	{
		return Finance.ddb( cost, salvage, life, period );
	}

	public static double jsStaticFunction_fv( double rage, int nPer,
			double pmt, double pv, int due )
	{
		return Finance.fv( rage, nPer, pmt, pv, due );
	}

	public static double jsStaticFunction_ipmt

	( double rate, int per, int nPer, double pv, double fv, int due )
	{
		return Finance.ipmt( rate, per, nPer, pv, fv, due );
	}

	public static double jsStaticFunction_irr( Object cashObj, double intrate )
	{
		double[] cash = toDoubleArray( cashObj );
		return Finance.irr( cash, intrate );
	}

	public static double jsStaticFunction_mirr( Object arptrObj, double frate,
			double rrate )
	{
		double[] arptr = toDoubleArray( arptrObj );
		return Finance.mirr( arptr, frate, rrate );
	}

	public static double jsStaticFunction_nper( double rate, double pmt,
			double pv, double fv, int due )
	{
		return Finance.nper( rate, pmt, pv, fv, due );
	}

	public static double jsStaticFunction_npv( double rate, Object arptrObj )
	{
		double[] arptr = toDoubleArray( arptrObj );

		return Finance.npv( rate, arptr );

	}

	public static double jsStaticFunction_percent( double denom, double num,
			double valueIfZero )
	{
		if (Double.isNaN(valueIfZero))
		{
			valueIfZero = 0;
		}
		return Finance.percent( denom, num, valueIfZero );
	}

	public static double jsStaticFunction_pmt( double rate, int nper,
			double pv, double fv, int due )
	{
		return Finance.pmt( rate, nper, pv, fv, due );
	}

	public static double jsStaticFunction_ppmt( double rate, int per, int nPer,
			double pv, double fv, int due )
	{
		return Finance.ppmt( rate, per, nPer, pv, fv, due );
	}

	public static double jsStaticFunction_pv( double rate, int nPer,
			double pmt, double fv, int due )
	{
		return Finance.pv( rate, nPer, pmt, fv, due );
	}

	public static double jsStaticFunction_rate( int nPer, double pmt,
			double pv, double fv, int due, double guess )
	{
		return Finance.rate( nPer, pmt, pv, fv, due, guess );
	}

	public static double jsStaticFunction_sln( double cost, double salvage,
			double life )
	{
		return Finance.sln( cost, salvage, life );
	}

	public static double jsStaticFunction_syd( double cost, double salvage,
			double life, int period )
	{
		return Finance.syd( cost, salvage, life, period );
	}
}

