package com.example.countdown;

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 android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

public class EventNameDialogFragment extends DialogFragment {
    private EventNameListener listener;
    
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            listener = (EventNameListener) activity;
        }
        catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement EventNameListener");
        }
    }
    
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        LayoutInflater inflator = getActivity().getLayoutInflater();
        View layout = inflator.inflate(R.layout.custom_dialog, null);

        Dialog dialog = new AlertDialog.Builder(getActivity())
                .setView(layout)
                .setNeutralButton(android.R.string.ok,
                        new EventListener()).create();
        return dialog;
    }

    class EventListener implements DialogInterface.OnClickListener {
        @Override
        public void onClick(DialogInterface di, int which) {
            Dialog d = (Dialog) di;
            EditText editText = (EditText) d
                    .findViewById(R.id.editText);
            String eventName = editText.getText().toString();
            listener.onEventNameChosen(eventName);
        }
    }
}