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.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;

public class CFragmentString extends Fragment {
	public String sLabel;
	public String sStringInicial;
	public int iMinimoDigitos;
	public int iMaximoDigitos;
	public int iLimpaBuffer;
	public int iColetaSecreta;
	public CDPOSDRVTELA cdposdrvtela;
	private EditText edtString;
	private TextView txtLabel;
	private String sAux;
	private String sRetorno;
  private FakeR fakeR;
	
	@Override
    public void onStart()
    {
        super.onStart();        
    } 
	
	private void showKeyboard() {
     	InputMethodManager imm = (InputMethodManager) cdposdrvtela.getSystemService(Context.INPUT_METHOD_SERVICE);
     	imm.showSoftInput(edtString, 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_string_layout"), container, false);
		txtLabel=(TextView) view.findViewById(fakeR.getId("id","txtLabelString"));
		txtLabel.setText(sLabel);
     	edtString=(EditText) view.findViewById(fakeR.getId("id","edtString"));
     	edtString.setTextSize(12); //???
     	
		if (iLimpaBuffer==1)			
			edtString.setText("");
		else
			edtString.setText(sStringInicial);
		
		if (iColetaSecreta==1)
			edtString.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
		else
			edtString.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
		
		showKeyboard();
		edtString.requestFocus();

		edtString.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("[CFragmentString] ENTER do teclado pressionado");
	    		bResult = DadosValidos();
	    		if (bResult) {
	    			hideKeyboard();
	    		}
	    	} 
	    	
	    	return bResult;
	    }
		});


     	edtString.setOnKeyListener(new OnKeyListener() 
 		{
 			public boolean onKey(View v, int keyCode, KeyEvent event) 
 			{ 	 
 				boolean bResult = false;
 				
 				//System.out.println("[CFragmentString] onKey (0) keyCode=[" + keyCode + "]");
        sRetorno = edtString.getText().toString();

 				// if keydown and "enter" is pressed
 				if ((event.getAction() == KeyEvent.ACTION_DOWN)
 						&& (keyCode == KeyEvent.KEYCODE_ENTER)) 
 				{
 					bResult = DadosValidos();
 		    		if (bResult) {
 		    			hideKeyboard();
 	        				}
 	        			}
 				
 	 			if (event.getAction() == KeyEvent.ACTION_UP && (keyCode >= KeyEvent.KEYCODE_0 && keyCode <= KeyEvent.KEYCODE_9))
 	 			{
 	 				sAux = edtString.getText().toString();
 	 				if(iMaximoDigitos>0 && sAux.length()>iMaximoDigitos)
 	 				{
 	 					sAux = sAux.substring(0,iMaximoDigitos);
 	 					edtString.setText(sAux);
 	 					edtString.setSelection(sAux.length());
 	 				}
 	 			} 	 				 				
 				
 				return bResult;
 			}
 		});   		
     	
        return view;  
	}
	
	private boolean DadosValidos() {
 		if (iMinimoDigitos != 0 || iMaximoDigitos != 0) {
 			if (iMinimoDigitos != 0) { 	        		             				
 				if (edtString.getText().length() < iMinimoDigitos) {
         			cdposdrvtela.bOKPressionado = false;
 					return false;
 				}
 			}
 			
 			if (iMaximoDigitos != 0) {       				
 				if (edtString.getText().length() > iMaximoDigitos) {
         			cdposdrvtela.bOKPressionado = false;
 					return false;
 				}
 			}
 		}
 		
 		sRetorno = edtString.getText().toString();
		cdposdrvtela.bOKPressionado = true;
		return true;
	}
	  
    public String stringDigitada() {    	
	    //System.out.println("[CFragmentString] stringDigitada=[" + sRetorno + "]");
	    //System.out.println("[CFragmentString] edtString.getText().toString()=[" + edtString.getText().toString() + "]");
    	return sRetorno; //edtString.getText().toString();    	
    }	
    

}
