package com.jachdev.consumerprotection.ui.calories;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.jachdev.commonlibs.widget.CustomEditText;
import com.jachdev.commonlibs.widget.CustomTextView;
import com.jachdev.consumerprotection.R;
import com.jachdev.consumerprotection.util.StepDetector;
import com.jachdev.consumerprotection.util.StepListener;

public class CaloriesActivity extends AppCompatActivity implements SensorEventListener, StepListener {

    private CustomTextView TvSteps, tvCount;
    private CustomEditText etWeight, etHeight;
    private Button BtnStart, BtnStop;
    private StepDetector simpleStepDetector;
    private SensorManager sensorManager;
    private Sensor accel;
    private static final String TEXT_NUM_STEPS = "Number of Steps: ";
    private int numSteps;

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

        // Get an instance of the SensorManager
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        accel = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        simpleStepDetector = new StepDetector();
        simpleStepDetector.registerListener(this);

        TvSteps = (CustomTextView) findViewById(R.id.tv_steps);
        tvCount = (CustomTextView) findViewById(R.id.tv_count);
        BtnStart = (Button) findViewById(R.id.btn_start);
        BtnStop = (Button) findViewById(R.id.btn_stop);
        etWeight = (CustomEditText) findViewById(R.id.et_weight);
        etHeight = (CustomEditText) findViewById(R.id.et_height);



        BtnStart.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                numSteps = 0;
                sensorManager.registerListener(CaloriesActivity.this, accel, SensorManager.SENSOR_DELAY_FASTEST);

            }
        });


        BtnStop.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                sensorManager.unregisterListener(CaloriesActivity.this);

                startActivity(new Intent(CaloriesActivity.this, ExercisesSuggeserActivity.class));

            }
        });
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            simpleStepDetector.updateAccel(
                    event.timestamp, event.values[0], event.values[1], event.values[2]);
        }
    }

    @Override
    public void step(long timeNs) {
        numSteps++;
        TvSteps.setText(TEXT_NUM_STEPS + numSteps);

        double weightSum = etWeight.getTextDouble() * 0.035;
        double velocity = (1.4 * numSteps) * (1.4 * numSteps);
        double velocityDevide = velocity/etHeight.getTextDouble();
        double addFstSec = velocityDevide + weightSum;
        double muly = addFstSec * 0.029;
        double res = muly * etWeight.getTextDouble();

        tvCount.setAnyText(res + "");
    }
}