package com.koeping.lukas.sensordatacollector;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity  {
    private boolean isRecording = false;

    private final int MY_PERMISSIONS_WRITE_EXTERNAL_STORAGE = 0;
    private Intent sensorServiceIntent;
    private String currentDate;
    private long startTime;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getWindow().addFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }


    public void startStopRecording(View view) {

        if (isRecording) {
            stopRecording();
        }
        else {
            startRecording();
        }
        changeRecordingButtonText(isRecording);
        isRecording = !isRecording;

    }


    private void startRecording() {
        boolean writing_permission = checkPermission();


        if (writing_permission) {
            startCollectingData();
        }
        else {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    MY_PERMISSIONS_WRITE_EXTERNAL_STORAGE);
        }

    }


    private void stopRecording() {
        stopService(sensorServiceIntent);

    }

    private void startCollectingData() {
        this.currentDate = Helper.getCurretDate();
        this.startTime = System.currentTimeMillis();

        sensorServiceIntent = new Intent(this, SensorService.class);
        sensorServiceIntent.putExtra("directory_name", this.currentDate);
        sensorServiceIntent.putExtra("startTime", startTime);
        startService(sensorServiceIntent);
    }




    private boolean checkPermission() {
        int writePermission = ContextCompat.checkSelfPermission(this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE);


        if (writePermission == PackageManager.PERMISSION_GRANTED) {
            return true;
        }
        else {
            return false;
        }
    }

    private void changeRecordingButtonText(boolean isRecording) {
        Button b = (Button)findViewById(R.id.recording_button);

        if(isRecording) {
            b.setText("Start\nRecording");
        }
        else {
            b.setText("Stop\nRecording");
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS_WRITE_EXTERNAL_STORAGE: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    startCollectingData();

                } else {
                    Button b = (Button)findViewById(R.id.recording_button);
                    b.setText("No Writing Permissions!!!!");
                }
                return;
            }
        }
    }


    public void markEvent(View view) {

        int view_id = view.getId();
        long timestamp = System.currentTimeMillis();


        switch(view_id) {
            case R.id.stand_button:
                doMarking(timestamp, Events.STAND);
                changeEventText(Events.STAND);
                break;
            case R.id.elevator_up_button:
                doMarking(timestamp, Events.ELEVATOR_UP);
                changeEventText(Events.ELEVATOR_UP);
                break;
            case R.id.elevator_down_button:
                doMarking(timestamp, Events.ELEVATOR_DOWN);
                changeEventText(Events.ELEVATOR_DOWN);
                break;
            case R.id.stairs_up_button:
                doMarking(timestamp, Events.STAIRS_UP);
                changeEventText(Events.STAIRS_UP);
                break;
            case R.id.stairs_down_button:
                doMarking(timestamp, Events.STAIRS_DOWN);
                changeEventText(Events.STAIRS_DOWN);
                break;
            case R.id.walk_normal_button:
                doMarking(timestamp, Events.WALK_NORMAL);
                changeEventText(Events.WALK_NORMAL);
                break;
            case R.id.walk_slow_button:
                doMarking(timestamp, Events.WALK_SLOW);
                changeEventText(Events.WALK_SLOW);
                break;
            case R.id.walk_fast_button:
                doMarking(timestamp, Events.WALK_FAST);
                changeEventText(Events.WALK_FAST);
                break;
            case R.id.undefined_button:
                doMarking(timestamp, Events.UNDEFINED);
                changeEventText(Events.UNDEFINED);

        }


    }


    private void doMarking(long timestamp, Events event) {
        EventInformation ei = new EventInformation(timestamp, event);
        new EventMarker(this.currentDate, this.startTime).execute(ei);
    }

    private void changeEventText(Events event) {
        ((TextView)findViewById(R.id.event_text)).setText(event.toString());

    }





}
