/*
 * Copyright 2013 bits of proof zrt.
 *
 * 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.
 */
package com.bitsofproof.btc1k.server;

import io.dropwizard.Application;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;

import java.security.Security;

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bitsofproof.btc1k.server.commands.TimeoutPaymentRequestCommand;
import com.bitsofproof.btc1k.server.resource.BopShopResource;
import com.bitsofproof.btc1k.server.vault.Vault;
import com.bitsofproof.dropwizard.supernode.SupernodeBundle;
import com.bitsofproof.dropwizard.supernode.SupernodeConfiguration;
import com.bitsofproof.supernode.account.ConfirmationManager;
import com.bitsofproof.supernode.api.BCSAPI;

public class Btc1kApplication extends Application<Btc1kConfiguration>
{
	private static final Logger log = LoggerFactory.getLogger (Btc1kApplication.class);

	private final SupernodeBundle<Btc1kConfiguration> supernodeBundle = new SupernodeBundle<Btc1kConfiguration> ()
	{
		@Override
		protected SupernodeConfiguration getSupernodeConfiguration (Btc1kConfiguration configuration)
		{
			return configuration.getSupernode ();
		}
	};

	private Vault vault;

	public static void main (String[] args) throws Exception
	{
		Security.addProvider (new BouncyCastleProvider ());
		new Btc1kApplication ().run (args);
	}

	@Override
	public void initialize (Bootstrap<Btc1kConfiguration> bootstrap)
	{
		bootstrap.addBundle (supernodeBundle);
		bootstrap.addCommand (new TimeoutPaymentRequestCommand ());
	}

	@Override
	public void run (Btc1kConfiguration configuration, Environment environment) throws Exception
	{
		BCSAPI api = supernodeBundle.getBCSAPI ();
		ConfirmationManager confirmationManager = new ConfirmationManager ();
		confirmationManager.init (api, 144);
		api.registerTrunkListener (confirmationManager);
		vault = configuration.getVaultFactory ().createVault ();
		vault.getAccountManager ().sync (api);
		confirmationManager.addAccount (vault.getAccountManager ());
		api.registerRejectListener (vault.getAccountManager ());
		api.registerTransactionListener (vault.getAccountManager ());
		System.out.println ("Vault " + vault.getVaultAddress ());
		environment.jersey ().register (new BopShopResource (
				supernodeBundle.getBCSAPI (),
				vault,
				configuration.getBopShopKey (),
				configuration.getCustomerId (),
				configuration.getPassphrase ()
				).processCleared ());
	}
}

@@ -663,24 +663,64 @@
 pplication;%0A
+import io.dropwizard.lifecycle.Managed;%0A
 import io.dr
@@ -2319,78 +2319,693 @@
 ;%0A%09%09
-ConfirmationManager confirmationManager = new ConfirmationManager ();%0A
+environment.lifecycle ().manage (new Btc1kService (api, configuration.getVaultFactory ().createVault ()));%0A%09%09environment.jersey ().register (new BopShopResource (%0A%09%09%09%09supernodeBundle.getBCSAPI (),%0A%09%09%09%09vault,%0A%09%09%09%09configuration.getBopShopKey (),%0A%09%09%09%09configuration.getCustomerId (),%0A%09%09%09%09configuration.getPassphrase ()%0A%09%09%09%09).processCleared ());%0A%09%7D%0A%0A%09private static class Btc1kService implements Managed%0A%09%7B%0A%09%09private ConfirmationManager confirmationManager = new ConfirmationManager ();%0A%09%09private BCSAPI api;%0A%09%09private Vault vault;%0A%0A%09%09private Btc1kService (BCSAPI api, Vault vault)%0A%09%09%7B%0A%09%09%09this.api = api;%0A%09%09%09this.vault = vault;%0A%09%09%7D%0A%0A%09%09@Override%0A%09%09public void start () throws Exception%0A%09%09%7B%0A%09
 %09%09co
@@ -3038,13 +3038,14 @@
 i, 1
-44
+00
 );%0A
+%09
 %09%09ap
@@ -3096,66 +3096,8 @@
 );%0A%09
-%09vault = configuration.getVaultFactory ().createVault ();%0A
 %09%09va
@@ -3133,16 +3133,17 @@
  (api);%0A
+%09
 %09%09confir
@@ -3191,32 +3191,33 @@
 tManager ());%0A%09%09
+%09
 api.registerReje
@@ -3257,16 +3257,17 @@
 er ());%0A
+%09
 %09%09api.re
@@ -3324,16 +3324,17 @@
  ());%0A%09%09
+%09
 System.o
@@ -3389,239 +3389,69 @@
 ;%0A%09%09
-environment.jersey ().register (new BopShopResource (%0A%09%09%09%09supernodeBundle.getBCSAPI (),%0A%09%09%09%09vault,%0A%09%09%09%09configuration.getBopShopKey (),%0A%09%09%09%09configuration.getCustomerId (),%0A%09%09%09%09configuration.getPassphrase ()%0A%09%09%09%09).processCleared ());
+%7D%0A%0A%09%09@Override%0A%09%09public void stop () throws Exception%0A%09%09%7B%0A%09%09%7D
 %0A%09%7D%0A

use dropwizard lifecycle