package com.jachdev.consumerprotection.ui.calories;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatImageButton;
import androidx.core.app.ActivityCompat;
import butterknife.BindView;
import butterknife.OnClick;
import me.itangqi.waveloadingview.WaveLoadingView;

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.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.jachdev.commonlibs.base.BaseActivity;
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 PedometerActivity extends BaseActivity implements SensorEventListener, StepListener {

    public static final String KEY_DATA_WEIGHT = "com.jachdev.consumerprotection.ui.calories.KEY_DATA_HEIGHT";
    public static final String KEY_DATA_HEIGHT = "com.jachdev.consumerprotection.ui.calories.KEY_DATA_HEIGHT";
    private static final String TAG = PedometerActivity.class.getSimpleName();
    @BindView(R.id.waveLoadingView)
    WaveLoadingView waveLoadingView;
    @BindView(R.id.tvkCal)
    CustomTextView tvkCal;
    @BindView(R.id.tvMiles)
    CustomTextView tvMiles;
    @BindView(R.id.btnStart)
    AppCompatImageButton btnStart;

    private boolean isStart = false;
    private double weight, height = 0;


    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 = " steps";
    private static final int MAX_STEPS = 100;
    private int numSteps;


    @Override
    protected int layoutRes() {
        return R.layout.activity_calories;
    }

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

        onNewIntent(getIntent());

        init();

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

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        if(intent == null || intent.getExtras() == null)
            return;

        weight = intent.getExtras().getDouble(KEY_DATA_WEIGHT);
        height = intent.getExtras().getDouble(KEY_DATA_HEIGHT);
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        Log.d(TAG, "onAccuracyChanged");
    }

    @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) {
        Log.d(TAG, "step: " + numSteps);
        Log.d(TAG, "timeNs: " + timeNs);
        if(MAX_STEPS < numSteps)
            return;

        numSteps++;
        waveLoadingView.setCenterTitle(numSteps + TEXT_NUM_STEPS);
        waveLoadingView.setProgressValue(numSteps);

        tvMiles.setAnyText(getString(R.string.no_miles, String.format("%.02f", getDistanceRun(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 + "");
    }

    @OnClick(R.id.btnStart)
    void onClickStart(){
        if(isStart){
            isStart = false;

            stopPedo();
        }else{
            isStart = true;

            startPedo();
        }

        updateUi();
    }

    private void init() {
        tvMiles.setAnyText(getString(R.string.no_miles, String.valueOf(0)));
        tvkCal.setAnyText(getString(R.string.no_cal, String.valueOf(0)));
    }

    private void startPedo() {

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

    private void stopPedo() {

        sensorManager.unregisterListener(PedometerActivity.this);
    }

    private void updateUi() {
        btnStart.setBackgroundDrawable(ActivityCompat.getDrawable(this, !isStart ? R.drawable.bg_pedo_normal : R.drawable.bg_pedo_started));
    }

    private float getDistanceRun(int steps){
        return (float)(steps * height)/(float)100000;
    }
}