package com.example.rcos.gomueller;

import android.app.ListActivity;
import android.graphics.Color;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.util.SparseBooleanArray;
import java.util.ArrayList;


public class ExerciseDetailActivity extends ListActivity {

    private ArrayList<String> detailArray;
    private ArrayAdapter<String> adapter;
    private ListView detailListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exercise_detail);

        Bundle bundle = getIntent().getExtras();
        final String message = bundle.getString("message");

        final ExerciseCRUD crudDetail = new ExerciseCRUD(this);

        detailArray = crudDetail.getExerciseDetail(message);
        adapter = new ArrayAdapter<String>(this, R.layout.row_layout, R.id.listText, detailArray);

        Button btnDel = (Button) findViewById(R.id.btnDel);

        //When delete button is pressed...
        OnClickListener listenerDel = new OnClickListener() {
            @Override
            public void onClick(View v) {
                //Get the checked items
                SparseBooleanArray checkedItemPositions = getListView().getCheckedItemPositions();
                int itemCount = getListView().getCount();

                //Delete all the selected items
                for(int i=itemCount-1; i >= 0; i--){
                    if(checkedItemPositions.get(i))
                    {
                        crudDetail.delete(message, detailArray.get(i));
                        adapter.remove(detailArray.get(i));
                    }
                }
                checkedItemPositions.clear();
                adapter.notifyDataSetChanged();
            }
        };

        /** Setting the event listener for the delete button */
        btnDel.setOnClickListener(listenerDel);

        setListAdapter(adapter);
    }


    @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_exercise_detail, 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_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onListItemClick(ListView list, View view, int position, long id) {
        super.onListItemClick(list, view, position, id);

        String selectedItem = (String) getListView().getItemAtPosition(position);

        //highlight the selected item
        view.setBackgroundColor(Color.YELLOW);

        adapter.notifyDataSetChanged();
    }
}

@@ -520,16 +520,46 @@
 ayList;%0A
+import java.util.Collections;%0A
 %0A%0Apublic
@@ -655,16 +655,73 @@
 lArray;%0A
+    private boolean isItemSelected%5B%5D = new boolean%5B100%5D;%0A
     priv
@@ -1199,24 +1199,25 @@
 l(message);%0A
+%0A
         adap
@@ -3330,24 +3330,214 @@
 position);%0A%0A
+        //toggle selected item on/off%0A        isItemSelected%5Bposition%5D = !isItemSelected%5Bposition%5D;%0A        //Check whether the item was selected%0A        if (isItemSelected%5Bposition%5D) %7B%0A    
         //hi
@@ -3562,16 +3562,20 @@
 ed item%0A
+    
         
@@ -3613,16 +3613,135 @@
 ELLOW);%0A
+        %7D%0A        else %7B%0A            //set it to transparent%0A            view.setBackgroundColor(0x00000000);%0A        %7D
 %0A       

Allow the user to deselect exercises