package ca.simonho.randomwalk;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    ImageButton forwardButton;
    ImageButton backButton;
    ImageButton leftButton;
    ImageButton rightButton;
    ImageButton randomButton;

    HashMap<ImageButton, Boolean> buttonStates;
    Boolean newButtonState;
    ArrayList<ImageButton> selectedButtons;
    ImageButton randomlyChosenButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        //Get references to buttons
        forwardButton = (ImageButton) findViewById(R.id.forwardButton);
        backButton = (ImageButton) findViewById(R.id.backButton);
        leftButton = (ImageButton) findViewById(R.id.leftButton);
        rightButton = (ImageButton) findViewById(R.id.rightButton);
        randomButton = (ImageButton) findViewById(R.id.randomButton);

        selectedButtons = new ArrayList<>();

        //Set initial button state
        buttonStates = new HashMap<>();
        buttonStates.put(forwardButton, false);
        buttonStates.put(backButton, false);
        buttonStates.put(leftButton, true);
        buttonStates.put(rightButton, true);
        buttonStates.put(randomButton, true);

        //Set initial state based colour and click listener for each button
        for (HashMap.Entry<ImageButton, Boolean> entry : buttonStates.entrySet()) {
            setColour(entry.getKey(), entry.getValue());

            entry.getKey().setOnClickListener(this);
        }
    }

    private void toggleButtonState(ImageButton button){
        newButtonState = !buttonStates.get(button);
        buttonStates.put(button, newButtonState);
        setColour(button, newButtonState);
    }

    private void setColour(ImageButton button, Boolean newState){
        if (newState){
            button.setColorFilter(getResources().getColor(R.color.buttonSelected));
        } else {
            button.setColorFilter(getResources().getColor(R.color.buttonNotSelected));
        }
    }

    private void showRandomDirection(ImageButton button){
        button.setColorFilter(getResources().getColor(R.color.buttonChosen));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_about) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.forwardButton:
                //Toggle the existing button state/colour
                toggleButtonState(forwardButton);
                break;
            case R.id.backButton:
                //Toggle the existing button state/colour
                toggleButtonState(backButton);
                break;
            case R.id.leftButton:
                //Toggle the existing button state/colour
                toggleButtonState(leftButton);
                break;
            case R.id.rightButton:
                //Toggle the existing button state/colour
                toggleButtonState(rightButton);
                break;
            case R.id.randomButton:
                selectedButtons.clear();

                for (HashMap.Entry<ImageButton, Boolean> entry : buttonStates.entrySet()) {
                    //Reset button colour states each click
                    setColour(entry.getKey(), entry.getValue());

                    //Get all buttons that have been selected
                    if(entry.getKey() != randomButton & entry.getValue()){
                        selectedButtons.add(entry.getKey());
                    }
                }

                //Get randomly picked direction
                if (selectedButtons.size() > 0){
                    randomlyChosenButton = selectedButtons.get((new Random()).nextInt(selectedButtons.size()));

                    //Set chosen direction arrow colour
                    showRandomDirection(randomlyChosenButton);

                    Log.d("Random direction: ", String.valueOf(getResources().getResourceEntryName(randomlyChosenButton.getId())));
                }

                for(ImageButton button:selectedButtons){
                    Log.d("Selected: ", String.valueOf(getResources().getResourceEntryName(button.getId())));
                }
                Log.d("---","---");
                break;
        }
    }
}

@@ -51,16 +51,43 @@
 Bundle;%0A
+import android.os.Handler;%0A
 import a
@@ -781,24 +781,96 @@
 senButton;%0A%0A
+    Integer directionShowDuration = 5000;%0A    Handler durationHandler;%0A%0A
     @Overrid
@@ -1514,41 +1514,205 @@
     
-selectedButtons = new ArrayList%3C%3E
+//Initialize list of user selected buttons%0A        selectedButtons = new ArrayList%3C%3E();%0A%0A        //Initialize the handler for chosen direction display duration%0A        durationHandler = new Handler
 ();%0A
@@ -2843,16 +2843,22 @@
 rection(
+final 
 ImageBut
@@ -2862,32 +2862,89 @@
 Button button)%7B%0A
+        //Highlight the randomly chosen direction button%0A
         button.s
@@ -3004,16 +3004,303 @@
 hosen));
+%0A%0A        //Set button colour back to state based colour after a delay%0A        durationHandler.postDelayed(new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A                setColour(button, buttonStates.get(button));%0A            %7D%0A        %7D, directionShowDuration);
 %0A    %7D%0A%0A

Set chosen direction button colour back to state