package com.espritmobile.sim.restocheck;

import com.espritmobile.sim.restocheck.utils.Utils;
import com.gc.materialdesign.views.ButtonRectangle;

import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class MainActivity extends FragmentActivity {
	private static long back_pressed;
	String file = "settings";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		SharedPreferences prefs = getSharedPreferences(file, 0);
		Utils.currency = prefs.getString("currency", "DT");
		Utils.currencyid = prefs.getInt("currencyid", 0);


	}

	@Override
	public void onBackPressed() {
		if (back_pressed + 2000 > System.currentTimeMillis())
			super.onBackPressed();
		else
			Toast.makeText(getBaseContext(),
					"Veuillez appuyer une nouvelle fois pour quitter !",
					Toast.LENGTH_LONG).show();
		back_pressed = System.currentTimeMillis();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu items for use in the action bar
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle presses on the action bar items
		switch (item.getItemId()) {
		case R.id.action_settings:
			afficherElementDialog();
			return true;

		default:
			return super.onOptionsItemSelected(item);
		}
	}

	private void afficherElementDialog() {
		final Dialog currency_dialog = new Dialog(this);
		currency_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		currency_dialog.setContentView(R.layout.currency_dialog);

		Spinner spinner = (Spinner) currency_dialog.findViewById(R.id.currency);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.currency, R.layout.spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);
		spinner.setSelection(Utils.currencyid);
		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Utils.currency = (String) parent.getItemAtPosition(position);
				Utils.currencyid = position;
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub
				parent.setSelection(Utils.currencyid);
			}
		});
		ButtonRectangle next = (ButtonRectangle) currency_dialog
				.findViewById(R.id.next);
		next.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				SharedPreferences prefs = getSharedPreferences(file, 0);
				SharedPreferences.Editor editor = prefs.edit();
				editor.putString("currency", Utils.currency);
				editor.putInt("currencyid", Utils.currencyid);
				editor.commit();
				currency_dialog.dismiss();

			}
		});
		ButtonRectangle cancel = (ButtonRectangle) currency_dialog
				.findViewById(R.id.cancel);
		cancel.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				currency_dialog.dismiss();

			}
		});

		currency_dialog.setCanceledOnTouchOutside(true);
		currency_dialog.show();

	}
}
