/**
 * Copyright (C) 2009 - present by OpenGamma Inc. and the OpenGamma group of companies
 * 
 * Please see distribution for license.
 */
package com.opengamma.financial.currency;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opengamma.core.security.SecurityUtils;
import com.opengamma.id.UniqueId;
import com.opengamma.master.config.ConfigDocument;
import com.opengamma.master.config.ConfigMaster;
import com.opengamma.master.config.ConfigMasterUtils;
import com.opengamma.util.money.Currency;

/**
 * Loads a default currency matrix into the configuration database.
 */
public class CurrencyMatrixConfigPopulator {

  
  
  private static final Logger s_logger = LoggerFactory.getLogger(CurrencyMatrixConfigPopulator.class);
  
  private static final String[] CURRENCIES = new String[] {"EUR", "GBP", "CHF", "AUD", "SEK", "NZD", "CAD", "DKK", "JPY"};

  /**
   * Bloomberg currency matrix config name
   */
  public static final String BLOOMBERG_LIVE_DATA = "BloombergLiveData";
  
  /**
   * Synthetic currency matrix config name
   */
  public static final String SYNTHETIC_LIVE_DATA = "SyntheticLiveData";

  public CurrencyMatrixConfigPopulator(ConfigMaster cfgMaster) {
    populateCurrencyMatrixConfigMaster(cfgMaster);
  }

  public static ConfigMaster populateCurrencyMatrixConfigMaster(ConfigMaster cfgMaster) {
    storeCurrencyMatrix(cfgMaster, BLOOMBERG_LIVE_DATA, createBloombergConversionMatrix());
    storeCurrencyMatrix(cfgMaster, SYNTHETIC_LIVE_DATA, createSyntheticConversionMatrix());
    return cfgMaster;
  }

  private static void storeCurrencyMatrix(final ConfigMaster cfgMaster, final String name, final CurrencyMatrix currencyMatrix) {
    ConfigDocument<CurrencyMatrix> doc = new ConfigDocument<CurrencyMatrix>(CurrencyMatrix.class);
    doc.setName(name);
    doc.setValue(currencyMatrix);
    ConfigMasterUtils.storeByName(cfgMaster, doc);
  }

  public static CurrencyMatrix createBloombergConversionMatrix() {
    final SimpleCurrencyMatrix matrix = new SimpleCurrencyMatrix();
    final Currency commonCross = Currency.USD;
    for (String currency : CURRENCIES) {
      matrix.setLiveData(commonCross, Currency.of(currency), UniqueId.of(SecurityUtils.BLOOMBERG_TICKER.toString(), currency + " Curncy"));
    }
    for (String currency : CURRENCIES) {
      final Currency target = Currency.of(currency);
      for (String currency2 : CURRENCIES) {
        if (!currency.equals(currency2)) {
          matrix.setCrossConversion(Currency.of(currency2), target, commonCross);
        }
      }
    }
    dumpMatrix(matrix);
    return matrix;
  }
  
  public static CurrencyMatrix createSyntheticConversionMatrix() {
    final SimpleCurrencyMatrix matrix = new SimpleCurrencyMatrix();
    final Currency commonCross = Currency.USD;
    for (String currency : CURRENCIES) {
      matrix.setLiveData(commonCross, Currency.of(currency), UniqueId.of(SecurityUtils.OG_SYNTHETIC_TICKER.getName(), commonCross.toString() + currency));
    }

    for (String currency : CURRENCIES) {
      final Currency target = Currency.of(currency);
      for (String currency2 : CURRENCIES) {
        if (!currency.equals(currency2)) {
          matrix.setCrossConversion(Currency.of(currency2), target, commonCross);
        }
      }
    }
    dumpMatrix(matrix);
    return matrix;

  }
  
  public static void dumpMatrix(final CurrencyMatrix matrix) {
    StringBuilder sb = new StringBuilder();
    sb.append('\n');
    for (Currency x : matrix.getTargetCurrencies()) {
      sb.append('\t').append(x.getCode());
    }
    for (Currency y : matrix.getSourceCurrencies()) {
      sb.append('\n').append(y.getCode());
      for (Currency x : matrix.getTargetCurrencies()) {
        sb.append('\t').append(matrix.getConversion(y, x));
      }
    }
    s_logger.debug("Currency matrix = {}", sb);
  }
  
}

@@ -640,14 +640,8 @@
  %7B%0A%0A
-  %0A  %0A
   pr
@@ -774,16 +774,33 @@
 tring%5B%5D 
+DOLLARS_PER_UNIT_
 CURRENCI
@@ -837,32 +837,117 @@
 %22, %22
-CHF
+AUD
 %22, %22
-AUD
+NZD%22 %7D;%0A  private static final String%5B%5D UNITS_PER_DOLLAR_CURRENCIES = new String%5B%5D %7B%22JPY
 %22, %22
-SEK
+CHF
 %22, %22
-NZD
+SEK
 %22, %22
@@ -961,17 +961,367 @@
 DKK%22
-, %22JPY%22%7D;
+ %7D;%0A  private static final String%5B%5D CURRENCIES = combine(DOLLARS_PER_UNIT_CURRENCIES, UNITS_PER_DOLLAR_CURRENCIES);%0A  %0A  private static String%5B%5D combine(final String%5B%5D a, final String%5B%5D b) %7B%0A    final String%5B%5D x = new String%5Ba.length + b.length%5D;%0A    System.arraycopy(a, 0, x, 0, a.length);%0A    System.arraycopy(b, 0, x, a.length, b.length);%0A    return x;%0A  %7D
 %0A%0A  
@@ -2541,32 +2541,49 @@
 ring currency : 
+DOLLARS_PER_UNIT_
 CURRENCIES) %7B%0A  
@@ -2718,32 +2718,236 @@
 urncy%22));%0A    %7D%0A
+    for (String currency : UNITS_PER_DOLLAR_CURRENCIES) %7B%0A      matrix.setLiveData(Currency.of(currency), commonCross, UniqueId.of(SecurityUtils.BLOOMBERG_TICKER.toString(), currency + %22 Curncy%22));%0A    %7D%0A
     for (String 
@@ -3468,32 +3468,49 @@
 ring currency : 
+DOLLARS_PER_UNIT_
 CURRENCIES) %7B%0A  
@@ -3668,16 +3668,234 @@
 ;%0A    %7D%0A
+    for (String currency : UNITS_PER_DOLLAR_CURRENCIES) %7B%0A      matrix.setLiveData(Currency.of(currency), commonCross, UniqueId.of(SecurityUtils.OG_SYNTHETIC_TICKER.getName(), commonCross.toString() + currency));%0A    %7D
 %0A    for

Correct default currency conversion matrix.