package com.galois.qrstream.lib;

import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.widget.TextView;

import java.util.Iterator;
import java.util.List;

import com.galois.qrstream.qrpipe.Transmit;
import com.galois.qrstream.qrpipe.TransmitException;
import com.galois.qrstream.image.BitmapImage;
import com.google.common.base.Charsets;
import org.apache.commons.lang.RandomStringUtils;


/**
 * Created by donp on 2/11/14.
 */
public class TransmitFragment extends Fragment {

    private TextView dataTitle;
    private ImageView send_window;
    private Button sendButton;
    private Transmit transmitter;
    private Job job;

    // Allows us to step through QR code transmission
    private Iterable<BitmapImage> qrCodes;
    private Iterator<BitmapImage> qrCodeIter;
    private int count = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        transmitter = new Transmit(350,350);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.transmit_fragment, container, false);

        send_window = (ImageView)rootView.findViewById(R.id.send_window);
        dataTitle = (TextView)rootView.findViewById(R.id.data_title);
        sendButton = (Button)rootView.findViewWithTag("send");
        sendButton.setOnClickListener(new CaptureClick());
        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();
        Bundle bundle = getArguments();
        if(bundle != null) {
            job = (Job) bundle.getSerializable("job");
            transmitData(job.getTitle(), job.getData());
            nextFrame();
        }
    }

    private void transmitData(String title, byte[] bytes) {
        Log.d("qrstream", "transmitData title="+title+" byte count="+bytes.length);
        updateUi(title);
        Log.i(Constants.APP_TAG, "Trying to create and transmit QR codes");
        try {
            // TODO Replace encoding of test strings with user data
            //qrCodes = encodeRandomTestData();
            qrCodes = transmitter.encodeQRCodes(bytes);
            qrCodeIter = qrCodes.iterator();
            count = 0;
            Log.i(Constants.APP_TAG, "Successful creation of QR codes");
        } catch (TransmitException e) {
            Log.e(Constants.APP_TAG, e.getMessage());
        }
    }

    private void updateUi(String title) {
        sendButton.setText("Next frame");
        dataTitle.setText(title);
    }

    private void nextFrame() {
        if (qrCodeIter != null) {
            if (qrCodeIter.hasNext()) {
                count++;
                Log.w(Constants.APP_TAG, "Drawing QR Code: " + count);
                Bitmap b = toBitmap(qrCodeIter.next());
                send_window.setImageDrawable(new BitmapDrawable(getResources(), b));
            } else {
                // Reset so we can transmit again
                qrCodeIter = qrCodes.iterator();
                count = 0;
            }
        }
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    public class CaptureClick implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            nextFrame();
        }
    }

    /**
     * Converts from QRlib BitmapImage to Android's Bitmap image type
     */
    private Bitmap toBitmap(final BitmapImage matrix) {
        int height = matrix.getHeight();
        int width = matrix.getWidth();
        Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                bmp.setPixel(x, y, matrix.get(x, y) ? Color.BLACK : Color.WHITE);
            }
        }
        return bmp;
    }

    /**
     * Make more dynamic test string using Apache commons random string generator
     */
    private Iterable<BitmapImage> encodeRandomTestData() throws TransmitException {
        String input = RandomStringUtils.randomAlphanumeric(20);
        Log.w(Constants.APP_TAG, "About to encode: " + input);
        qrCodes = transmitter.encodeQRCodes(input.getBytes(Charsets.ISO_8859_1));

        // Debugging output that prints number of generated QR codes
        int qrCount = 0;
        for (BitmapImage i : qrCodes) {
            qrCount++;
        }
        Log.w(Constants.APP_TAG, "# codes generated: " + qrCount);
        return qrCodes;
    }
}

@@ -254,16 +254,54 @@
 wGroup;%0A
+import android.view.ViewTreeObserver;%0A
 import a
@@ -463,24 +463,60 @@
 apDrawable;%0A
+import android.widget.LinearLayout;%0A
 import andro
@@ -1352,53 +1352,8 @@
 e);%0A
-        transmitter = new Transmit(350,350);%0A
     
@@ -1603,73 +1603,787 @@
     
-send_window = (ImageView)rootView.findViewById(R.id.send_window);
+LinearLayout ll = (LinearLayout)rootView.findViewById(R.id.transmit_layout);%0A        ll.setKeepScreenOn(true);%0A%0A        send_window = (ImageView)rootView.findViewById(R.id.send_window);%0A        send_window.addOnLayoutChangeListener(new View.OnLayoutChangeListener() %7B%0A            @Override%0A            public void onLayoutChange(View v, int left, int top, int right, int bottom,%0A                                       int oldLeft, int oldTop, int oldRight, int oldBottom) %7B%0A                Log.d(Constants.APP_TAG, %22Transmitter created for width %22+%0A                        send_window.getWidth()+%22 height %22+send_window.getHeight());%0A                transmitter = new Transmit(send_window.getWidth(), send_window.getHeight());%0A                sendJob();%0A            %7D%0A        %7D);%0A
 %0A   
@@ -2611,72 +2611,32 @@
     
-@Override%0A    public void onResume() %7B%0A        super.onResume();
+private void sendJob() %7B
 %0A   
@@ -2847,32 +2847,108 @@
        %7D%0A    %7D%0A%0A
+    @Override%0A    public void onResume() %7B%0A        super.onResume();%0A    %7D%0A%0A
     private void
@@ -3042,15 +3042,19 @@
 le=%22
-+
+ + 
 title
-+
+ + 
 %22 by
@@ -3063,17 +3063,19 @@
  count=%22
-+
+ + 
 bytes.le

Create the Transmitter once the image view dimentions are known