/*Copyright 2011 indiandragon

 This file is part of j2me2AB.

 J2meAB is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 j2me2AB is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with j2me2AB.  If not, see <http://www.gnu.org/licenses/>.
 */
package javax.microedition.lcdui;
import android.graphics.Typeface;
import android.text.InputFilter;
import android.text.InputType;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TextField extends Item {

    public static final int ANY = 0x000;
    public static final int DECIMAL = 0x001;
    public static final int EMAILADDR = 0x002;
    public static final int INITIAL_CAPS_SENTENCE = 0x004;
    public static final int INITIAL_CAPS_WORD = 0x008;
    public static final int NON_PREDICTIVE = 0x010;
    public static final int NUMERIC = 0x020;
    public static final int PASSWORD = 0x040;
    public static final int PHONENUMBER = 0x080;
    public static final int SENSITIVE = 0x100;
    public static final int URL = 0x200;
    public static final int UNEDITABLE = 0x400;
    int constraints = ANY;
    String text = "";
    int maxsize = 0;
    TextView textViewLabel;
    EditText editText;
    public boolean bPasswordFlag = false;

    public TextField(String plabel, String ptext, int pmaxSize, int pconstraints) {
        if (plabel == null) {
            plabel = "";
        }
        if (ptext == null) {
            ptext = "";
        }
        this.label = plabel;
        text = ptext;
        maxsize = pmaxSize;
        constraints = pconstraints;
        int intInputType = CalculateInputMode(constraints);
        
        textViewLabel = new TextView(UIGlobal.APP_CONTEXT);
        textViewLabel.setTypeface(UIGlobal.APP_TYPEFACE, Typeface.BOLD);
        textViewLabel.setTextSize(Font.getDefaultFont().getSize());
        textViewLabel.setTextColor(UIGlobal.TEXT_NORMAL);
        textViewLabel.setText(plabel);

        editText = new EditText(UIGlobal.APP_CONTEXT);
        editText.setTypeface(UIGlobal.APP_TYPEFACE);
        editText.setTextSize(Font.getDefaultFont().getSize());
        editText.setTextColor(UIGlobal.TEXT_NORMAL);
        editText.setText(ptext);
        editText.setInputType(intInputType);

        if (bPasswordFlag == true) {
            editText.setTransformationMethod(new PasswordTransformationMethod());
        }
        InputFilter[] FilterArray = new InputFilter[1];
        FilterArray[0] = new InputFilter.LengthFilter(pmaxSize);
        editText.setFilters(FilterArray);

        this.setOrientation(LinearLayout.VERTICAL);
        this.addView(textViewLabel, 0);
        this.addView(editText, 1);
    }

    public int getConstraints() {
        return constraints;
    }

    public int getMaxSize() {
        return maxsize;
    }

    public String getString() {
        if (editText != null) {
            return editText.getText().toString();
        }
        return "";
    }

    public void setConstraints(int pconstraints) {
        constraints = pconstraints;
    }

    public int setMaxSize(int pmaxSize) {
        maxsize = pmaxSize;
        return maxsize;
    }

    public void setString(String text) {
        if (editText != null) {
            //editText.setText(text);
           
            char[] chars = text.toCharArray();
            editText.setText(chars, 0, chars.length);
        }
    }

    int size() {
        if (editText != null) {
            return editText.getText().toString().length();
        }
        return 0;
    }

    public int CalculateInputMode(int pconstraints) {
        int inputtype = InputType.TYPE_CLASS_TEXT;
        if ((pconstraints & ANY) > 0) {
            inputtype = InputType.TYPE_CLASS_TEXT;
        } else if ((pconstraints & EMAILADDR) > 0) {
            inputtype = InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS;
        } else if ((pconstraints & NUMERIC) > 0) {
            inputtype = InputType.TYPE_CLASS_NUMBER;
        } else if ((pconstraints & PHONENUMBER) > 0) {
            inputtype = InputType.TYPE_CLASS_PHONE;
        } else if ((pconstraints & URL) > 0) {
            inputtype = InputType.TYPE_TEXT_VARIATION_URI;
        } else if ((pconstraints & DECIMAL) > 0) {
            inputtype = InputType.TYPE_CLASS_NUMBER
                    | InputType.TYPE_NUMBER_FLAG_DECIMAL;
        }

        if ((pconstraints & INITIAL_CAPS_SENTENCE) > 0) {
            inputtype = inputtype | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES;
        } else if ((pconstraints & INITIAL_CAPS_WORD) > 0) {
            inputtype = inputtype | InputType.TYPE_TEXT_FLAG_CAP_WORDS;
        } else if ((pconstraints & PASSWORD) > 0) {
            inputtype = inputtype | InputType.TYPE_TEXT_VARIATION_PASSWORD;
            bPasswordFlag = true;
        }
        return inputtype;
    }

    public String getLabel() {
        return this.label;
    }

    public void setLabel(String label) {
        this.label = label;
        if (this.textViewLabel != null) {
            this.textViewLabel.setText(label);
        }
    }

    public View getView() {
        return this;
    }
}
