package net.heartshield.prevent;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

import net.heartshield.data.DataManagerFactory;
import net.heartshield.data.IMeasurementDataManager;
import net.heartshield.data.Measurement;
import net.heartshield.data.MeasurementDataManager;
import net.heartshield.data.MeasurementSummary;

import java.io.IOException;

/**
 * Displays a list of previous measurements and allows to open them for viewing/editing.
 *
 * @author David Madl (git@abanbytes.eu)
 * @date 2017-04-20
 */
public class ResultListActivity extends ListActivity {
    private static final String TAG = "ResultListActivity";

    private IMeasurementDataManager mDataManager;
    private ResultLineAdapter mAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //setContentView(R.layout.activity_result_list);

    }

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

        // the following initialization is in onResume() and not in onCreate() because a testing mode
        // requires on-the-fly changes to MeasurementDataManager

        mDataManager = DataManagerFactory.getInstance().getDataManager(this);
        mDataManager.startBackgroundThread();
        mDataManager.startBackgroundSync();

        /*

        // testing only
        MeasurementSummary[] entries = new MeasurementSummary[]{
                new MeasurementSummary(1492694754, "C648B4CC", MeasurementSummary.Status.OK, true, 34, 86),
                new MeasurementSummary(1492664750, "C648B4CC", MeasurementSummary.Status.OK, false, 74, 123)
        };

        */

        mAdapter = new ResultLineAdapter(this, mDataManager.list());

        mDataManager.setSummaryListener(new IMeasurementDataManager.MeasurementSummaryListener() {
            @Override
            public void onMeasurementCreated(final Measurement measurement) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mAdapter.add(measurement.getSummary());
                    }
                });
            }

            @Override
            public void onMeasurementUpdated(final Measurement measurement) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mAdapter.update(measurement.getSummary());
                    }
                });
            }
        });

        setListAdapter(mAdapter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mDataManager.stopBackgroundThread();
        mDataManager.setSummaryListener(null); // stop receiving updates when not active
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        MeasurementSummary item = (MeasurementSummary) getListAdapter().getItem(position);
        //Toast.makeText(this, item.getId().toString() + " selected", Toast.LENGTH_SHORT).show();

        /*
        if(item.status == MeasurementSummary.Status.OFFLINE) {
            Measurement measurement = null;
            try {
                measurement = mDataManager.retrieve(item.getId());
            } catch (IOException e) {
                e.printStackTrace();
                Toast.makeText(ResultListActivity.this, "Failed to open result file! " + e.getMessage(), Toast.LENGTH_LONG).show();
                return;
            }

            Toast.makeText(ResultListActivity.this, "Sending measurement again...", Toast.LENGTH_LONG).show();

            mDataManager.update(measurement, new IMeasurementDataManager.MeasurementResultListener() {
                @Override
                public void onResult(Measurement measurement) {
                    //assert(measurement.result != null);

                    // successful result from server
                    Intent intent = new Intent(ResultListActivity.this, ResultActivity.class);
                    intent.putExtra("measurementId", measurement.getId());
                    startActivity(intent);
                }

                @Override
                public void onError(Measurement measurement, IMeasurementDataManager.ErrorCode code, String message) {
                    // some error saving the stuff
                    Toast.makeText(ResultListActivity.this, "Failed to get result: " + code + " " + message, Toast.LENGTH_LONG).show();
                    Log.e(TAG, "Failed to get result: " + code + " " + message);
                }
            });
        } else {*/
            Intent intent = new Intent(ResultListActivity.this, ResultActivity.class);
            intent.putExtra("measurementId", item.getId());
            startActivity(intent);
        //}
    }
}
