package com.ware.alex.upanddown;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

public class ResultsActivity extends AppCompatActivity {
    private static ArrayList<String> playerNames = new ArrayList<>();
    private static ArrayList<Integer> playerScores = new ArrayList<>();

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

        Intent intent = getIntent();
        playerNames = intent.getStringArrayListExtra("player_names");
        playerScores = intent.getIntegerArrayListExtra("player_scores");

        generateLeaderBoard();
    }

    private void generateLeaderBoard() {
        int playerNum = playerNames.size();
        for(int i = 0; i < playerNum; i++) {
            addHighest();
        }
    }

    private void addHighest() {
        int index = 0;
        int highScore = 0;

        for(int i = 0; i < playerScores.size(); i++) {
            if (playerScores.get(i) > highScore) {
                index = i;
                highScore = playerScores.get(i);
            }
        }

        String result = playerScores.get(index) + " - " + playerNames.get(index);
        playerScores.remove(index);
        playerNames.remove(index);

        TextView playerText = new TextView(this);
        playerText.setText(result);
        playerText.setTextColor(Color.BLACK);
        playerText.setTextSize(20);
        playerText.setGravity(Gravity.CENTER_HORIZONTAL);

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(0,10,0,10);
        playerText.setLayoutParams(layoutParams);

        LinearLayout playerResultLayout = (LinearLayout) findViewById(R.id.playerResultLayout);
        playerResultLayout.addView(playerText);
    }
}