package at.fhtw.mcs.controller;

import java.net.URL;
import java.util.ResourceBundle;

import at.fhtw.mcs.model.Track;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Button;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextArea;
import javafx.scene.control.ToggleGroup;
import javafx.scene.text.Text;

/**
 * Controller class for Track.fxml
 */
public class TrackController implements Initializable {
	@FXML
	private Text textTrackName;
	@FXML
	private Text textDynamicRange;
	@FXML
	private LineChart<Number, Number> lineChartWaveform;
	@FXML
	private RadioButton radioButtonActiveTrack;
	@FXML
	private Button buttonMoveUp;
	@FXML
	private Button buttonMoveDown;
	@FXML
	private Button buttonDelete;
	@FXML
	private TextArea textAreaComment;

	private ToggleGroup toggleGroup;
	private Track track;

	public TrackController(Track track, ToggleGroup toggleGroup) {
		this.track = track;
		this.toggleGroup = toggleGroup;
	}

	@Override
	public void initialize(URL arg0, ResourceBundle arg1) {
		int offset = Math.round(track.getLength() / 2000);
		System.out.println("Offset: " + offset);

		textTrackName.setText(track.getFilename());
		textDynamicRange.setText(String.format("%.2f dB", track.getDynamicRange()));

		XYChart.Series<Number, Number> series = new XYChart.Series<Number, Number>();

		float[] tempData = track.getAudioData();

		int audioFileLength = track.getLength();
		int x = 0;

		/**
		 * TODO momentan ist die Zeichnung auf 2000 Werte begrenzt, bei längeren
		 * Files kommt dadurch kein schönes Bild zustande, müssen wir uns noch
		 * anschauen
		 */

		for (int j = 0; j < audioFileLength * track.getNumberOfChannels(); j++) {

			if (j % offset == 0) {
				float mean = 0;
				float leftChannel = tempData[j];

				if (track.getNumberOfChannels() == 2) {
					float rightChannel = tempData[j + 1];
					mean = (leftChannel + rightChannel) / 2;
				} else {
					mean = leftChannel;
				}
				series.getData().add(new XYChart.Data<Number, Number>(x, mean));
				x++;
			}
		}

		lineChartWaveform.getData().add(series);

		NumberAxis xAxis = (NumberAxis) lineChartWaveform.getXAxis();
		xAxis.setAutoRanging(false);
		xAxis.setUpperBound(x - 1);
		NumberAxis yAxis = (NumberAxis) lineChartWaveform.getYAxis();
		yAxis.setAutoRanging(false);
		yAxis.setLowerBound(-1.0);
		yAxis.setUpperBound(1.0);

		radioButtonActiveTrack.setToggleGroup(toggleGroup);
		radioButtonActiveTrack.setUserData(track);
		if (toggleGroup.getSelectedToggle() == null) {
			radioButtonActiveTrack.setSelected(true);
		}

		textAreaComment.textProperty().addListener(new ChangeListener<String>() {
			public void changed(ObservableValue<? extends String> value, String previousComment, String newComment) {
				track.setComment(newComment);
			}
		});
	}

	public Button getButtonMoveUp() {
		return this.buttonMoveUp;
	}

	public Button getButtonMoveDown() {
		return this.buttonMoveDown;
	}

	public Button getButtonDelete() {
		return this.buttonDelete;
	}

	public RadioButton getRadioButtonActiveTrack() {
		return this.radioButtonActiveTrack;
	}
}

@@ -1843,16 +1843,16 @@
 int 
-j
+i
  = 0; 
-j
+i
  %3C a
@@ -1901,43 +1901,23 @@
 (); 
-j++) %7B%0A%0A%09%09%09if (j %25
+i +=
  offset
- == 0
 ) %7B%0A
-%09
 %09%09%09f
@@ -1931,17 +1931,16 @@
 an = 0;%0A
-%09
 %09%09%09float
@@ -1967,14 +1967,13 @@
 ata%5B
-j
+i
 %5D;%0A%0A
-%09
 %09%09%09i
@@ -2015,17 +2015,16 @@
 ) %7B%0A%09%09%09%09
-%09
 float ri
@@ -2045,17 +2045,17 @@
 empData%5B
-j
+i
  + 1%5D;%0A%09
@@ -2053,25 +2053,24 @@
 i + 1%5D;%0A%09%09%09%09
-%09
 mean = (left
@@ -2101,17 +2101,16 @@
 / 2;%0A%09%09%09
-%09
 %7D else %7B
@@ -2110,17 +2110,16 @@
  else %7B%0A
-%09
 %09%09%09%09mean
@@ -2137,20 +2137,18 @@
 nel;%0A%09%09%09
-%09
 %7D%0A
-%09
 %09%09%09serie
@@ -2211,17 +2211,16 @@
 ));%0A
-%09
 %09%09%09x++;%0A
 %09%09%09%7D
@@ -2215,21 +2215,16 @@
 %09%09%09x++;%0A
-%09%09%09%7D%0A
 %09%09%7D%0A%0A%09%09l

simplify paint loop