package org.foodrev.androidthingpi;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

import com.google.android.things.pio.PeripheralManagerService;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import org.foodrev.androidthingpi.leds.BlueLED;
import org.foodrev.androidthingpi.leds.GreenLED;
import org.foodrev.androidthingpi.leds.RedLED;
import org.foodrev.androidthingpi.leds.base.LED;

import java.util.ArrayList;

/**
 * Skeleton of an Android Things activity.
 * <p>
 * Android Things peripheral APIs are accessible through the class
 * PeripheralManagerService. For example, the snippet below will open a GPIO pin and
 * set it to HIGH:
 * <p>
 * <pre>{@code
 * PeripheralManagerService service = new PeripheralManagerService();
 * mLedGpio = service.openGpio("BCM6");
 * mLedGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
 * mLedGpio.setValue(true);
 * }</pre>
 * <p>
 * For more complex peripherals, look for an existing user-space driver, or implement one if none
 * is available.
 *
 * @see <a href="https://github.com/androidthings/contrib-drivers#readme">https://github.com/androidthings/contrib-drivers#readme</a>
 */
public class MainActivity extends Activity {

    private final static String TAG = "MainActivity";
    FirebaseDatabase firebaseDatabase;
    DatabaseReference databaseReference;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d("Hooray!", "onCreate: we did it!");
        createLightsThread();
    }

    public void createLightsThread() {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                setupFirebase();
                PeripheralManagerService manager = new PeripheralManagerService();

                LED redLED = new RedLED(manager);
                LED greenLED = new GreenLED(manager);
                LED blueLED = new BlueLED(manager);

                ArrayList<LED> ledArray = new ArrayList<>();

                ledArray.add(redLED);
                ledArray.add(greenLED);
                ledArray.add(blueLED);

                while (true) {
                    for (LED led : ledArray) {
                        led.turnOn();
                        databaseReference.setValue(led.toString());
                        delay(1000);
                        led.turnOff();
                    }
                }
            }
        });
        thread.start();
    }

    public void delay(int delayMillis) {
        try {
            Thread.sleep(delayMillis);
        } catch(InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
    }

    private void setupFirebase() {
        firebaseDatabase = FirebaseDatabase.getInstance();
        databaseReference = firebaseDatabase.getReference("current_led");
    }

}

@@ -170,24 +170,125 @@
 gerService;%0A
+import com.google.firebase.database.DataSnapshot;%0Aimport com.google.firebase.database.DatabaseError;%0A
 import com.g
@@ -383,16 +383,72 @@
 atabase;
+%0Aimport com.google.firebase.database.ValueEventListener;
 %0A%0Aimport
@@ -1988,24 +1988,91 @@
 Firebase();%0A
+                databaseReference.child(%22faucet%22).setValue(%22off%22);%0A
             
@@ -2163,15 +2163,22 @@
     
-LED red
+final LED blue
 LED 
@@ -2183,19 +2183,20 @@
 D = new 
-Red
+Blue
 LED(mana
@@ -2193,32 +2193,33 @@
 ueLED(manager);%0A
+%0A
                 
@@ -2222,356 +2222,559 @@
     
-LED greenLED = new GreenLED(manager);%0A                LED blueLED = new BlueLED(manager);%0A%0A                ArrayList%3CLED%3E ledArray = new ArrayList%3C%3E();%0A%0A                ledArray.add(redLED);%0A                ledArray.add(greenLED);%0A                ledArray.add(blueLED);%0A%0A                while (true) %7B%0A                    for (LED led : ledArray) %7B
+ValueEventListener valueEventListener = new ValueEventListener() %7B%0A                    @Override%0A                    public void onDataChange(DataSnapshot dataSnapshot) %7B%0A                        // double check to ensure child is there%0A                        if(dataSnapshot.hasChild(%22faucet%22)) %7B%0A                            // if on, then perform sequence%0A                            if (dataSnapshot.child(%22faucet%22).getValue().toString().equals(%22on%22)) %7B%0A                                blueLED.turnOn();%0A                                delay(5000);
 %0A   
@@ -2798,22 +2798,43 @@
     
-led
+        blueLED
 .turnO
-n
+ff
 ();%0A
+        
     
@@ -2875,31 +2875,38 @@
 nce.
-setValue(led.toString()
+child(%22faucet%22).setValue(%22off%22
 );%0A 
@@ -2932,20 +2932,13 @@
     
-delay(1000);
+    %7D
 %0A   
@@ -2962,62 +2962,257 @@
     
-led.turnOff();%0A                    %7D%0A                %7D
+%7D%0A                    %7D%0A%0A                    @Override%0A                    public void onCancelled(DatabaseError databaseError) %7B%0A                    %7D%0A                %7D;%0A%0A                databaseReference.addValueEventListener(valueEventListener);%0A
 %0A   
@@ -3624,19 +3624,9 @@
 ce(%22
-current_led
+/
 %22);%0A

add faucet control