package com.chaitanya.codeathon;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

@SuppressWarnings("FieldCanBeLocal")
public class StartGameActivity extends AppCompatActivity {

    final public static String EXTRA_VALUE_NAME = "message1", EXTRA_VALUE_TYPE = "message2";
    private String username, ch[];
    private Question questions[], ques;
    private int marks, i;
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start_game);
        Intent intent = getIntent();
        username = intent.getStringExtra(EXTRA_VALUE_NAME);
        int type = intent.getIntExtra(EXTRA_VALUE_TYPE,0);
        QuestionBank questionBank = new QuestionBank(type);
        questions = questionBank.getQuestions();
        nextQuestion();
    }

    private void nextQuestion() {
        if(i == 5) {
            Intent intent = new Intent(this, LastActivity.class);
            intent.putExtra(LastActivity.EXTRA_VALUE_NAME, username);
            intent.putExtra(LastActivity.EXTRA_VALUE_SCORE, marks);
            startActivity(intent);
            return;
        }
        ques = questions[i];
        textView = (TextView) findViewById(R.id.question_text);
        textView.setText(ques.getQues());
        ch = ques.getChoices();
        RadioButton radioButton = (RadioButton) findViewById(R.id.option_1);
        radioButton.setText(ch[0]);
        radioButton = (RadioButton) findViewById(R.id.option_2);
        radioButton.setText(ch[1]);
        radioButton = (RadioButton) findViewById(R.id.option_3);
        radioButton.setText(ch[2]);
        radioButton = (RadioButton) findViewById(R.id.option_4);
        radioButton.setText(ch[3]);
        i++;
    }

    private void checkAnswer(int id) {
        if (id == -1) {
            marks--;
            return;
        }
        RadioButton radioButton = (RadioButton) findViewById(id);
        if(ques.isCorrect(radioButton.getText().toString())) {
            marks += 5;
        }
        else
            marks--;

    }

    public void onSubmitClick(View view) {
        RadioGroup radioGroup = (RadioGroup) findViewById(R.id.option);
        int id = radioGroup.getCheckedRadioButtonId();
        checkAnswer(id);
        nextQuestion();
    }
}
