package app.mar.activities;

import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import app.mar.game.challenges.Challenge;
import app.mar.game.challenges.Question;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * to handle interaction events.
 * Use the {@link QuestionFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class QuestionFragment extends ChallengeFragment  {
    protected ImageView mUIImage;
    protected EditText  mUIResponse;
    protected TextView  mUIQuestion;
    protected Question  mQuestion=null;


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

    public static QuestionFragment newInstance() {
        QuestionFragment fragment = new QuestionFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = super.onCreateView(inflater, container, R.layout.fragment_question);
        mUIImage = (ImageView) v.findViewById(R.id.iv_img);
        mUIQuestion = (TextView) v.findViewById(R.id.tv_question);
        mUIResponse = (EditText) v.findViewById(R.id.et_response);
        return v;
    }


    @Override
    public boolean check() {
        String resp = mUIResponse.getText().toString();
        if(mQuestion.checkAnswer(resp))
        {
            Toast.makeText(getActivity(), mQuestion.getGoodMessage(), Toast.LENGTH_LONG).show();
            mUIResponse.getText().clear();
            return true;
        }else
        {
            Toast.makeText(getActivity(), mQuestion.getBadMessage(), Toast.LENGTH_LONG).show();
            mUIResponse.getText().clear();
            return false;
        }
    }


    @Override
    protected void onInitChallenge(Challenge c) {
        if(c instanceof Question)
        {
            mQuestion=(Question)c;
            mUIQuestion.setText(mQuestion.getQuestion());
        }
        else
        {
            Toast.makeText(getActivity(), "Error l'entrée n'est pas une question ! ", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    protected void onResetUi() {
        mUIResponse.setHint("Entrer votre réponse");
    }
}
