package de.thomas.dreja.ec.musicquiz.gui.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import de.thomas.dreja.ec.musicquiz.R;
import de.thomas.dreja.ec.musicquiz.ctrl.PlaylistResolver.SortOrder;

public class SortOrderSelectionDialog extends DialogFragment {

	private SortOrderSelectionListener listener;
	
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		
		String[] items = new String[SortOrder.values().length];
		
		for(int i=0; i<items.length; i++) {
			items[i] = SortOrder.values()[i].getName(getResources());
		}
		
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
	    builder.setTitle(R.string.sort_by)
	           .setItems(items, new DialogInterface.OnClickListener() {
	               public void onClick(DialogInterface dialog, int which) {
	            	   listener.onSortOrderSelected(SortOrder.values()[which]);
	           }
	    });

	    return builder.create();
	}

	public interface SortOrderSelectionListener {
		public void onSortOrderSelected(SortOrder order);
	}
	
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		
		// Verify that the host activity implements the callback interface
        try {
            // Instantiate the NoticeDialogListener so we can send events to the host
            listener = (SortOrderSelectionListener) activity;
        } catch (ClassCastException e) {
            // The activity doesn't implement the interface, throw exception
            throw new ClassCastException(activity.toString()
                    + " must implement SortOrderSelectionListener");
        }
	}
}
