package com.dniemcu;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

public class MailConfiguration extends Activity {
	
	//Nombre de los parámetros almacenados en las preferencias
	private final static String SETTING_USER = "setting_user";
	private final static String SETTING_PASS = "setting_pass";
	SharedPreferences sharedPreferences;
	
	AlertDialog ad = null;

    private Context myContext 	= null;
	
	void updateUserData()
	{
		// Actualizamos los valores mostrados para cuenta y contraseña
		sharedPreferences = getApplicationContext().getSharedPreferences("com.sp.main_preferences", Context.MODE_PRIVATE);
        
        // Recupera los valores de usuario y contraseña guardados
	    String user = sharedPreferences.getString(SETTING_USER,"");
	    String pass = sharedPreferences.getString(SETTING_PASS,"");

        // Ajustamos el tipo de letra
        Typeface type = Typeface.createFromAsset(myContext.getAssets(),"fonts/NeutraText-LightAlt.otf");
        ((TextView)findViewById(R.id.SEND_data_text_01)).setTypeface(type);
        ((TextView)findViewById(R.id.SEND_data_text_02)).setTypeface(type);
        ((TextView)findViewById(R.id.SEND_data_text_03)).setTypeface(type);
        ((TextView)findViewById(R.id.SEND_data_tab_01)).setTypeface(type);
        ((TextView)findViewById(R.id.SEND_data_tab_01)).setText(user);
        ((TextView)findViewById(R.id.SEND_data_tab_02)).setTypeface(type);
        ((TextView)findViewById(R.id.SEND_data_tab_02)).setText(pass.replaceAll(".", "*"));
        ((Button)findViewById(R.id.Btn_CONT)).setTypeface(type);
	}
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // Quitamos la barra del título
     	this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.mail_config);

        // Inicializamos miembros
        myContext  = MailConfiguration.this;

        // Actualizamos valores
        updateUserData();
        
        ///////////////////////////////////////////////////////////////////////////////////
		// Botón de vuelta al Activity anterior
		Button btnPrKBack = (Button)findViewById(R.id.Btn_BACK);
		
		btnPrKBack.setOnClickListener(new OnClickListener() {		
		public void onClick(View v) {
			
				// Volvemos al Activity anterior
				onBackPressed();
			}
		});
		
		///////////////////////////////////////////////////////////////////////////////////
		// Botón de CONTINUAR
		Button btnPrKCont = (Button)findViewById(R.id.Btn_CONT);
		btnPrKCont.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				LayoutInflater factory = LayoutInflater.from(myContext);
	            final View userPasswordView = factory.inflate(R.layout.userpasswordentry, null);
		    	ad = new AlertDialog.Builder(myContext).create();
			    ad.setCancelable(false);
			    ad.setIcon(R.drawable.alert_dialog_icon);
			    ad.setView(userPasswordView);
			    ad.setButton(AlertDialog.BUTTON_POSITIVE, getString(R.string.op_aceptar), new DialogInterface.OnClickListener() 
			    {
			        public void onClick(DialogInterface dialog, int which) 
			        {
			        	// Almacenamos los datos de la nueva cuenta
			        	EditText user = (EditText)ad.findViewById(R.id.user_edit);
			        	EditText password = (EditText)ad.findViewById(R.id.password_edit);

			        	//Guarda los valores en las preferencias compartidas
			            SharedPreferences.Editor editor = sharedPreferences.edit();
			            editor.putString(SETTING_USER, user.getText().toString());
			            editor.putString(SETTING_PASS, password.getText().toString());
			            editor.commit();

			            // Eliminamos la notificación
			            ((MyAppMcu)getApplicationContext()).setNotifications(MyAppMcu.EVENT_CONFIG, 0);
			            
			            finish();
			   	     	startActivity(new Intent(MailConfiguration.this, DNIeMcu.class));
			        }
			    });
			    ad.setButton(AlertDialog.BUTTON_NEGATIVE, getString(R.string.op_cancelar), new DialogInterface.OnClickListener() 
			    {
			        public void onClick(DialogInterface dialog, int which) 
			        {
			        	
			        }
			    });
				ad.show();

                // Ajustamos el tipo de letra
                Typeface type = Typeface.createFromAsset(myContext.getAssets(),"fonts/NeutraText-LightAlt.otf");
                ((TextView)ad.findViewById(R.id.user_view)).setTypeface(type);
                ((TextView)ad.findViewById(R.id.user_edit)).setTypeface(type);
                ((TextView)ad.findViewById(R.id.password_view)).setTypeface(type);
                ((TextView)ad.findViewById(R.id.password_edit)).setTypeface(type);
                ((TextView)ad.findViewById(R.id.checkBoxShow)).setTypeface(type);

                final EditText passwordText = (EditText) userPasswordView.findViewById(R.id.password_edit);
                final CheckBox passwordShow = (CheckBox) userPasswordView.findViewById(R.id.checkBoxShow);
                passwordShow.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
                {
                    @Override
                    public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) {
                        if (isChecked) {
                            passwordText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                            passwordShow.setText(myContext.getString(R.string.psswd_dialog_show));
                        } else {
                            passwordText.setTransformationMethod(PasswordTransformationMethod.getInstance());
                            passwordShow.setText(myContext.getString(R.string.psswd_dialog_hide));
                        }
                    }
                });
            }
			});
    }
}
