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

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

import edu.whs.spin2018.baoapp.R;
import edu.whs.spin2018.baoapp.utilities.SharedPrefUtils;
import edu.whs.spin2018.baoapp.utilities.ViewUtils;

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

    private OnNameGesetztListener mListener;

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

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment NamensabfrageDialog.
     */
    public static NamensabfrageDialog newInstance() {
        NamensabfrageDialog fragment = new NamensabfrageDialog();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.DialogAbgerundeteEcken);
        setCancelable(false);
    }

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

        TextView tvNamenseingabe = v.findViewById(R.id.dialog_name_et_benutzername);
        Button btnSpeichern = v.findViewById(R.id.dialog_name_btn_speichern);
        ViewUtils.setzeSchatten(btnSpeichern);

        btnSpeichern.setOnClickListener((view) -> {
            String name = tvNamenseingabe.getText().toString();

            if (istNameGueltig(name)) {
                Toast.makeText(getActivity(), getString(R.string.name_gespeichert),
                        Toast.LENGTH_SHORT).show();
                SharedPrefUtils.setzeBenutzernamen(inflater.getContext(),
                        name);
                mListener.onNameGesetzt(name);
                dismiss();
            }
        });

        return v;
    }

    private boolean istNameGueltig(String text) {
        return !TextUtils.isDigitsOnly(text) && !TextUtils.isEmpty(text);
    }


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

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

    /**
     * Interface zur Kommunikation mit der Hauptmenue Aktivitaet
     */
    public interface OnNameGesetztListener {

        /**
         * Benachrichtigt die Aktivitaet, welche das Interface implementiert, dass der Name geandert
         * wurde.
         *
         * @param name - neuer Name
         */
        void onNameGesetzt(String name);
    }
}
