package com.lady.viktoria.lightdrip;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lady.viktoria.lightdrip.DatabaseModels.SensorData;
import com.lady.viktoria.lightdrip.RealmConfig.RealmBaseFragment;
import com.wdullaer.materialdatetimepicker.date.DatePickerDialog;
import com.wdullaer.materialdatetimepicker.time.TimePickerDialog;

import java.util.Calendar;
import java.util.Date;
import java.util.UUID;

import io.realm.Realm;
import io.realm.RealmResults;

import static io.realm.Realm.getInstance;

public class SensorActionFragment extends RealmBaseFragment implements DatePickerDialog.OnDateSetListener, TimePickerDialog.OnTimeSetListener {

    private final static String TAG = SensorActionFragment.class.getSimpleName();

    public SensorActionFragment() {
    }

    Calendar SensorStart;
    int mYear, mMonthOfYear, mDayOfMonth, mHourOfDay, mMinute;
    private Realm mRealm;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_sensoraction, container, false);
        SensorStart = Calendar.getInstance();
        Realm.init(getActivity());
        mRealm = getInstance(getRealmConfig());
        SensorDialog();
        return view;
    }

    protected void SensorDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage("Please choice if you want to stop current Sensor or start a new Sensor");
        builder.setTitle("Sensor Actions");
        builder.setPositiveButton("Start Sensor", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                StartSensor();
                dialog.dismiss();
            }
        });

        builder.setNegativeButton("Stop sensor", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                StopSensor();
                dialog.dismiss();
            }
        });
        builder.create().show();
    }

    private void StartSensor() {
        Calendar now = Calendar.getInstance();
        DatePickerDialog dpd = DatePickerDialog.newInstance(
                SensorActionFragment.this,
                now.get(Calendar.YEAR),
                now.get(Calendar.MONTH),
                now.get(Calendar.DAY_OF_MONTH)
        );
        dpd.show(getFragmentManager(), "Datepickerdialog");
    }

    private void StopSensor() {
        long stopped_at = new Date().getTime();
        RealmResults<SensorData> results = mRealm.where(SensorData.class).findAll();
        String lastUUID = results.last().getuuid();
        SensorData mSensorData = mRealm.where(SensorData.class).equalTo("uuid", lastUUID).findFirst();
        mRealm.beginTransaction();
        mSensorData.setstopped_at(stopped_at);
        mRealm.commitTransaction();
        mRealm.close();
    }

    private void CurrentSensor() {

    }

    @Override
    public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
        mYear = year;
        mMonthOfYear = monthOfYear;
        mDayOfMonth = dayOfMonth;
        Calendar now = Calendar.getInstance();
        TimePickerDialog tpd = TimePickerDialog.newInstance(
                SensorActionFragment.this,
                now.get(Calendar.HOUR_OF_DAY),
                now.get(Calendar.MINUTE),
                true
        );
        tpd.show(getFragmentManager(), "Timepickerdialog");
    }

    @Override
    public void onTimeSet(TimePickerDialog view, int hourOfDay, int minute, int second) {
        mHourOfDay = Integer.parseInt(hourOfDay < 10 ? "0"+hourOfDay : ""+hourOfDay);
        mMinute = Integer.parseInt(minute < 10 ? "0"+minute : ""+minute);
        SensorStart.set(mYear, mMonthOfYear, mDayOfMonth, mHourOfDay, mMinute, 0);
        long startTime = SensorStart.getTime().getTime();
        String uuid = UUID.randomUUID().toString();
        mRealm.beginTransaction();
        SensorData mSensorData = mRealm.createObject(SensorData.class, uuid);
        mSensorData.setstarted_at(startTime);
        mRealm.commitTransaction();
        mRealm.close();
        getActivity().getFragmentManager().popBackStack();
    }
}
@@ -124,24 +124,96 @@
 ort.
-v7.app.AlertDial
+design.widget.Snackbar;%0Aimport android.support.v7.app.AlertDialog;%0Aimport android.util.L
 og;%0A
@@ -1976,32 +1976,77 @@
 g, int which) %7B%0A
+                if (!isSensorActive()) %7B%0A    
                 
@@ -2068,32 +2068,36 @@
                 
+ 
+   
 dialog.dismiss()
@@ -2090,32 +2090,224 @@
 alog.dismiss();%0A
+                %7D else %7B%0A                    Snackbar.make(getView(), %22Please stop current Sensor fist!%22, Snackbar.LENGTH_LONG).show();%0A                    dialog.dismiss();%0A                %7D%0A
             %7D%0A  
@@ -2660,32 +2660,36 @@
 sor() %7B%0A        
+    
 Calendar now = C
@@ -2703,32 +2703,36 @@
 .getInstance();%0A
+    
         DatePick
@@ -2784,32 +2784,36 @@
                 
+    
 SensorActionFrag
@@ -2815,32 +2815,36 @@
 nFragment.this,%0A
+    
                 
@@ -2875,32 +2875,36 @@
                 
+    
 now.get(Calendar
@@ -2912,16 +2912,20 @@
 MONTH),%0A
+    
         
@@ -2963,35 +2963,43 @@
 _MONTH)%0A        
-);%0A
+    );%0A    
         dpd.show
@@ -3553,32 +3553,519 @@
 sor() %7B%0A%0A    %7D%0A%0A
+    private boolean isSensorActive() %7B%0A        RealmResults%3CSensorData%3E results = mRealm.where(SensorData.class).findAll();%0A        String lastUUID = results.last().getuuid();%0A        SensorData mSensorData = mRealm.where(SensorData.class).equalTo(%22uuid%22, lastUUID).findFirst();%0A        mRealm.beginTransaction();%0A        mRealm.commitTransaction();%0A        mRealm.close();%0A        if (mSensorData.getstopped_at() == 0L) %7B%0A            return true;%0A        %7D%0A        return false;%0A    %7D%0A%0A
     @Override%0A  

add sensor active check