package com.javonharper.rhythm;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.graphics.drawable.TransitionDrawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.devadvance.circularseekbar.CircularSeekBar;
import com.devadvance.circularseekbar.CircularSeekBar.OnCircularSeekBarChangeListener;

public class RhythmActivity extends Activity {
	private boolean active = false;
	private Timer timer = null;
	private long MILLISECONDS_IN_A_MINUTE = 60000L;
	MediaPlayer player = null;
	Vibrator vibes;
	public static long START_TRANSITION_DURATION = 2000;
	public static long END_TRANSITION_DURATION = START_TRANSITION_DURATION / 5;
	CircularSeekBar seekbar;
	private Integer INITIAL_BPM = 110;
	private Integer currentBpm = INITIAL_BPM;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		player = MediaPlayer.create(getApplicationContext(), R.raw.woodblock);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_rhythm);
		vibes = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
		seekbar = (CircularSeekBar) findViewById(R.id.bpmSeekbar);
		seekbar.setOnSeekBarChangeListener(new CircleSeekBarListener());
		changeBpmText(INITIAL_BPM.toString());
	}

	@Override
	protected void onStart() {
		super.onStart();
		active = false;
		initializeFonts();
	}

	@Override
	protected void onStop() {
		super.onStop();
		stopMetronome();
	}

	public void toggleMetronome(View view) {
		vibrate();
		if (active) {
			startMetronome();
		} else {
			stopMetronome();
		}
	}

	private void stopMetronome() {
		changeButtonText("Stop");
		resetBackground();
		startMetronomeTimer();
	}

	private void startMetronome() {
		changeButtonText("Start");
		saturateBackground();
		stopMetronomeTimer();
	}

	private void startMetronomeTimer() {
		active = true;

		long interval = getBpmInterval();
		timer = new Timer("metronome", true);
		timer.scheduleAtFixedRate(new TimerTask() {

			@Override
			public void run() {
				player.start();
			}
		}, 0, interval);
	}

	private void stopMetronomeTimer() {
		active = false;

		if (timer != null) {
			timer.cancel();
		}
	}

	private void changeButtonText(String text) {
		Button startStopButton = (Button) findViewById(R.id.start_stop_button);
		startStopButton.setText(text);
	}

	private void changeBpmText(String text) {
		TextView bpmTextView = (TextView) findViewById(R.id.bpmTextView);
		bpmTextView.setText(text);
	}

	private long getBpmInterval() {
		return MILLISECONDS_IN_A_MINUTE / getBpm();
	}

	private long getBpm() {
		return currentBpm;
	}

	private void initializeFonts() {
		Typeface font = Typeface.createFromAsset(getAssets(),
				"SourceSansPro-Light.ttf");

		TextView bpmTextView = (TextView) findViewById(R.id.bpmTextView);
		bpmTextView.setTypeface(font);

		TextView startStopButton = (TextView) findViewById(R.id.start_stop_button);
		startStopButton.setTypeface(font);
	}

	private void vibrate() {
		vibes.vibrate(50);
	}

	private void saturateBackground() {
		View view = (View) findViewById(R.id.appView);
		TransitionDrawable background = (TransitionDrawable) view
				.getBackground();
		background.startTransition((int) START_TRANSITION_DURATION);
	}

	private void resetBackground() {
		View view = (View) findViewById(R.id.appView);
		TransitionDrawable background = (TransitionDrawable) view
				.getBackground();
		background.reverseTransition((int) END_TRANSITION_DURATION);
	}

	public class CircleSeekBarListener implements
			OnCircularSeekBarChangeListener {
		@Override
		public void onStopTrackingTouch(CircularSeekBar seekBar) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onStartTrackingTouch(CircularSeekBar seekBar) {
			// TODO Auto-generated method stub
		}

		@Override
		public void onProgressChanged(CircularSeekBar circularSeekBar,
				int progress, boolean fromUser) {
			changeBpmText(Integer.toString(progress));
		}
	}
}

@@ -907,16 +907,43 @@
 eekbar;%0A
+%09%0A%09// Settings for the bpm%0A
 %09private
@@ -991,34 +991,263 @@
 ger 
-currentBpm = INITIAL_BPM;%0A
+MINIMUM_BPM = 40;%0A%09private Integer MAXIMUM_BPM = 208;%0A%09%0A%09// Offsets for the progress bar%0A%09private Integer INITIAL_BPM_PROGRESS = INITIAL_BPM - MINIMUM_BPM;%0A%09private Integer MAXIMUM_BPM_PROGRESS = MAXIMUM_BPM - MINIMUM_BPM;%0A%09%0A%09private Integer currentBpm;%0A%09
 %0A%09@O
@@ -1659,24 +1659,62 @@
 ());
+%09%09
 %0A%09%09
-changeBpmText
+seekbar.setMax(MAXIMUM_BPM_PROGRESS);%0A%09%09updateBpm
 (INI
@@ -1725,19 +1725,17 @@
 _BPM
-.toString()
+_PROGRESS
 );%0A%09
@@ -3069,24 +3069,165 @@
 rrentBpm;%0A%09%7D
+%0A%09%0A%09private void updateBpm(int progressValue) %7B%0A%09%09currentBpm = progressValue + MINIMUM_BPM;%0A%09%09changeBpmText(Integer.toString(currentBpm));%0A%09%7D
 %0A%0A%09private v
@@ -4518,38 +4518,18 @@
 %0A%09%09%09
-changeBpmText(Integer.toString
+%09updateBpm
 (pro
@@ -4534,17 +4534,16 @@
 rogress)
-)
 ;%0A%09%09%7D%0A%09%7D

Use offsetted bpm to start at 40 and end at 208