package com.hyperling.apps.games;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.TextView;

public class HighScoreActivity extends AppCompatActivity {

    private String sharedPreferencesKey, TAG, html;
    private String[] allBirdScoreKeys, allChickenScoreKeys;

    private SharedPreferences sharedPreferences;

    LeaderboardHelper leaderboardHelper;

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

        TAG = getString(R.string.TAG);

        // Get keys
        sharedPreferencesKey = getString(R.string.sharedPreferencesKey);
        allBirdScoreKeys = getResources().getStringArray(R.array.allBirdScoreKeys);
        allChickenScoreKeys = getResources().getStringArray(R.array.allChickenScoreKeys);

        // Shared preferences
        sharedPreferences = getSharedPreferences(sharedPreferencesKey, MODE_PRIVATE);

        // Bird stuff
        LinearLayout birdScoreLayout = (LinearLayout) findViewById(R.id.birdScoreLayout);
        loadScoreLayout(birdScoreLayout, allBirdScoreKeys);

        // Chicken stuff
        LinearLayout chickenScoreLayout = (LinearLayout) findViewById(R.id.chickenScoreLayout);
        loadScoreLayout(chickenScoreLayout, allChickenScoreKeys);
    }

    private void loadScoreLayout(LinearLayout layout, String[] scoreKeys){

        for (String key : scoreKeys){
            int score = sharedPreferences.getInt(key, 0);
            if (score > 0){
                String label = key;
                // Add spaces to string before uppercase and numbers
                label = label.replaceAll(" ", "");
                //label = label.replaceAll("0", "");
                for (int i = 1; i < label.length(); i++){
                    if (label.charAt(i) == label.toUpperCase().charAt(i) || isNumeric(label.charAt(i))){
                        label = label.substring(0, i) + " " + label.substring(i);
                        i++;
                        if (isNumeric(label.charAt(i))){
                            label = label.substring(0, i) + "- Version " + label.substring(i);
                            break;
                        }
                    }
                }

                label = label.concat(": ");
                String scoreString = "" + score;

                // Create UI Objects
                LinearLayout linearLayout = new LinearLayout(this);
                TextView tvLabel = new TextView(this);
                TextView tvScore = new TextView(this);

                // Set their text
                tvLabel.setText(label);
                tvScore.setText(scoreString);

                // Finish the layout
                linearLayout.setOrientation(LinearLayout.HORIZONTAL);
                linearLayout.addView(tvLabel);
                linearLayout.addView(tvScore);

                // Add score to layout
                layout.addView(linearLayout);
            }
        }
    }

    @Override
    protected void onResume() {
        leaderboardHelper = new LeaderboardHelper();
        super.onResume();

        Runnable getHTML = new Runnable() {
            @Override
            public void run() {
                html = leaderboardHelper.getTable("BirdScore009");
                Log.d(TAG, "HighScoreActivity.onResume():  html=" + html);
            }
        };

        Thread t = new Thread(getHTML);
        t.start();

        Log.d(TAG, "HighScoreActivity.onResume()  html=" + html);
    }

    private boolean isNumeric(char c){
        try{
            String s = "" + c;
            int i = Integer.parseInt(s);
            if (i >= 0) {
                return true;
            }
        }
        catch (Exception e){
            //e.printStackTrace();
        }

        return false;
    }
}
