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;

/**
 * Enter age and gender before measurement.
 *
 * @author David Madl (git@abanbytes.eu)
 * @date 2016-04-01
 */
public class EnterAgeActivity extends Activity {
    private static final String TAG = "EnterAgeActivity";

    private IMeasurementDataManager mDataManager;
    private Timer mConnTimer;

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

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

        mDataManager = MeasurementDataManager.getInstance(getApplicationContext());

        mAgePicker = (com.shawnlin.numberpicker.NumberPicker) findViewById(R.id.age);
        mConnection = (ImageView) findViewById(R.id.connection);

        postConnImage(0);
    }

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

        startTimer();

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

        UserMeta user = (UserMeta) getIntent().getSerializableExtra("meta");
        if(user != null) {
            Log.i(TAG, "got age=" + user.age + " gender=" + user.gender);
            //mAgePicker.setValue(user.age);
        }
    }

    @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();
            }
        });
    }

	// TODO: Enter medication (HRV influencing) before measurement

    public void start(View view) {
        //Intent intent = new Intent(EnterAgeActivity.this, TimeActivity.class);

        Intent intent;

        if(AppInfo.getInstance(this).devSettings.showHeightDialog) {
            intent = new Intent(EnterAgeActivity.this, EnterHeightActivity.class);
        } else {
            intent = new Intent(EnterAgeActivity.this, EnterGenderActivity.class);
        }
        UserMeta meta = new UserMeta(mAgePicker.getValue(), null, null);
        intent.putExtra("meta", meta);
        intent.putExtra("mode", mMode);
        startActivity(intent);
    }

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

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

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