package com.linx.dposandroid;

import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.text.InputType;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnKeyListener;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

public class CFragmentCodigoSeguranca extends Fragment {
	public String sLabel;
	public String sValorInicial;
	public int iTamanhoMaximoDigitos;
	public CDPOSDRVTELA cdposdrvtela;
	private EditText edtCodigoSeguranca;
	private TextView txtLabelCodigoSeguranca;
	private String sAux;
  private FakeR fakeR;
	private void showKeyboard() {
     	InputMethodManager imm = (InputMethodManager) cdposdrvtela.getSystemService(Context.INPUT_METHOD_SERVICE);
     	imm.showSoftInput(edtCodigoSeguranca, InputMethodManager.SHOW_IMPLICIT);
        imm.toggleSoftInput(0, 0);
	}
	
	private void hideKeyboard() {
     	InputMethodManager imm = (InputMethodManager) cdposdrvtela.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0, 0);
	}
		
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        /** Inflating the layout for this fragment **/
        fakeR = new FakeR(getActivity());
		View view = inflater.inflate(fakeR.getId("layout","fragment_codigo_seguranca_layout"), container, false);		
		txtLabelCodigoSeguranca=(TextView) view.findViewById(fakeR.getId("id","txtLabelCodigoSeguranca"));		
     	edtCodigoSeguranca=(EditText) view.findViewById(fakeR.getId("id","edtCodigoSeguranca"));
     	txtLabelCodigoSeguranca.setText(sLabel);
     	edtCodigoSeguranca.setTextSize(12);     	
     	edtCodigoSeguranca.setText(sValorInicial);
     	edtCodigoSeguranca.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS );
     	
     	showKeyboard();
     	edtCodigoSeguranca.requestFocus();
     	
     	edtCodigoSeguranca.setOnEditorActionListener(new OnEditorActionListener() {
		    @Override
		    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
		    	boolean bResult = false;
		    	
		    	//System.out.println("[CFragmentString] onEditorAction event ");
		    	
		    	if (actionId == EditorInfo.IME_ACTION_DONE) {
		    		System.out.println("[CFragmentCodigoSeguranca] ENTER do teclado pressionado");
 					cdposdrvtela.bOKPressionado = true;
 			    	hideKeyboard();
		    	} 
		    	
		    	return bResult;
		    }
			});
		
     	edtCodigoSeguranca.setOnKeyListener(new OnKeyListener() 
 		{
 			public boolean onKey(View v, int keyCode, KeyEvent event) 
 			{ 	 
 				// if keydown and "enter" is pressed
 				if ((event.getAction() == KeyEvent.ACTION_DOWN)
 						&& (keyCode == KeyEvent.KEYCODE_ENTER)) 
 				{
 			            	cdposdrvtela.bOKPressionado = true;
 			    	hideKeyboard();
 				}
 				else if (event.getAction() == KeyEvent.ACTION_UP && (keyCode >= KeyEvent.KEYCODE_0 && keyCode <= KeyEvent.KEYCODE_9))
 	 			{
 	 				sAux = edtCodigoSeguranca.getText().toString();
 	 				if (sAux.length() > iTamanhoMaximoDigitos)
 	 				{
 	 					sAux=sAux.substring(0,iTamanhoMaximoDigitos);
 	 					edtCodigoSeguranca.setText(sAux);
 	 					edtCodigoSeguranca.setSelection(sAux.length());
 	 				}
 	 			} 					
 				
 				return false;
 			}
 		});   		
        return view;  
	}
	
    public String getcodigoSeguranca() {
    	return edtCodigoSeguranca.getText().toString();    	
    }
	

}
