package com.aemreunal.audiorecorder;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.aemreunal.audiorecorder.model.Recorder;
import com.aemreunal.audiorecorder.model.RecorderController;


public class RootActivity extends Activity implements RecorderController {
    private Button recordButton;
    private EditText durationTextField;
    private EditText jobNameTextField;

    private Recorder recorder;
    private long durationInMS;
    private String jobName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_root);
        recordButton = (Button) findViewById(R.id.recordButton);
        durationTextField = (EditText) findViewById(R.id.durationEditText);
        jobNameTextField = (EditText) findViewById(R.id.jobNameEditText);
    }

    public void recordButtonTapped(View view) {
        if (recorder == null) {
            createRecorder();
            durationTextField.setEnabled(false);
            jobNameTextField.setEnabled(false);
        }

        if (!recorder.isRecording()) {
            recorder.startRecording();
            switchToRecordingState();
        } else {
            recorder.stopRecording();
            switchToReadyToRecordState();
        }
    }

    private void createRecorder() {
        // Check duration
        String durationInMSAsString = durationTextField.getText().toString();
        if (durationInMSAsString == null || durationInMSAsString == "" || durationInMSAsString.length() == 0) {
            showDurationError();
            durationInMSAsString = getString(R.string.defaultDurationText);
        }
        durationInMS = Integer.parseInt(durationInMSAsString) * 1000; // * 1000 to convert from seconds to ms

        // Check job name
        jobName = jobNameTextField.getText().toString();
        if (jobName == null || jobName == "" || jobName.length() == 0) {
            jobName = "job";
        }

        recorder = new Recorder(this, jobName, durationInMS);
    }

    private void showDurationError() {
        Toast.makeText(this, "Please enter a valid duration value between 1 and 180. Defaulting to 5 seconds.", Toast.LENGTH_LONG).show();
    }

    public void switchToRecordingState() {
        recordButton.setText(R.string.stopRecording);
    }

    public void switchToReadyToRecordState() {
        recordButton.setText(R.string.startRecording);
    }

    public void updateTimerDisplay(long remainingTime) {
        System.out.println("Timer fired! Remaining time: " + remainingTime);
    }

    public void saveRecording(String filePath) {
        // Do something with the file.
        Toast.makeText(this, "File saved as: " + filePath, Toast.LENGTH_LONG).show();
    }
}

@@ -1088,24 +1088,136 @@
 Recorder();%0A
+            if(durationInMS == -1) %7B%0A                showDurationError();%0A                return;%0A            %7D%0A
             
@@ -1604,16 +1604,43 @@
 uration%0A
+        durationInMS = -1;%0A
         
@@ -1742,26 +1742,26 @@
 sString 
-=
+!
 = null 
-%7C%7C
+&&
  duratio
@@ -1774,24 +1774,24 @@
 sString 
-=
+!
 = %22%22 
-%7C%7C
+&&
  duratio
@@ -1805,33 +1805,33 @@
 String.length() 
-=
+!
 = 0) %7B%0A         
@@ -1837,104 +1837,113 @@
     
-showD
+int d
 uration
-Error();%0A            durationInMSAsString = getString(R.string.defaultDurationText);
+ = Integer.parseInt(durationInMSAsString);%0A            if(duration %3E= 1 && duration %3C= 180) %7B
 %0A   
@@ -1939,34 +1939,32 @@
  180) %7B%0A        
-%7D%0A
         duration
@@ -1974,46 +1974,16 @@
 S = 
-Integer.parseInt(durationInMSAsString)
+duration
  * 1
@@ -2026,16 +2026,104 @@
 ds to ms
+%0A            %7D%0A        %7D%0A%0A        if(durationInMS == -1) %7B%0A            return;%0A        %7D
 %0A%0A      
@@ -2508,33 +2508,8 @@
 180.
- Defaulting to 5 seconds.
 %22, T

Check duration validity