package edu.wichita.iot.smart_home.comfortvote;

import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.j256.ormlite.dao.Dao;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import edu.wichita.iot.smart_home.comfortvote.callback.AppendToLogCallback;
import edu.wichita.iot.smart_home.comfortvote.callback.SensorUpdateCallback;
import edu.wichita.iot.smart_home.comfortvote.data.ComfData;
import edu.wichita.iot.smart_home.comfortvote.data.DatabaseHelper;
import edu.wichita.iot.smart_home.comfortvote.data.SensorData;
import edu.wichita.iot.smart_home.comfortvote.data.SensorSampleData;

/**
 * Created by Fariba on 4/6/2016.
 */
public class DataSamplingFragment extends DialogFragment{

    private static final int STATUS_STOP = 0;
    private static final int STATUS_RUN = 1;

    private int sampleingStatus = STATUS_STOP;

    TextView myVoteTextView;
    View rootView;
    SmartBand smartBand;

    // Database Helper
    private DatabaseHelper databaseHelper = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_data_sampling, container, false);
        myVoteTextView = (TextView) rootView.findViewById(R.id.my_vote);

        // Update the service status
        sampleingStatus = STATUS_STOP;

        configureButtons(rootView);

        smartBand = new SmartBand(new SensorUpdateCallback() {
            @Override
            public void update(ComfData comfData, int sensorType) {
                SaveSampleData(comfData, sensorType);
            }
        }, new AppendToLogCallback() {
            @Override
            public void append(String str) {
                // Do Nothing
            }
        });

        return rootView;
    }


    private void  configureButtons(View v){
        ((Button) v.findViewById(R.id.btn_sample)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sampleingStatus == STATUS_STOP){
                    ((Button) v.findViewById(R.id.btn_sample)).setText("Stop");
                    smartBand.activateForSampling(getActivity());
                    sampleingStatus = STATUS_RUN;
                } else if (sampleingStatus == STATUS_RUN){
                    ((Button) v.findViewById(R.id.btn_sample)).setText("Start");
                    smartBand.pause();
                    sampleingStatus = STATUS_STOP;
                }
            }
        });

    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        smartBand.pause();
        super.onDismiss(dialog);
    }

    private void showDialog(String msgStr){
        new AlertDialog.Builder(getActivity())
                .setTitle("Alert")
                .setMessage(msgStr)
                .setCancelable(false)
                .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // Whatever...
                    }
                }).create().show();
    }

    private DatabaseHelper getDBHelper() {
        if (databaseHelper == null) {
            databaseHelper = DatabaseHelper.getHelper(getActivity());
        }
        return databaseHelper;
    }

    private void SaveSampleData(ComfData comfData, int sensorType)      {
        if (sensorType != SmartBand.ACCELEROMETER_SENSOR){
            return;
        }

        try{
            Dao<SensorSampleData, Integer> dao = getDBHelper().getSendorSampleDataDao();
            SensorSampleData sensorSampleData = (SensorSampleData) SensorSampleData.newInstance(comfData);
            dao.create(sensorSampleData);
        } catch (SQLException e){
            System.out.print(e);
        }
    }

}

@@ -267,16 +267,41 @@
 Format;%0A
+import android.util.Log;%0A
 import a
@@ -478,16 +478,45 @@
 extView;
+%0Aimport android.widget.Toast;
 %0A%0Aimport
@@ -677,16 +677,68 @@
 il.List;
+%0Aimport java.util.Timer;%0Aimport java.util.TimerTask;
 %0A%0Aimport
@@ -1399,21 +1399,21 @@
 extView 
-myVot
+dbSiz
 eTextVie
@@ -1460,16 +1460,174 @@
 tBand;%0A%0A
+    // timer handling%0A    private Timer mTimer = null;%0A%0A    // constant%0A    public static final long DB_INFO_PRESENTATION_INTERVAL = 1000 * 1; // 1 seconds%0A%0A%0A
     // D
@@ -1939,13 +1939,13 @@
     
-myVot
+dbSiz
 eTex
@@ -1994,14 +1994,14 @@
 .id.
-my_vot
+db_siz
 e);%0A
@@ -2511,31 +2511,904 @@
     
-return rootView;%0A    %7D%0A
+// cancel if already existed%0A        if(mTimer != null) %7B%0A            mTimer.cancel();%0A        %7D else %7B%0A            // recreate new%0A            mTimer = new Timer();%0A        %7D%0A%0A        // schedule task%0A        mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 0, DB_INFO_PRESENTATION_INTERVAL);%0A%0A        return rootView;%0A    %7D%0A%0A    class TimeDisplayTimerTask extends TimerTask %7B%0A%0A        @Override%0A        public void run() %7B%0A            getActivity().runOnUiThread(new Runnable() %7B%0A                @Override%0A                public void run() %7B%0A                    try %7B%0A                        long dbSize= getDBHelper().getSendorSampleDataDao().countOf();%0A                        dbSizeTextView.setText(Long.toString(dbSize));%0A                    %7D catch (SQLException e)%7B%0A                        e.printStackTrace();%0A                    %7D%0A                %7D%0A            %7D);%0A        %7D%0A    %7D
 %0A%0A  
@@ -4205,24 +4205,25 @@
 e dialog) %7B%0A
+%0A
         smar

Configure timer to show db row count