/**
 * Copyright (C) 2011 - present by OpenGamma Inc. and the OpenGamma group of companies
 * 
 * Please see distribution for license.
 */
package com.opengamma.financial.analytics.model.forex.option.black;

import java.text.DecimalFormat;
import java.util.Collections;
import java.util.Set;

import com.opengamma.OpenGammaRuntimeException;
import com.opengamma.analytics.financial.forex.calculator.PresentValueBlackVolatilityNodeSensitivityBlackForexCalculator;
import com.opengamma.analytics.financial.forex.method.PresentValueForexBlackVolatilityNodeSensitivityDataBundle;
import com.opengamma.analytics.financial.interestrate.InstrumentDerivative;
import com.opengamma.analytics.financial.model.option.definition.ForexOptionDataBundle;
import com.opengamma.analytics.financial.model.option.definition.SmileDeltaTermStructureDataBundle;
import com.opengamma.engine.ComputationTarget;
import com.opengamma.engine.function.FunctionExecutionContext;
import com.opengamma.engine.function.FunctionInputs;
import com.opengamma.engine.value.ComputedValue;
import com.opengamma.engine.value.ValueRequirement;
import com.opengamma.engine.value.ValueRequirementNames;
import com.opengamma.engine.value.ValueSpecification;
import com.opengamma.financial.analytics.DoubleLabelledMatrix2D;

/**
 * 
 */
public class FXOptionBlackVegaMatrixFunction extends FXOptionBlackSingleValuedFunction {
  private static final PresentValueBlackVolatilityNodeSensitivityBlackForexCalculator CALCULATOR = PresentValueBlackVolatilityNodeSensitivityBlackForexCalculator.getInstance();
  private static final DecimalFormat DELTA_FORMATTER = new DecimalFormat("##");

  public FXOptionBlackVegaMatrixFunction() {
    super(ValueRequirementNames.VEGA_MATRIX);
  }

  @Override
  protected Set<ComputedValue> getResult(final InstrumentDerivative forex, final ForexOptionDataBundle<?> data, final ComputationTarget target,
      final Set<ValueRequirement> desiredValues, final FunctionInputs inputs, final ValueSpecification spec, final FunctionExecutionContext executionContext) {
    if (data instanceof SmileDeltaTermStructureDataBundle) {
      final PresentValueForexBlackVolatilityNodeSensitivityDataBundle result = forex.accept(CALCULATOR, (SmileDeltaTermStructureDataBundle) data);
      final double[] expiries = result.getExpiries().getData();
      final double[] delta = result.getDelta().getData();
      final double[][] vega = result.getVega().getData();
      final int nDelta = delta.length;
      final int nExpiries = expiries.length;
      final Double[] rowValues = new Double[nExpiries];
      final String[] rowLabels = new String[nExpiries];
      final Double[] columnValues = new Double[nDelta];
      final String[] columnLabels = new String[nDelta];
      final double[][] values = new double[nDelta][nExpiries];
      for (int i = 0; i < nDelta; i++) {
        columnValues[i] = delta[i];
        columnLabels[i] = "P" + DELTA_FORMATTER.format(delta[i] * 100) + " " + result.getCurrencyPair().getFirst() + "/" + result.getCurrencyPair().getSecond();
        for (int j = 0; j < nExpiries; j++) {
          if (i == 0) {
            rowValues[j] = expiries[j];
            rowLabels[j] = getFormattedExpiry(expiries[j]);
          }
          values[i][j] = vega[j][i];
        }
      }
      return Collections.singleton(new ComputedValue(spec, new DoubleLabelledMatrix2D(rowValues, rowLabels, columnValues, columnLabels, values)));
    }
    throw new OpenGammaRuntimeException("Can only calculate vega matrix for surfaces with smiles");
  }

  private static String getFormattedExpiry(final double expiry) {
    if (expiry < 1. / 54) {
      final int days = (int) Math.ceil((365 * expiry));
      return days + "D";
    }
    if (expiry < 1. / 13) {
      final int weeks = (int) Math.ceil((52 * expiry));
      return weeks + "W";
    }
    if (expiry < 0.95) {
      final int months = (int) Math.ceil((12 * expiry));
      return months + "M";
    }
    return ((int) Math.ceil(expiry)) + "Y";
  }
}

@@ -86,18 +86,17 @@
 anies%0A *
- 
 %0A
+
  * Pleas
@@ -1275,16 +1275,81 @@
 x2D;
+%0Aimport com.opengamma.financial.analytics.model.VegaMatrixHelper;
 %0A%0A/**%0A *
  %0A *
@@ -1344,17 +1344,16 @@
 %0A%0A/**%0A *
- 
 %0A */%0Apub
@@ -3242,19 +3242,48 @@
 ls%5Bj%5D = 
-get
+VegaMatrixHelper.getFXVolatility
 Formatte
@@ -3633,466 +3633,6 @@
  %7D%0A%0A
-  private static String getFormattedExpiry(final double expiry) %7B%0A    if (expiry %3C 1. / 54) %7B%0A      final int days = (int) Math.ceil((365 * expiry));%0A      return days + %22D%22;%0A    %7D%0A    if (expiry %3C 1. / 13) %7B%0A      final int weeks = (int) Math.ceil((52 * expiry));%0A      return weeks + %22W%22;%0A    %7D%0A    if (expiry %3C 0.95) %7B%0A      final int months = (int) Math.ceil((12 * expiry));%0A      return months + %22M%22;%0A    %7D%0A    return ((int) Math.ceil(expiry)) + %22Y%22;%0A  %7D%0A
 %7D%0A

Factoring out common code