package com.github.bitcoinlabs.bitcoinmobileandroid;

import java.util.Hashtable;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Display;
import android.view.WindowManager;
import android.widget.ImageView;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;

import com.google.bitcoin.core.ECKey;
import com.google.bitcoin.core.Address;
import com.google.bitcoin.core.NetworkParameters;


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

    private static final int WHITE = 0xFFFFFFFF;
    private static final int BLACK = 0xFF000000;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.receive_qr_display);

        final double recVal = getIntent().getDoubleExtra(RECEIVE_VALUE, 0);

        setTitle("Invoice: " + MoneyUtils.formatMoney(recVal));
        try {
            showQrBitmap(recVal);
        } catch (WriterException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void callMe(Context c, double recVal) {
        final Intent intent = new Intent(c, QrDisplay.class);
        intent.putExtra(RECEIVE_VALUE, recVal);
        c.startActivity(intent);
    }

    private void showQrBitmap(double val) throws WriterException {
        
        ECKey k = new ECKey();
        String address = k.toAddress(NetworkParameters.prodNet()).toString();
        
        BarcodeFormat format = BarcodeFormat.QR_CODE;
        int dimension = 0;
        String contents = "bitcoin:"+address+"?amount="+val;
        Hashtable<EncodeHintType, Object> hints = null;
        String encoding = guessAppropriateEncoding(contents);
        if (encoding != null) {
            hints = new Hashtable<EncodeHintType, Object>(2);
            hints.put(EncodeHintType.CHARACTER_SET, encoding);
        }
        MultiFormatWriter writer = new MultiFormatWriter();
        BitMatrix result = writer.encode(contents, format, dimension , dimension, hints);
        int width = result.getWidth();
        int height = result.getHeight();
        int[] pixels = new int[width * height];
        // All are 0, or black, by default
        for (int y = 0; y < height; y++) {
            int offset = y * width;
            for (int x = 0; x < width; x++) {
                pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
            }
        }

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        BitmapDrawable drawable = new BitmapDrawable(bitmap);
        drawable.setFilterBitmap(false);
        final ImageView qrDisplay = (ImageView) findViewById(R.id.qrDisplay);
        qrDisplay.setImageDrawable(drawable);
    }

    private static String guessAppropriateEncoding(CharSequence contents) {
        // Very crude at the moment
        for (int i = 0; i < contents.length(); i++) {
          if (contents.charAt(i) > 0xFF) {
            return "UTF-8";
          }
        }
        return null;
      }
}
@@ -168,16 +168,63 @@
 Intent;%0A
+import android.database.sqlite.SQLiteDatabase;%0A
 import a
@@ -247,16 +247,16 @@
 Bitmap;%0A
-
 import a
@@ -755,16 +755,86 @@
 eters;%0A%0A
+import com.github.bitcoinlabs.bitcoinmobileandroid.WalletOpenHelper;%0A%0A
 %0A/**%0A * 
@@ -1925,32 +1925,56 @@
 tion %7B%0A        %0A
+        // Create a key%0A
         ECKey k 
@@ -1972,16 +1972,18 @@
  ECKey k
+ey
  = new E
@@ -2016,12 +2016,16 @@
 ress
+58
  = k
+ey
 .toA
@@ -2073,32 +2073,440 @@
 ing();%0A        %0A
+        // Save it%0A        WalletOpenHelper helper = new WalletOpenHelper(getApplicationContext());%0A        SQLiteDatabase db = helper.getWritableDatabase();%0A        db.execSQL(%0A                %22INSERT INTO keys ('address58', 'key') VALUES (?, ?)%22,%0A                new Object%5B%5D %7B address58, key.toASN1()%7D );%0A        %0A        //TEMP: read it%0A        //Cursor cursor = db.query(%22SELECT * FROM keys%22);%0A        %0A
         BarcodeF
@@ -2610,17 +2610,23 @@
 in:%22
-+
+ + 
 address
-+
+58 + 
 %22?am
@@ -2631,17 +2631,19 @@
 amount=%22
-+
+ + 
 val;%0A   

save new private keys