package com.insidesecure.drm.android.reference;


import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import org.json.JSONArray;


public class MultipleSelectionDialog extends AlertDialog
{
    private static final String OK_LABEL = "OK";
    private static final String CNL_LABEL = "CANCEL";
 

    private ArrayList<String>   rowsData;
    private ArrayAdapter        arrayAdapter;
    private ListView            listView;
    private SharedPreferences   sharedPreferences;
    private String              sharedPreferencesKey;
    private Editor              editor;


    public MultipleSelectionDialog(Context           context,
                                   String[]          contents,
                                   SharedPreferences sharedPreferences,
                                   String            sharedPreferencesKey,
                                   String            title)
    {

        super(context);

        rowsData = new ArrayList<String>();

        arrayAdapter = new ArrayAdapter(context, android.R.layout.simple_list_item_multiple_choice, rowsData);

        listView = new ListView(context);
        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        listView.setCacheColorHint(0);
        listView.setAdapter(arrayAdapter);

        if (contents != null)
        {
            for (int i = 0; i < contents.length; i ++)
            {
                arrayAdapter.add(contents[i]);
            }
        }
  
        this.sharedPreferences = sharedPreferences;
        this.sharedPreferencesKey = sharedPreferencesKey;

        editor = sharedPreferences == null ? null : sharedPreferences.edit();
  
        setTitle(title);
        setCancelable(false);
        setButton(AlertDialog.BUTTON_POSITIVE, OK_LABEL, new OnClickListener()
        {


            public void onClick(DialogInterface dialog,
                                int             id)
            {
                JSONArray storedData;

                storedData = new JSONArray();

                for (int i = 0; i < arrayAdapter.getCount(); i ++)
                {
                    if (listView.getCheckedItemPositions().get(i))
                    {
                        storedData.put(arrayAdapter.getItem(i));
                    }
                }

                editor.putString(MultipleSelectionDialog.this.sharedPreferencesKey, storedData.toString());
                editor.commit();

                Log.d(getClass().toString(), "Values set " + storedData);

                dismiss();
            }
        });
        setButton(AlertDialog.BUTTON_NEUTRAL, CNL_LABEL, new OnClickListener()
        {


            public void onClick(DialogInterface dialog,
                                int             id)
            {

                dismiss();
            }
        });

        setView(listView);
    }


    public void setDialog()
    {
        JSONArray storedData;
        int storedDataRowIndex;

        try
        {
            listView.getCheckedItemPositions().clear();

            storedData = new JSONArray(sharedPreferences.getString(sharedPreferencesKey, "[]"));

            for (int i = 0; i < storedData.length(); i ++)
            {
                if ((storedDataRowIndex = rowsData.indexOf(storedData.getString(i))) != -1)
                {
                    listView.setItemChecked(storedDataRowIndex, true);
                }
            }
        }

        catch (Exception exception)
        {
            exception.printStackTrace();
            Log.e(getClass().toString(), "Unexpected error " + exception.toString());
        }
    }
}