package net.heartshield.prevent;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import net.heartshield.control.IMeasurementController;
import net.heartshield.data.AppInfo;
import net.heartshield.data.IMeasurementDataManager;
import net.heartshield.data.MeasurementDataManager;
import net.heartshield.data.UserMeta;

import java.util.Timer;
import java.util.TimerTask;

/**
 * @author David Madl (git@abanbytes.eu)
 * @date 2017-06-10
 */
public class EnterHeightActivity extends Activity {
    private static final String TAG = "EnterHeightActivity";

    private IMeasurementDataManager mDataManager;
    private Timer mConnTimer;

    private UserMeta mUser;

    private com.shawnlin.numberpicker.NumberPicker mHeightPicker;
    private ImageView mConnection;
    private IMeasurementController.MeasurementMode mMode;

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

        mDataManager = MeasurementDataManager.getInstance(getApplicationContext());

        mHeightPicker = (com.shawnlin.numberpicker.NumberPicker) findViewById(R.id.height);
        mConnection = (ImageView) findViewById(R.id.connection);

        postConnImage(0);
    }

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

        startTimer();

        mMode = (IMeasurementController.MeasurementMode) getIntent().getSerializableExtra("mode");

        mUser = (UserMeta) getIntent().getSerializableExtra("meta");
        if(mUser == null)
            throw new IllegalStateException("should pass in UserMeta from EnterAgeActivity");

        if(mUser.height != null) {
            Log.i(TAG, "got height=" + mUser.height);
            mHeightPicker.setValue((int) (mUser.height * 100.0));
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        stopTimer();
    }

    private static final int CONN_UPDATE_INTERVAL_MS = 5000;

    private void startTimer() {
        if(mConnTimer == null) {
            mConnTimer = new Timer();
            mConnTimer.schedule(new TimerTask() {
                @Override
                public void run() {
                    updateConnectionDisplay();
                }
            }, CONN_UPDATE_INTERVAL_MS, CONN_UPDATE_INTERVAL_MS);
        }
    }

    private void stopTimer() {
        if(mConnTimer != null) {
            mConnTimer.cancel();
            mConnTimer = null;
        }
    }

    public void updateConnectionDisplay() {
        String appId = AppInfo.getInstance(this).getAppMeta().appId;
        mDataManager.ping(appId, new IMeasurementDataManager.PingResultListener() {
            @Override
            public void onResult() {
                postConnImage(R.drawable.connection_ok);
            }

            @Override
            public void onError() {
                postConnImage(R.drawable.connection_nok);
            }
        });
    }

    private void postConnImage(final int resId) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mConnection.setImageResource(resId);
                mConnection.invalidate();
            }
        });
    }

    public void start(View view) {
        //Intent intent = new Intent(EnterHeightActivity.this, TimeActivity.class);
        Intent intent = new Intent(EnterHeightActivity.this, EnterGenderActivity.class);
        mUser.height = mHeightPicker.getValue() / 1e2;
        intent.putExtra("meta", mUser);
        intent.putExtra("mode", mMode);
        startActivity(intent);
    }

    public void stats(View view) {
        Intent intent = new Intent(EnterHeightActivity.this, ResultListActivity.class);
        startActivity(intent);
    }

    public void stetho(View view) {
        Intent intent = new Intent(EnterHeightActivity.this, PcgActivity.class);
        startActivity(intent);
    }

    public void help(View view) {
        Intent intent = new Intent(EnterHeightActivity.this, DisclaimerActivity.class);
        intent.putExtra("forceShow", true);
        startActivity(intent);
    }
}
