/**
 * Copyright (C) 2009 - 2010 by OpenGamma Inc.
 *
 * Please see distribution for license.
 */
package com.opengamma.financial.analytics.ircurve;

import java.util.Map;

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

import com.opengamma.config.ConfigDocument;
import com.opengamma.config.ConfigMaster;
import com.opengamma.config.ConfigTypeMaster;
import com.opengamma.core.common.Currency;

/**
 * Populates the yield curve configuration.
 */
public class YieldCurveConfigPopulator {
  private static final Logger s_logger = LoggerFactory.getLogger(YieldCurveConfigPopulator.class);

  public YieldCurveConfigPopulator(ConfigMaster cfgMaster) {
    populateCurveConfigMaster(cfgMaster);
  }
  
  public static ConfigMaster populateCurveConfigMaster(ConfigMaster cfgMaster) {
    ConfigTypeMaster<YieldCurveDefinition> curveDefinitionMaster = cfgMaster.typed(YieldCurveDefinition.class);
    populateCurveDefinitionConfigMaster(curveDefinitionMaster);
    ConfigTypeMaster<CurveSpecificationBuilderConfiguration> curveSpecificationBuilderConfigMaster = cfgMaster.typed(CurveSpecificationBuilderConfiguration.class);
    populateCurveSpecificationBuilderConfigMaster(curveSpecificationBuilderConfigMaster);
    return cfgMaster;
  }
  
  private static void dumpDefinition(YieldCurveDefinition curveDefinition) {
    s_logger.warn("Curve Definition");
    s_logger.warn("  Name:" + curveDefinition.getName());
    s_logger.warn("  Currency:" + curveDefinition.getCurrency());
    s_logger.warn("  Strips:");
    for (FixedIncomeStrip strip : curveDefinition.getStrips()) {
      s_logger.warn("    " + strip);
    }
  }

  public static void populateCurveDefinitionConfigMaster(ConfigTypeMaster<YieldCurveDefinition> configRepo) {
    Map<String, Map<Currency, YieldCurveDefinition>> standardCurveDefinitions = CurveDefinitionAndSpecifications.buildStandardCurveDefintions();
    for (Map.Entry<String, Map<Currency, YieldCurveDefinition>> entry : standardCurveDefinitions.entrySet()) {
      String curveName = entry.getKey();
      Map<Currency, YieldCurveDefinition> definitions = entry.getValue();
      for (Map.Entry<Currency, YieldCurveDefinition> currencyEntry : definitions.entrySet()) {
        Currency ccy = currencyEntry.getKey();
        YieldCurveDefinition definition = currencyEntry.getValue();
        ConfigDocument<YieldCurveDefinition> document = new ConfigDocument<YieldCurveDefinition>();
        document.setName(curveName + "_" + ccy.getISOCode());
        document.setValue(definition);
        configRepo.add(document);
        dumpDefinition(definition);
      }
    }
//    ConfigDocument<YieldCurveDefinition> forwardUSD = new ConfigDocument<YieldCurveDefinition>();
//    forwardUSD.setName("FORWARD_USD");
//    forwardUSD.setValue(CurveDefinitionAndSpecifications.buildUSDForwardCurveDefinition());
//    configRepo.add(forwardUSD);
//    ConfigDocument<YieldCurveDefinition> fundingUSD = new ConfigDocument<YieldCurveDefinition>();
//    fundingUSD.setName("FUNDING_USD");
//    fundingUSD.setValue(CurveDefinitionAndSpecifications.buildUSDFundingCurveDefinition());
//    configRepo.add(fundingUSD);
    ConfigDocument<YieldCurveDefinition> swapOnlyUSD = new ConfigDocument<YieldCurveDefinition>();
    swapOnlyUSD.setName("SWAP_ONLY_USD");
    swapOnlyUSD.setValue(CurveDefinitionAndSpecifications.buildUSDSwapOnlyCurveDefinition());
    configRepo.add(swapOnlyUSD);
    ConfigDocument<YieldCurveDefinition> swapOnlyNo3YrUSD = new ConfigDocument<YieldCurveDefinition>();
    swapOnlyNo3YrUSD.setName("SWAP_ONLY_NO3YR_USD");
    swapOnlyNo3YrUSD.setValue(CurveDefinitionAndSpecifications.buildUSDSwapOnlyNo3YrCurveDefinition());
    configRepo.add(swapOnlyNo3YrUSD);
    ConfigDocument<YieldCurveDefinition> swapOnly3YrUSD = new ConfigDocument<YieldCurveDefinition>();
    swapOnly3YrUSD.setName("SWAP_ONLY_3YR_USD");
    swapOnly3YrUSD.setValue(CurveDefinitionAndSpecifications.buildUSDSwapOnly3YrCurveDefinition());
    configRepo.add(swapOnly3YrUSD);
    ConfigDocument<YieldCurveDefinition> singleUSD = new ConfigDocument<YieldCurveDefinition>();
    singleUSD.setName("SINGLE_USD");
    singleUSD.setValue(CurveDefinitionAndSpecifications.buildUSDSingleCurveDefinition());
    configRepo.add(singleUSD);
  }

  public static void populateCurveSpecificationBuilderConfigMaster(ConfigTypeMaster<CurveSpecificationBuilderConfiguration> configMaster) {
    Map<Currency, CurveSpecificationBuilderConfiguration> configurations = CurveDefinitionAndSpecifications.buildStandardCurveSpecificationBuilderConfigurations();
    for (Map.Entry<Currency, CurveSpecificationBuilderConfiguration> entry : configurations.entrySet()) {
      ConfigDocument<CurveSpecificationBuilderConfiguration> doc = new ConfigDocument<CurveSpecificationBuilderConfiguration>();
      doc.setName("DEFAULT_"+entry.getKey().getISOCode());
      doc.setValue(entry.getValue());
      configMaster.add(doc);
    }
//    ConfigDocument<CurveSpecificationBuilderConfiguration> doc = new ConfigDocument<CurveSpecificationBuilderConfiguration>();
//    doc.setName("DEFAULT_USD");
//    doc.setValue(CurveDefinitionAndSpecifications.buildUSDCurveSpecificationBuilderConfiguration());
    
  }

}

@@ -1333,20 +1333,21 @@
 _logger.
-warn
+debug
 (%22Curve 
@@ -1369,28 +1369,29 @@
    s_logger.
-warn
+debug
 (%22  Name:%22 +
@@ -1428,28 +1428,29 @@
    s_logger.
-warn
+debug
 (%22  Currency
@@ -1495,28 +1495,29 @@
    s_logger.
-warn
+debug
 (%22  Strips:%22
@@ -1603,12 +1603,13 @@
 ger.
-warn
+debug
 (%22  

Change diagnostic printing from WARN to DEBUG.