package org.vietabroader.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamPanel;

import java.awt.event.WindowAdapter;
import java.awt.image.BufferedImage;
import java.awt.event.WindowEvent;
import java.util.List;
import javax.sound.midi.SysexMessage;
import javax.swing.*;

import com.google.zxing.BinaryBitmap;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.Result;
import com.google.zxing.common.HybridBinarizer;
import org.vietabroader.model.GlobalState;
import org.vietabroader.model.VASpreadsheet;
import sun.jvm.hotspot.runtime.Threads;

public class WebcamController implements Controller {
    private static final Logger logger = LoggerFactory.getLogger(AuthenticationController.class);

    private final int READER_SLEEP_TIME_MS = 1000;

    private List<Object> participantList;

    private Webcam webcam;
    private WebcamPanel webcamPanel;
    private JFrame webcamView;
    private JTextField txtWebcamMessage;

    public WebcamController setWebcam(Webcam cam) {
        webcam = cam;
        return this;
    }

    public WebcamController setWebcamView(JFrame view) {
        webcamView = view;
        return this;
    }

    public WebcamController setWebcamPanel(WebcamPanel pan) {
        webcamPanel = pan;
        return this;
    }

    public WebcamController setTextWebcamMessage(JTextField txt) {
        txtWebcamMessage = txt;
        return this;
    }

    @Override
    public void control() {
        SwingWorker<Void, String> worker = new QRReaderWorker();

        webcamView.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e){
                worker.cancel(false);
                webcamPanel.stop();
                GlobalState.getInstance().setStatus(GlobalState.Status.REFRESHED);
            }
        });

        worker.execute();
    }

    public class QRReaderWorker extends SwingWorker<Void, String> {

        @Override
        public Void doInBackground() throws InterruptedException {
            do {
                Thread.sleep(READER_SLEEP_TIME_MS);

                Result result = null;
                BufferedImage image;

                if (webcam.isOpen()) {

                    if ((image = webcam.getImage()) == null) {
                        continue;
                    }

                    LuminanceSource source = new BufferedImageLuminanceSource(image);
                    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

                    try {
                        result = new MultiFormatReader().decode(bitmap);
                    } catch (NotFoundException e) {
                        // fall thru, it means there is no QR code in image
                    }
                }

                if (result != null) {
                    publish(result.getText());
                }
            } while (!isCancelled());

            return null;
        }

        @Override
        protected void process(List<String> chunks) {
            VASpreadsheet spreadsheet = GlobalState.getInstance().getSpreadsheet();
            int foundValueAt;

            try {
                participantList = spreadsheet.readCol("Key");

            } catch (Exception e) {

            }

            for (String result : chunks) {
                System.out.println(result);

                if (participantList.contains(result)) {
                    foundValueAt = participantList.indexOf(result);

                    try {
                        if (spreadsheet.readValue("Output", foundValueAt).equals("Checked in")) {
                            txtWebcamMessage.setText("This person has already checked in");
                            continue;
                        } else {
                            spreadsheet.writeValue("Output", foundValueAt, "Checked in");

                            txtWebcamMessage.setText("Checking this person in... OK");
                            new Thread() {
                                public void run() {
                                    try {
                                        spreadsheet.uploadOneColumn("Output");
                                    } catch (Exception e) {

                                    }
                                }
                            }.start();
                        }
                    } catch (Exception e) {

                    }

                } else {
                    txtWebcamMessage.setText("This person has not been registered yet");
                }
            }
        }
    }
}
@@ -174,16 +174,35 @@
 Panel;%0A%0A
+import java.awt.*;%0A
 import j
@@ -319,16 +319,21 @@
 va.util.
+Array
 List;%0Aim
@@ -345,33 +345,18 @@
 java
-x.sound.midi.SysexMessage
+.util.List
 ;%0Aim
@@ -778,48 +778,8 @@
 eet;
-%0Aimport sun.jvm.hotspot.runtime.Threads;
 %0A%0Apu
@@ -985,51 +985,8 @@
 0;%0A%0A
-    private List%3CObject%3E participantList;%0A%0A
     
@@ -3290,24 +3290,62 @@
     
-int foundValueAt
+List%3CObject%3E participantList = new ArrayList%3CObject%3E()
 ;%0A%0A 
@@ -3419,25 +3419,24 @@
 Col(%22Key%22);%0A
-%0A
             
@@ -3521,52 +3521,8 @@
 s) %7B
-%0A                System.out.println(result);
 %0A%0A  
@@ -3571,24 +3571,62 @@
 (result)) %7B%0A
+                    int foundValueAt;%0A
             
@@ -3867,24 +3867,40 @@
 %22This person
+, %22 + result + %22
  has already
@@ -3938,32 +3938,148 @@
             
-continue
+txtWebcamMessage.setBackground(Color.YELLOW);%0A                            txtWebcamMessage.setDisabledTextColor(Color.BLACK)
 ;%0A          
@@ -4259,29 +4259,185 @@
 ing 
-this person in... OK%22
+%22 + result + %22 in... OK%22);%0A                            txtWebcamMessage.setBackground(Color.GREEN);%0A                            txtWebcamMessage.setDisabledTextColor(Color.WHITE
 );%0A 
@@ -4991,49 +4991,165 @@
 ext(
-%22This person has not been registered yet%22
+result + %22 is invalid%22);%0A                    txtWebcamMessage.setBackground(Color.RED);%0A                    txtWebcamMessage.setDisabledTextColor(Color.WHITE
 );%0A 

fix indentation, color and background color of the text field