package com.example.phoenix.quizgameapp;

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

import java.util.ArrayList;


public class MainActivity extends AppCompatActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {


    RadioButton rBtn1, rBtn2;
    RadioGroup rg;
    Button next, replay;

    ArrayList<Integer> scoreR;
    ArrayList<Integer> scoreW;

    int vl = 0;


    TextView tvQn, tvAns;

    String[] qns;
    int[] answers;
    int count = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Resources rs = getResources();
        qns = rs.getStringArray(R.array.questions);

        scoreR = new ArrayList<Integer>();
        scoreW = new ArrayList<Integer>();


        answers = rs.getIntArray(R.array.questionsAnswers);
        tvQn = (TextView) findViewById(R.id.txtQn);
        tvAns = (TextView) findViewById(R.id.qnAnsr);

        tvQn.setText(qns[0]);

        rBtn1 = (RadioButton) findViewById(R.id.ansA);
        rBtn2 = (RadioButton) findViewById(R.id.ansB);

        rBtn1.setText(""+answers[0]);
        rBtn2.setText(""+41);

        rg = (RadioGroup) findViewById(R.id.rg);
        rg.setOnCheckedChangeListener(this);

        rBtn1.setSelected(false);
        rBtn2.setSelected(false);

        next = (Button) findViewById(R.id.next);

        replay = (Button) findViewById(R.id.replay);
        replay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent restart = new Intent(MainActivity.this, MainActivity.class);
                startActivity(restart);
            }
        });

        next.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        setNext();
        int size = scoreR.size();
        if(size < 10){
            scoreR.add(vl);
            rBtn1.clearFocus();
            rBtn2.clearFocus();
            rBtn1.setChecked(false);
            rBtn2.setChecked(false);
        }
        else {

            Toast.makeText(getApplicationContext(), "Game Over: ", Toast.LENGTH_LONG).show();
            compareArrays(answers, scoreR);
        }

    }

    void setNext(){
        String vl = "";
        int ansA = 0;
        if (count <= qns.length){
            ++count;

            if (count < qns.length){
                vl = qns[count];
                ansA = answers[count];
                tvQn.setText(vl);

                if (count == 1){
                    rBtn1.setText(""+ansA);
                    rBtn2.setText(""+3);
                }
                else if (count == 2){
                    rBtn1.setText(""+661);
                    rBtn2.setText(""+ansA);
                }
                else if (count == 3){
                    rBtn1.setText(""+ansA);
                    rBtn2.setText(""+45);
                }
                else if (count == 4){
                    rBtn1.setText(""+42);
                    rBtn2.setText(""+ansA);
                }
                else if (count == 5){
                    rBtn1.setText(""+ansA);
                    rBtn2.setText(""+55);
                }
                else if (count == 6){
                    rBtn1.setText(""+60);
                    rBtn2.setText(""+ansA);
                }
                else if (count == 7){
                    rBtn1.setText(""+60);
                    rBtn2.setText(""+ansA);
                }
                else if (count == 8){
                    rBtn1.setText(""+ansA);
                    rBtn2.setText(""+450);
                }
                else if (count == 9){
                    rBtn1.setText(""+60);
                    rBtn2.setText(""+ansA);
                }
                else if (count == 10){
                    rBtn1.setText(""+60);
                    rBtn2.setText(""+ansA);
                }







            }

        }
    }


    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        int selectedId = rg.getCheckedRadioButtonId();

        RadioButton rd = (RadioButton) findViewById(selectedId);

        vl = Integer.parseInt(rd.getText().toString());
    }

    public void compareArrays(int[] scores, ArrayList<Integer> correctAns){

        boolean b = true;

        if (scores != null && correctAns != null){
            int ct = 0;
            for (int i = 0; i < correctAns.size(); i++){
                if (scores[i] == correctAns.get(i)){
                    b = true;
                    ct += 1;

                }
            }
            int totalScore = ct * 10;
            tvAns.setText(""+totalScore+"%");
            replay.setVisibility(View.VISIBLE);

        }
        else {
            b = false;
        }


    }
}
