package jp.gihyo.mitoma.android.p012watchword;

import android.app.Activity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.util.Random;

public class WatchwordAction {

    private Activity act = null;
    private String[] watchwordArray=null;
    private int questionNo = 0;
    public WatchwordAction(Activity act){
        this.act = act;
        this.watchwordArray = act.getResources().getStringArray(R.array.watchword_array);

    }
    public void exec(View view){
        String strQuestion = "";
        String strAnswer = "";
        String strJudge = "";
        TextView tvQuestion = act.findViewById(R.id.tvQuestion);
        EditText etAnswer = act.findViewById(R.id.etAnswer);
        TextView tvJudge = act.findViewById(R.id.tvJudge);

        String buttonTag =(String) view.getTag();
        if(buttonTag.equals("QUESTION")){
            this.questionNo = new Random().nextInt(this.watchwordArray.length);
            strQuestion = this.watchwordArray[this.questionNo].split(",")[0];
        }else if(buttonTag.equals("ANSWER")){
            strAnswer = etAnswer.getText().toString();
            strQuestion = this.watchwordArray[this.questionNo].split(",")[0];
            if(strAnswer.equals(this.watchwordArray[this.questionNo].split(",")[1])){
                strJudge = "正解";
            }else{
                strJudge = "不正解";
            }
        }
        tvQuestion.setText(strQuestion);
        etAnswer.setText(strAnswer);
        tvJudge.setText(strJudge);
    }
}
