package macampcorp.macamp.activities.fragments;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.app.Fragment;
import android.text.InputType;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import macampcorp.macamp.activities.HostActivity;
import macampcorp.macamp.activities.R;
import macampcorp.macamp.game.challenges.Challenge;
import macampcorp.macamp.game.challenges.Question;
import macampcorp.macamp.utils.Android;

/**
 * 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);
        mUIResponse.setOnEditorActionListener(new EditText.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    HostActivity ha = getHostActivity();
                    ha.onClickValid(null);
                    return true;
                }
                return false;
            }

        });
        return v;
    }


    @Override
    public int check() {
        String resp = mUIResponse.getText().toString();
        if(resp.length()==0)
        {
            Toast.makeText(getActivity(), R.string.no_respons_q, Toast.LENGTH_SHORT).show();
            return Challenge.NO_RESPONSE;
        }
        if(mQuestion.checkAnswer(resp))
        {
            if(mQuestion.getGoodMessage()!=null)
                Toast.makeText(getActivity(), mQuestion.getGoodMessage(), Toast.LENGTH_SHORT).show();
            mUIResponse.getText().clear();
            return Challenge.OK;
        }else
        {
            if(mQuestion.getBadMessage()!=null)
                Toast.makeText(getActivity(), mQuestion.getBadMessage(), Toast.LENGTH_LONG).show();
            mUIResponse.getText().clear();
            return Challenge.FAIL;
        }
    }

    public void onChallengeFailed()
    {
        Android.hideKeyboard(getActivity());
    }


    @Override
    protected void onInitChallenge(Challenge c) {
        super.onInitChallenge(c);
        Android.hideKeyboard(getActivity());



        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.setText("");
        mUIResponse.setHint(R.string.hint_resp);
        getHostActivity().setVisibilityView(R.id.btn_valid, View.VISIBLE);
        Android.hideKeyboard(getActivity());

    }


}
