angular.module("omniFactories")
	.factory("Orderbook",["DExOffer","Transaction","Account","Wallet","ModalManager","MIN_MINER_FEE", "WHOLE_UNIT", 
		function OrderbookFactory(DExOffer,Transaction,Account,Wallet,ModalManager,MIN_MINER_FEE,WHOLE_UNIT){
			var Orderbook = function(tradingPair){
				var self = this;

				self.initialize = function(){
					self.tradingPair=tradingPair;
					self.title = "Trade #" + tradingPair.property + " for " + (tradingPair.pair == 1 ? "Mastercoin": "Test Mastercoin");
					self.active = true;
					self.disabled = !self.active;
					self.buy = new DExOffer();
					self.sell = new DExOffer();

					self.pair = Wallet.getAsset(tradingPair.pair);
					self.property = Wallet.getAsset(tradingPair.property);

					self.addresses = Wallet.addresses.filter(function(address){
						return ((address.privkey && address.privkey.length == 58) || address.pubkey)
					});

				};

				self.submitBuyOffer = function(){
					// TODO: Validations
					var fee = Account.settings.minerFee || MIN_MINER_FEE;
					var dexOffer = new Transaction(21,self.buy.address,fee,{
							transaction_version:0,
							sale_currency_id:self.tradingPair.pair,
							sale_amount:self.buy.amounts.pair,
							desired_currency_id:self.tradingPair.property,
							desired_amount:self.buy.amounts.property,
							action:1 // TODO: fill appropriate value
						});
					ModalManager.openConfirmationModal({
						dataTemplate: '/views/modals/partials/dex_offer.html',
						scope: {
							title:"Confirm DEx Transaction",
							address:self.buy.address,
							saleCurrency:self.tradingPair.pair,
							saleAmount:self.buy.amounts.pair,
							desiredCurrency:self.tradingPair.property,
							desiredAmount:self.buy.amounts.property,
							totalCost:dexOffer.totalCost,
							action:"Add",
							confirmText: "Create Transaction"
						},
						transaction:dexOffer
					})
				};

				self.submitSellOffer = function(){
					// TODO: Validations
					var fee = Account.settings.minerFee || MIN_MINER_FEE;
					var dexOffer = new Transaction(21,self.sell.address,fee,{
							transaction_version:0,
							sale_currency_id:self.tradingPair.property,
							sale_amount:self.buy.amounts.property,
							desired_currency_id:self.tradingPair.pair,
							desired_amount:self.buy.amounts.pair,
							action:1 // TODO: fill appropriate value
						});
					ModalManager.openConfirmationModal({
						dataTemplate: '/views/modals/partials/dex_offer.html',
						scope: {
							title:"Confirm DEx Transaction",
							address:self.sell.address,
							saleCurrency:self.tradingPair.pair,
							saleAmount:self.sell.amounts.pair,
							desiredCurrency:self.tradingPair.property,
							desiredAmount:self.sell.amounts.property,
							totalCost:dexOffer.totalCost,
							action:"Add",
							confirmText: "Create Transaction"
						},
						transaction:dexOffer
					})
				};

				self.getBalance = function(address, assetId){
					return address && address.getBalance(assetId) ? new Big(address.getBalance(assetId).value).times(WHOLE_UNIT).valueOf() : 0;
				}

				self.initialize();
			}

			return Orderbook;
		}]);
@@ -138,16 +138,32 @@
 _UNIT%22, 
+%22SATOSHI_UNIT%22, 
 %0A%09%09funct
@@ -256,16 +256,29 @@
 OLE_UNIT
+,SATOSHI_UNIT
 )%7B%0A%09%09%09va
@@ -1205,32 +1205,41 @@
 %09%09%09%09sale_amount:
+ new Big(
 self.buy.amounts
@@ -1235,32 +1235,63 @@
 buy.amounts.pair
+).times(SATOSHI_UNIT).valueOf()
 ,%0A%09%09%09%09%09%09%09desired
@@ -1344,32 +1344,40 @@
 %09desired_amount:
+new Big(
 self.buy.amounts
@@ -1389,57 +1389,56 @@
 erty
-,%0A%09%09%09%09%09%09%09action:1 // TODO: fill appropriate value
+).times(SATOSHI_UNIT).valueOf(),%0A%09%09%09%09%09%09%09action:1
 %0A%09%09%09
@@ -2251,32 +2251,41 @@
 sale_amount:
+new Big(
 self.
-buy
+sell
 .amounts.pro
@@ -2281,32 +2281,63 @@
 amounts.property
+).times(SATOSHI_UNIT).valueOf()
 ,%0A%09%09%09%09%09%09%09desired
@@ -2390,32 +2390,41 @@
 ired_amount:
+new Big(
 self.
-buy
+sell
 .amounts.pai
@@ -2428,57 +2428,57 @@
 pair
-,%0A%09%09%09%09%09%09%09action:1 // TODO: fill appropriate value
+).times(SATOSHI_UNIT).valueOf(),%0A%09%09%09%09%09%09%09action:1 
 %0A%09%09%09

convert values to satoshis