package macampcorp.macamp.activities.fragments;

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

import java.util.ArrayList;

import macampcorp.macamp.activities.R;
import macampcorp.macamp.game.Game;
import macampcorp.macamp.game.challenges.Challenge;
import macampcorp.macamp.game.challenges.QCM;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * to handle interaction events.
 * Use the {@link QCMFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class QCMFragment extends ChallengeFragment {
    protected ImageView mUIImage;
    protected TextView mUIQuestion;
    protected RadioGroup mUIGoupQCM;
    protected ArrayList<String> mChoices;

    protected int           mIdOffset = 1234;

    protected Game mGame;
    protected QCM mQCM;
    public static final int REQUEST_CODE = 2000;


    public QCMFragment() {

    }


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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = super.onCreateView(inflater, container, R.layout.fragment_qcm);

        mUIImage = (ImageView) v.findViewById(R.id.iv_img);
        mUIQuestion = (TextView) v.findViewById(R.id.tv_question);
        mUIGoupQCM = (RadioGroup) v.findViewById(R.id.rg_qcm);
        return v;
    }



    @Override
    public int check() {
        int x = mUIGoupQCM.getCheckedRadioButtonId()-mIdOffset;

        if(x<0 || x>=mChoices.size())
        {
            Toast.makeText(getActivity(), R.string.no_response, Toast.LENGTH_SHORT).show();
            return Challenge.NO_RESPONSE;
        }else
        {
            String resp = mChoices.get(x);
            if(mQCM.checkAnswer(resp))
            {
                if(mQCM.getGoodMessage()!=null)
                    Toast.makeText(getActivity(), mQCM.getGoodMessage(), Toast.LENGTH_SHORT).show();
                return Challenge.OK;
            }else
            {
                if(mQCM.getBadMessage()!=null)
                    Toast.makeText(getActivity(), mQCM.getBadMessage(), Toast.LENGTH_SHORT).show();
                return Challenge.FAIL;
            }
        }
    }



    @Override
    public void onInitChallenge(Challenge c) {
        mUIGoupQCM.removeAllViews();

        super.onInitChallenge(c);
        if (c instanceof QCM) {
            mQCM = (QCM) c;

            mUIQuestion.setText(mQCM.getQuestion());
            mChoices = mQCM.getChoices();
            for (int i = 0; i < mChoices.size(); i++) {
                RadioButton radioButton = new RadioButton(getActivity());
                radioButton.setText(mChoices.get(i));
                radioButton.setId(i + mIdOffset);
                RadioGroup.LayoutParams rprms = new RadioGroup.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, RadioGroup.LayoutParams.WRAP_CONTENT);
                rprms.gravity = 1;
                mUIGoupQCM.addView(radioButton, rprms);
            }

        } else {
            Toast.makeText(getActivity(), "Error l'entrée n'est pas une question ! : "+c.getClass().getName(), Toast.LENGTH_LONG).show();
        }
    }

    @Override
    protected void onResetUi() {
        mUIGoupQCM.clearCheck();
        getHostActivity().setVisibilityView(R.id.btn_valid, View.VISIBLE);
    }

}
