package edu.whs.spin2018.baoapp.views.menues;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import edu.whs.spin2018.baoapp.R;

/**
 * A simple {@link DialogFragment} subclass. Activities that contain this fragment must implement the
 * {@link OnDialogBestaetigtListener} interface to handle interaction events. Use the {@link
 * SpieleinstellungenWurdenVerlassenDialog#newInstance} factory method to create an instance of this
 * fragment.
 */
public class SpieleinstellungenWurdenVerlassenDialog extends DialogFragment {

    public static final String TAG = "VerlassenTAG";
    private static final String ARG_GEGNERNAME = "gegnername";
    private String gegnername;
    private OnDialogBestaetigtListener mListener;

    public SpieleinstellungenWurdenVerlassenDialog() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of this fragment using the provided
     * parameters.
     *
     * @param gegnername - Name des Gegners
     * @return A new instance of fragment SpieleinstellungenWurdenVerlassenDialog.
     */
    public static SpieleinstellungenWurdenVerlassenDialog newInstance(String gegnername) {
        SpieleinstellungenWurdenVerlassenDialog fragment = new SpieleinstellungenWurdenVerlassenDialog();
        Bundle args = new Bundle();
        args.putString(ARG_GEGNERNAME, gegnername);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.DialogAbgerundeteEcken);
        if (getArguments() != null) {
            gegnername = getArguments().getString(ARG_GEGNERNAME);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_spieleinstellungen_wurden_verlassen_dialog,
                container, false);

        Button btnZurLobby = view.findViewById(R.id.btn_zur_lobby);
        btnZurLobby.setOnClickListener((v) -> dismissAllowingStateLoss());

        TextView tvEinstellungenWurdenVerlassen = view.findViewById(R.id.tv_gegner_hat_einstellungen_verlassen);
        tvEinstellungenWurdenVerlassen.setText(String.format(getString(R.string.gegner_hat_die_einstellungen_verlassen), gegnername));
        return view;
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
        if (mListener != null) {
            mListener.onDialogBestaetigt();
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnDialogBestaetigtListener) {
            mListener = (OnDialogBestaetigtListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnDialogBestaetigtListener");
        }
    }

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

    /**
     * This interface must be implemented by activities that contain this fragment to allow an
     * interaction in this fragment to be communicated to the activity and potentially other
     * fragments contained in that activity.
     * <p>
     * See the Android Training lesson <a href= "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnDialogBestaetigtListener {

        void onDialogBestaetigt();
    }
}
