/*
 *   Copyright 2013-2015 Daniel Pereira Coelho
 *   
 *   This file is part of the Expenses Android Application.
 *
 *   Expenses is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation in version 3.
 *
 *   Expenses is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with Expenses.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package com.dpcsoftware.mn;
import android.app.Dialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v4.widget.CursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
public class EditGroups extends AppCompatActivity {
	private ListView lv;
	private GroupsAdapter adapter;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listview);
		lv = (ListView) findViewById(R.id.listView1);
				
		renderGroups();
		
		getSupportActionBar().setTitle(R.string.editgroups_c1);
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.groups, menu);
		
		return true;
	}
	
	public boolean onOptionsItemSelected(MenuItem item) {
    	switch (item.getItemId()) {
    		case R.id.item1:
                Bundle args = new Bundle();
                args.putInt("MODE", AddEditDialog.ADD);
    			AddEditDialog addDg = new AddEditDialog();
                addDg.setArguments(args);
    			addDg.show(getSupportFragmentManager(), null);
    			break;
    	}
    	return true;
	}
	
	private void renderGroups() {
		SQLiteDatabase db = DatabaseHelper.quickDb(this, DatabaseHelper.MODE_READ);
		Cursor c = db.rawQuery("SELECT "
				+ Db.Table3._ID + ","
				+ Db.Table3.GROUP_NAME +
				" FROM " + Db.Table3.TABLE_NAME +
				" ORDER BY " + Db.Table3.GROUP_NAME + " ASC", null);
		if(adapter == null) {
			adapter = new GroupsAdapter(this, c);
			lv.setAdapter(adapter);
			setContentView(lv);
		}
		else {
			adapter.swapCursor(c);
			adapter.notifyDataSetChanged();
		}
		db.close();
	}
	
	private class GroupsAdapter extends CursorAdapter implements OnClickListener {
    	private LayoutInflater mInflater;
    	
	    public GroupsAdapter(Context context, Cursor c) {
	        super(context, c, 0);
	        mInflater=LayoutInflater.from(context);
	    }
	    
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            return mInflater.inflate(R.layout.editgroups_listitem,parent,false); 
        }
    	
    	public void bindView(View view, Context context, Cursor cursor) {
    		((TextView) view.findViewById(R.id.textViewGroup)).setText(cursor.getString(1));
    		ImageButton btEdit = (ImageButton) view.findViewById(R.id.imageButtonEdit);
    		btEdit.setOnClickListener(this);
    		btEdit.setTag(cursor.getPosition());
    		ImageButton btDelete = (ImageButton) view.findViewById(R.id.imageButtonDelete);
    		btDelete.setOnClickListener(this);
    		btDelete.setTag(cursor.getPosition());
    	}
    	
		@Override
		public void onClick(View v) {
			switch(v.getId()) {
			case R.id.imageButtonDelete:
				if(getCursor().getCount() == 1) {
					AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(EditGroups.this);
					dialogBuilder.setTitle(R.string.editgroups_c2);
					dialogBuilder.setMessage(R.string.editgroups_c3);
					dialogBuilder.create().show();					
				}
				else {
					Bundle args = new Bundle();
					args.putLong("DELETE_ID", getItemId((Integer) v.getTag()));
					DeleteDialog delDg = new DeleteDialog();
                    delDg.setArguments(args);
					delDg.show(getSupportFragmentManager(), null);
				}
				break;
			case R.id.imageButtonEdit:
				Bundle args2 = new Bundle();
				args2.putLong("EDIT_ID", getItemId((Integer) v.getTag()));
				Cursor c = getCursor();
				c.moveToPosition((Integer) v.getTag());
				args2.putString("CURRENT_NAME", c.getString(c.getColumnIndex(Db.Table3.GROUP_NAME)));
                args2.putInt("MODE", AddEditDialog.EDIT);
				AddEditDialog edtDg = new AddEditDialog();
                edtDg.setArguments(args2);
				edtDg.show(getSupportFragmentManager(), null);
				break;
			}
		}
    	
    }
	
	public static class DeleteDialog extends DialogFragment implements OnCheckedChangeListener, DialogInterface.OnClickListener {
		private long deleteId;
		private EditGroups act;
		private App app;
		private View layout;
        @NonNull
        public Dialog onCreateDialog(Bundle savedInstance) {
            act = (EditGroups) getActivity();
            app = (App) act.getApplication();
            Bundle args = getArguments();
            LayoutInflater li = LayoutInflater.from(act);
            layout = li.inflate(R.layout.editgroupseditcategories_deldialog, null);
			deleteId = args.getLong("DELETE_ID");
			
			Spinner sp = (Spinner) layout.findViewById(R.id.spinner1);
			SQLiteDatabase db = DatabaseHelper.quickDb(act, DatabaseHelper.MODE_READ);
			Cursor c = db.rawQuery("SELECT "
					+ Db.Table3._ID + ","
					+ Db.Table3.GROUP_NAME +
					" FROM " + Db.Table3.TABLE_NAME +
					" WHERE " + Db.Table3._ID + " <> " + deleteId +
					" ORDER BY " + Db.Table3.GROUP_NAME + " ASC", null);
			SimpleCursorAdapter adapter = new SimpleCursorAdapter(act, android.R.layout.simple_spinner_item, c, new String[] {Db.Table3.GROUP_NAME}, new int[] {android.R.id.text1}, 0);
			adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			sp.setAdapter(adapter);
			sp.setEnabled(false);
			
			((RadioButton) layout.findViewById(R.id.radio1)).setOnCheckedChangeListener(this);
			db.close();
            return new AlertDialog.Builder(act)
                    .setView(layout)
                    .setTitle(R.string.editgroups_c4)
                    .setPositiveButton(R.string.gp_2, this)
                    .setNegativeButton(R.string.gp_3, this)
                    .create();
		}
		
		@Override
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			layout.findViewById(R.id.spinner1).setEnabled(isChecked);
		}
		
		@Override
        public void onClick(DialogInterface dialog, int which) {
			if(which == DialogInterface.BUTTON_POSITIVE)
				deleteGroup();
			else
                dismiss();
		}
		
		private void deleteGroup() {
			SQLiteDatabase db = DatabaseHelper.quickDb(act, DatabaseHelper.MODE_WRITE);
			RadioGroup rg = (RadioGroup) layout.findViewById(R.id.radioGroup1);
			int toastString;
			
			int result = db.delete(Db.Table3.TABLE_NAME, Db.Table3._ID + " = " + deleteId, null);
			if(result == 1) {
				if(rg.getCheckedRadioButtonId() == R.id.radio0) {
                    //Delete expenses
                    db.delete(Db.Table1.TABLE_NAME, Db.Table1.ID_GROUP + " = " + deleteId, null);
                    //Delete budget items
                    db.delete(Db.Table4.TABLE_NAME, Db.Table4.ID_GROUP + " = " + deleteId, null);
                }
				else {
                    long newId = ((Spinner) layout.findViewById(R.id.spinner1)).getSelectedItemId();
                    //Update expenses


Answer: 					ContentValues cv = new ContentValues();