package com.example.Maps013;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
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.view.Window;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.SeekBar.OnSeekBarChangeListener;

// https://stackoverflow.com/questions/7064857/making-an-android-map-menu-to-change-map-type

public class Settings_map extends Activity 
{

	public static final String WEIGHT_VALUE = "weight_value";

	public static final String STEP_LENGTH_VALUE = "step_length_value";

	public static final String SENSITIVITY_VALUE = "sensitivity_value";

	public static final String SETP_SHARED_PREFERENCES = "setp_shared_preferences";

	public static SharedPreferences sharedPreferences;

	private Editor editor;

	private TextView tv_sensitivity_vlaue;
	private TextView tv_step_length_vlaue;
	private TextView tv_weight_value;

	private SeekBar sb_sensitivity;
	private SeekBar sb_step_length;
	private SeekBar sb_weight;

	private int sensitivity = 0;
	private int step_length = 0;
	private int weight = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.settings_map_layout);
		addView();
		init();
	}

	
	private void addView() 
	{
		tv_sensitivity_vlaue = (TextView) this.findViewById(R.id.sensitivity_value);
		tv_step_length_vlaue = (TextView) this.findViewById(R.id.step_lenth_value);
		tv_weight_value = (TextView) this.findViewById(R.id.weight_value);
		sb_sensitivity = (SeekBar) this.findViewById(R.id.sensitivity);
		sb_step_length = (SeekBar) this.findViewById(R.id.step_lenth);
		sb_weight = (SeekBar) this.findViewById(R.id.weight);

	}
	
	private void init() 
	{
		
		if (sharedPreferences == null) 
		{    
											
			sharedPreferences = getSharedPreferences(SETP_SHARED_PREFERENCES, MODE_PRIVATE);
		}

		editor = sharedPreferences.edit();

		sensitivity = 10 - sharedPreferences.getInt(SENSITIVITY_VALUE, 7);
		step_length = sharedPreferences.getInt(STEP_LENGTH_VALUE, 70);
		weight = sharedPreferences.getInt(WEIGHT_VALUE, 50);

		sb_sensitivity.setProgress(sensitivity);
		sb_step_length.setProgress((step_length - 40) / 5);               
		sb_weight.setProgress((weight - 30) / 2);

		tv_sensitivity_vlaue.setText(sensitivity + "");
		tv_step_length_vlaue.setText(step_length + getString(R.string.cm));
		tv_weight_value.setText(weight + getString(R.string.kg));
	}

	

	public void onClick(View view) 
	{
		switch (view.getId()) 
		{
				case R.id.save:
					editor.putInt(SENSITIVITY_VALUE, 10 - sensitivity);
					editor.putInt(STEP_LENGTH_VALUE, step_length);
					editor.putInt(WEIGHT_VALUE, weight);
					editor.commit();
					
					Toast.makeText(Settings_map.this, "Sauvegarde", Toast.LENGTH_SHORT).show();
					
					this.finish();
					StepDetector.SENSITIVITY = 10 - sensitivity;
					break;
					
				case R.id.cancel:
					this.finish();
					break;
		
				default:
					break;
		}
	}
	
	@Override
	protected void onRestart() 
	{
		super.onRestart();
		init();
	}

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