package com.upmoon.alex.campchamptrivia;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class HighScoreActivity extends AppCompatActivity {

    private int[] mHighScores = new int[10];

    private int mPlayerScore, mQuizID;

    private static final String TAG = "HIGH SCORE ACTIVITY";

    private static final String EXTRA_QUIZ_NUMBER =
            "com.upmoon.alex.campchamptrivia.quiz_number";

    private static final String PLAYER_SCORE =
            "com.upmoon.alex.campchamptrivia.player_score";

    private Button mShareScore;

    public static Intent newIntent(Context packageContext, int quizNum, int newScore){
        Intent i = new Intent(packageContext, QuizActivity.class);
        i.putExtra(EXTRA_QUIZ_NUMBER,quizNum);
        i.putExtra(PLAYER_SCORE, newScore);
        return i;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_high_score);
        Log.d(TAG, "HighScoreActivity onCreate() called");

        mShareScore = (Button) findViewById(R.id.shareButton);

        mPlayerScore = Integer.parseInt(PLAYER_SCORE);

        mQuizID = Integer.parseInt(EXTRA_QUIZ_NUMBER);

        mHighScores = getExistingHighScores();



        mShareScore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String mShareMessage = "I just scored a " + Integer.toString(mPlayerScore) + " on quiz " + Integer.toString(mQuizID) + " in CampChampTrivia!";
                Intent share = new Intent(Intent.ACTION_SEND);
                share.setType("text/plain"); // might be text, sound, whatever
                share.putExtra(Intent.EXTRA_STREAM, mShareMessage);
                startActivity(Intent.createChooser(share, "share"));
            }
        });
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG, "onStop() called");
        setHighScores(mHighScores);
    }

    private int[] getExistingHighScores() {
        int[] highScores = new int[10];
        FileInputStream fis;

        // High scores are stored in a file highScores.txt
        String FILENAME = "highScores";

        // Try to open highScores.txt in a FileInputStream
        try {
            fis = openFileInput(FILENAME);

            if(fis != null) {
                InputStreamReader inputStreamReader = new InputStreamReader(fis);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                String receptacle = "";

                // Load high scores from memory.
                for (int i = 0; i < 10; i++) {
                    if((receptacle = bufferedReader.readLine()) != null) {
                        highScores[i] = Integer.parseInt(receptacle);
                    } else {
                        // If there are no more high scores stored in the file, load zeros into array.
                        highScores[i] = 0;
                    }
                }

                // Close inputFileStream
                fis.close();
            }
        } catch (Exception exception) {
            Log.d(TAG, "Error: Unable to open file, FILENAME");
        }

        // Return highScores array
        return highScores;
    }

    private void setHighScores(int[] scores) {
        FileOutputStream fos;

        // High scores are stored in a file highScores.txt
        String FILENAME = "highScores";

        try {
            fos = new FileOutputStream(FILENAME);
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fos);
            for (int i = 0; i < 10; i++) {
                // write scores[i] to memory.
                outputStreamWriter.write(scores[i]);
            }
            outputStreamWriter.close();
        } catch (Exception exception) {
            Log.d(TAG, "Error: Unable to save high scores.");
        }

        return;
    }
}

@@ -253,16 +253,45 @@
 .Button;
+%0Aimport android.widget.Toast;
 %0A%0Aimport
@@ -1634,16 +1634,1056 @@
 res();%0A%0A
+        // Check if the player score has made the high scores list%0A        int tempHold = 0, tempHold2 = 0;%0A        for(int i = 0; i %3C 10; i++) %7B%0A            // If playerScore made the highScores list, bump the rest of the high scores down the list.%0A            if(tempHold != 0) %7B%0A                tempHold2 = mHighScores%5Bi%5D;%0A                mHighScores%5Bi%5D = tempHold;%0A                tempHold = tempHold2;%0A            %7D else if (mPlayerScore %3E mHighScores%5Bi%5D) %7B%0A                // Player has made the high scores list! Maybe a toast is in order?%0A                String success = %22Congratulations, you are now number %22 + Integer.toString(i) + %22 on the high score list!%22;%0A                Toast.makeText(HighScoreActivity.this,success,Toast.LENGTH_SHORT).show();%0A%0A                // Replace the high score with player score.%0A                tempHold = mHighScores%5Bi%5D;%0A                mHighScores%5Bi%5D = mPlayerScore;%0A            %7D else %7B%0A                // do nothing.%0A            %7D%0A        %7D%0A%0A        // TODO Populate GridView with mHighScores
 %0A%0A      
@@ -2804,24 +2804,135 @@
 k(View v) %7B%0A
+                // What does shareButton do if user has come here from ChoiceActivity instead of QuizActivity?%0A
             

Check if playerScore is a high score, if it is, add it to the array. highScores array must be in descending order.