package jp.gihyo.mitoma.android.s1201dialog.fragment;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import jp.gihyo.mitoma.android.s1201dialog.R;

public class MyDialog04Fragment extends DialogFragment {
    private String[] aryHobbies = {"音楽鑑賞",  "ゲーム", "スポーツ", "その他"};
    private boolean[] checkedItems = {false,false,false,false};
    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
        builder.setTitle("あなたの趣味");
        builder.setMultiChoiceItems(aryHobbies, checkedItems,new MyDialogListener());
        builder.setPositiveButton("送信",new MyDialogButtonListener());
        Dialog dialog = builder.create();
        return dialog;
    }

    private class MyDialogListener implements DialogInterface.OnMultiChoiceClickListener {        public void onClick(DialogInterface dialog, int which,boolean isChecked) {
            checkedItems[which] = isChecked;
        }
    }
    private class MyDialogButtonListener implements DialogInterface.OnClickListener {
        public void onClick(DialogInterface dialog, int which) {
            String choiceItem = "";
            EditText et = requireActivity().findViewById(R.id.etDialogData);
            for(int i=0;i<aryHobbies.length;i++){
                if(checkedItems[i]){
                    if(!choiceItem.equals(""))choiceItem +=", ";
                    choiceItem += aryHobbies[i];
                }
            }
            et.setText(choiceItem);
            //Toast.makeText(requireActivity(), "チェック:"+choiceItem, Toast.LENGTH_LONG).show();
        }
    }
}