package cisc181.myviewzoo;

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

public class SecondActivity extends AppCompatActivity {

  Intent startI;

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

    startI = getIntent();

    TextView tv = (TextView) findViewById(R.id.my_answer_text);
    tv.setText(startI.getStringExtra("answer"));
  }

  public void finishActivity(View V) {

    int len = startI.getStringExtra("answer").length();

    SharedPreferences sharedPref = getSharedPreferences("HighScore", Context.MODE_PRIVATE);

    int defaultValue = 0;
    int highScore = sharedPref.getInt("saved_high_score", defaultValue);

    if (len > highScore) {
      highScore = len;
      SharedPreferences.Editor editor = sharedPref.edit();
      editor.putInt("saved_high_score", highScore);
      editor.commit();
    }

    startI.putExtra("answerLength", len);
    startI.putExtra("longestLength", highScore);

    setResult(RESULT_OK, startI);

    finish();
  }
 }
