package com.bxl.eventtest;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.FragmentManager;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;

public class ListDialogFragment extends DialogFragment {
	
	private static final String TAG = ListDialogFragment.class.getSimpleName();
	
	static final String KEY_TITLE = "title";
	static final String KEY_ITEMS = "items";
	static final String KEY_BT_NAME = "btName";
	
	static int dialogType = 100;
	
	static void showDialog(FragmentManager manager, String title, String[] items, String[] btName) {
		ListDialogFragment listDialogFragment = new ListDialogFragment();
		Bundle args = new Bundle();
		args.putString(KEY_TITLE, title);
		args.putStringArray(KEY_ITEMS, items);
		args.putStringArray(KEY_BT_NAME, btName);
		listDialogFragment.setArguments(args);
		
		if(title.equals("PDF file list")){
			dialogType = 101;
		}else if(title.equals("Searched Printers")){
			dialogType = 102;
		}
		listDialogFragment.show(manager, TAG);
	}
	
	interface OnClickListener {
		public void onClick(String item);
	}
	
	private OnClickListener onClickListener;
	private String item;
	private String btName;
	
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		onClickListener = (OnClickListener) activity;
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		Bundle args = getArguments();
		String title = args.getString(KEY_TITLE);
		final String[] items = args.getStringArray(KEY_ITEMS);
		final String[] btNames = args.getStringArray(KEY_BT_NAME);
		
		if(dialogType == 100){
			AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
			builder.setTitle(title).setSingleChoiceItems(btNames, -1, new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					item = items[which];
				}
			}).setPositiveButton("OK", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					onClickListener.onClick(item);
				}
			}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					
				}
			});
			
			return builder.create();
		}else if(dialogType == 101){
			AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
			builder.setTitle(title).setItems(items, new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					onClickListener.onClick(items[which]);
				}
			});
			return builder.create();
		}else{
			AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
			builder.setTitle(title).setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					item = items[which];
				}
			}).setPositiveButton("OK", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					onClickListener.onClick(item);
				}
			}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					
				}
			});
			
			return builder.create();
		}
		
	}
}
