package epson.print.screen;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;


public class StringListSelectDialog extends DialogFragment {
    private static final String ARGS_KEY_STRING_LIST = "key-stringList";
    private OnItemSelectedListener mListener;
    private String[] mStringList;

    public interface OnItemSelectedListener {
        void onItemSelected(@NonNull String str);
    }

    @NonNull
    public static StringListSelectDialog newInstance(@NonNull String[] strArr) {
        StringListSelectDialog stringListSelectDialog = new StringListSelectDialog();
        Bundle bundle = new Bundle();
        bundle.putStringArray(ARGS_KEY_STRING_LIST, strArr);
        stringListSelectDialog.setArguments(bundle);
        return stringListSelectDialog;
    }

    @NonNull
    public Dialog onCreateDialog(@Nullable Bundle bundle) {
        Bundle arguments = getArguments();
        if (arguments != null) {
            mStringList = arguments.getStringArray(ARGS_KEY_STRING_LIST);
            if (this.mStringList != null) {
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setItems(this.mStringList, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialogInterface, int i) {
                        StringListSelectDialog.this.itemSelected(i);
                    }
                });
                setCancelable(false);
                return builder.create();
            }
            throw new IllegalArgumentException();
        }
        throw new IllegalArgumentException();
    }

    private void itemSelected(int i) {
        OnItemSelectedListener onItemSelectedListener = mListener;
        if (onItemSelectedListener != null) {
            onItemSelectedListener.onItemSelected(this.mStringList[i]);
        }
    }

    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            mListener = (OnItemSelectedListener) context;
        } catch (ClassCastException unused) {
            throw new ClassCastException(context.toString() + " must implement StringListSelectDialog.OnItemSelectedListener");
        }
    }

    public void onDetach() {
        super.onDetach();
        mListener = null;
    }
}
