package net.heartshield.prevent;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import net.heartshield.sensors.IntensityDetector;
import net.heartshield.sensors.SensorFactory;

/**
 * @author David Madl (git@abanbytes.eu)
 * @date 2017-05-14
 */
public class CameraTestActivity extends Activity {
    private HandlerThread mBackgroundThread;
    private Handler mBackgroundHandler;
    private Runnable mTask;
    private IntensityDetector mIntensitySensor;
    private TextView mText;

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

        mText = (TextView) findViewById(R.id.textView);
        mIntensitySensor = new SensorFactory(this).getIntensityDetector(null);
    }

    @Override
    protected void onResume() {
        super.onResume();
        startBackgroundThread();
    }

    @Override
    protected void onPause() {
        super.onPause();
        stopBackgroundThread();
    }

    private void postText(final String text) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mText.setText(text);
            }
        });
    }

    private boolean started = false;

    public void start(View view) {
        if(started) {
            postText("already started");
            return;
        }
        started = true;
        postText("mIntensitySensor.start()");

        mIntensitySensor.setDebugMode(true);
        mIntensitySensor.start();

        mTask = new Runnable() {
            @Override
            public void run() {
                try {
                    myRun();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            private void myRun() throws InterruptedException {
                Thread.sleep(3000);
                postText("mIntensitySensor.lock()");
                mIntensitySensor.lock();

                Thread.sleep(3000);
                postText("mIntensitySensor.stop()");
                mIntensitySensor.stop();
            }
        };
        mBackgroundHandler.post(mTask);
    }

    public void stop(View view) {
        postText("mIntensitySensor.stop()");
        mIntensitySensor.stop();
        mBackgroundThread.interrupt();
        stopBackgroundThread();
        startBackgroundThread();
        started = false;
    }

    private void startBackgroundThread() {
        mBackgroundThread = new HandlerThread("MeasureBackground");
        mBackgroundThread.start();
        mBackgroundHandler = new Handler(mBackgroundThread.getLooper());
    }

    private void stopBackgroundThread() {
        if(mBackgroundThread != null) {
            mBackgroundThread.quitSafely();
            try {
                mBackgroundThread.join();
                mBackgroundThread = null;
                mBackgroundHandler = null;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}
