package uk.co.jarofgreen.progressbarthegame;

import java.security.acl.LastOwnerException;
import java.util.Random;

import android.opengl.Visibility;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;


/**
 * 
 * @author James Baster <james@jarofgreen.co.uk>
 * @copyright JMB Technology Limited
 * @license Open Source under the 3-clause BSD License
 * @url https://github.com/jarofgreen/Progress-Bar--The-Game-Android
 *  */
public class GameActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_game);
		progressBar = (ProgressBar)findViewById(R.id.progressBar);
		progressBar.setMax(100);
		handler=new Handler();
		levelTextView = (TextView)findViewById(R.id.level);
		percentTextView = (TextView)findViewById(R.id.percent);
		nextLevelButton = (Button)findViewById(R.id.nextLevel);
		randomGenerator = new Random();
		startLevel();
	}

	protected int level = 1;
	protected int timePerPercent = 100;
	protected int randomTimePerPercent = 100;
	protected ProgressBar progressBar;
	protected TextView levelTextView;
	protected TextView percentTextView;	
	protected Button nextLevelButton;
	private Random randomGenerator;
	
	Handler handler;
	final Runnable runnable = new Runnable() {
		public void run() {
			GameActivity.this.addOnePerCent();
		}
	};;
	
	
	protected void startLevel() {
		timePerPercent = level * 75;
		randomTimePerPercent = level * 30;
		levelTextView.setText("Level "+Integer.toString(level));
		progressBar.setProgress(0);
		percentTextView.setText("0%");
		handler.postDelayed(runnable, getTimeTillNextPerCent()); 
	}
	
	protected void addOnePerCent() {
		int current = progressBar.getProgress();
		Log.d("CURRENT",Integer.toString(current));
		if (current == 99) {
			Toast.makeText(this, "Congratulations!", Toast.LENGTH_LONG).show();
			progressBar.setProgress(100);
			percentTextView.setText("100%");
			nextLevelButton.setVisibility(View.VISIBLE);
		} else {
			progressBar.setProgress(current+1);
			percentTextView.setText(Integer.toString(current+1)+"%");
			handler.postDelayed(runnable, getTimeTillNextPerCent()); 
		}
	}
	
	protected int getTimeTillNextPerCent() {
		if (randomGenerator.nextBoolean()) {
			return timePerPercent + randomGenerator.nextInt(randomTimePerPercent);
		} else {
			return timePerPercent - randomGenerator.nextInt(randomTimePerPercent);
		}
	}
	
	public void nextLevel(View v) {
		nextLevelButton.setVisibility(View.INVISIBLE);
		level = level + 1;
		startLevel();
	}
}

@@ -1210,16 +1210,51 @@
 el = 1;%0A
+%09protected int currentPercent = 0;%0A
 %09protect
@@ -1670,16 +1670,38 @@
 vel() %7B%0A
+%09%09currentPercent = 0;%0A
 %09%09timePe
@@ -1825,66 +1825,24 @@
 ;%0A%09%09
-progressBar.setProgress(0);%0A%09%09percentTextView.setText(%220%25%22
+drawProgressBar(
 );%0A%09
@@ -1945,47 +1945,43 @@
 %7B%0A%09%09
-int 
 current
- = progressBar.getProgress()
+Percent = currentPercent + 1
 ;%0A%09%09
@@ -2020,17 +2020,45 @@
 (current
-)
+Percent));%0A%09%09drawProgressBar(
 );%0A%09%09if 
@@ -2069,14 +2069,22 @@
 rent
+Percent
  == 
-99
+100
 ) %7B%0A
@@ -2161,134 +2161,170 @@
 %0A%09%09%09
-progressBar.setProgress(100);%0A%09%09%09percentTextView.setText(%22100%25%22);%0A%09%09%09nextLevelButton.setVisibility(View.VISIBLE);%0A%09%09%7D else %7B%0A%09
+nextLevelButton.setVisibility(View.VISIBLE);%0A%09%09%7D else %7B%0A%09%09%09handler.postDelayed(runnable, getTimeTillNextPerCent()); %0A%09%09%7D%0A%09%7D%0A%09%0A%09protected void drawProgressBar() %7B%0A
 %09%09pr
@@ -2356,14 +2356,18 @@
 rent
-+1
+Percent
 );%0A
-%09
 %09%09pe
@@ -2416,82 +2416,22 @@
 rent
-+1)+%22%25%22);%0A%09%09%09handler.postDelayed(runnable, getTimeTillNext
 Per
-Cent()); %0A%09%09%7D
+cent)+%22%25%22);
 %0A%09%7D%0A

Move draw to one function