package com.github.bitcoinlabs.bitcoinmobileandroid;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Created by IntelliJ IDEA.
 * User: kevin
 * Date: 4/1/11
 * Time: 2:56 PM
 * To change this template use File | Settings | File Templates.
 */
public class ConfirmPay extends Activity
{
    public static final String CONFIRM_PAY_URI = "CONFIRM_PAY_URI";

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setTitle("Send Bitcoin");
        setContentView(R.layout.pay_confirm);
        
        double val = 0;
        Uri bitcoinUri = getIntent().getData();
        assert("bitcoin".equals(bitcoinUri.getScheme()));
        //hackity hackity
        bitcoinUri = Uri.parse("bitcoin://" + bitcoinUri.getEncodedSchemeSpecificPart());

        final String bitcoinAddress = bitcoinUri.getAuthority();
        String amount = bitcoinUri.getQueryParameter("amount");
        final String label = bitcoinUri.getQueryParameter("label");
        String message = bitcoinUri.getQueryParameter("message");

        TextView payAddress = (TextView)findViewById(R.id.snd_address);
        final EditText payAmount = (EditText) findViewById(R.id.snd_amount);
        final EditText payLabel = (EditText) findViewById(R.id.snd_label);
        final EditText payMessage = (EditText) findViewById(R.id.snd_message);
        payAddress.setText("Address: " + bitcoinAddress);
        payLabel.setText(label);
        payMessage.setText(message);
        
        double amountD = 0.0;
        try {
            amountD = Double.parseDouble(amount);
        } catch (Exception e) {
        }
        payAmount.setText(MoneyUtils.formatMoney(amountD));
        payAmount.setSelectAllOnFocus(true);
        findViewById(R.id.confirmButton).setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View view)
            {
                Toast.makeText(ConfirmPay.this, payAmount.getText() + "BTC paid to " + payLabel.getText() + " (" + bitcoinAddress + ")" , Toast.LENGTH_LONG).show();
                finish();
            }
        });
        findViewById(R.id.cancelButton).setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View view)
            {
                setResult(RESULT_CANCELED);
                Toast.makeText(ConfirmPay.this, "Bitcoin payment canceled." , Toast.LENGTH_LONG).show();
                finish();
            }
        });
    }

    public static void callMe(Context c, Uri bitcoinUri)
    {
        final Intent intent = new Intent(null, bitcoinUri, c, ConfirmPay.class);
        c.startActivity(intent);
    }
}
@@ -307,16 +307,171 @@
 t.Toast;
+%0Aimport com.google.bitcoin.core.TransactionStandaloneEncoder;%0Aimport com.google.bitcoin.core.NetworkParameters;%0Aimport com.google.bitcoin.core.Transaction;
 %0A%0A/**%0A *
@@ -1860,32 +1860,66 @@
 sage);%0A        %0A
+        // TODO: lossless parsing%0A
         double a
@@ -1932,16 +1932,16 @@
  = 0.0;%0A
-
         
@@ -2030,32 +2030,100 @@
  e) %7B%0A        %7D%0A
+        final long amountSatoshis = (long)(amountD * 1e8);%0A        %0A
         payAmoun
@@ -2357,32 +2357,569 @@
 )%0A            %7B%0A
+                WalletOpenHelper helper = new WalletOpenHelper(getApplicationContext());%0A                Transaction tx = helper.createTransaction(amountSatoshis, bitcoinAddress);%0A                if (tx == null) %7B%0A                    Toast.makeText(ConfirmPay.this, %22Insufficient balance.%22 , Toast.LENGTH_LONG).show();%0A                %7D%0A                else %7B%0A                    byte%5B%5D msg = tx.bitcoinSerialize();%0A                    %0A                    // TODO log to DB%0A                    // TODO send msg%0A                    %0A    
                 
@@ -3059,32 +3059,50 @@
 H_LONG).show();%0A
+                %7D%0A
                 

use createTransaction