/*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.R;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class StringItem extends Item {

    TextView textView;
    Button textViewValue;
    int appearanceMode = 0;
    Item item;

    public StringItem(String strName, String strValue, int type) {
        this(strName, strValue);
    }

    public StringItem(String strName, String strValue) {
        item = this;
        if (strName == null) {
            strName = "";
        }
        if (strValue == null) {
            strValue = "";
        }

        textView = new TextView(UIGlobal.APP_CONTEXT);
        textView.setTypeface(UIGlobal.APP_TYPEFACE, Typeface.BOLD);
        textView.setTextSize(Font.getDefaultFont().getSize());
        textView.setText(strName);
        textView.setTextColor(UIGlobal.TEXT_NORMAL);


        textViewValue = new Button(UIGlobal.APP_CONTEXT);
//textViewValue;
//        textViewValue = (Button) findViewById(R.id.closeButton);
        
        textViewValue.setClickable(true);
        textViewValue.setOnClickListener(new OnClickListener() {
            public void onClick(View vs) {
                if (itemCommandListener != null) {
                    itemCommandListener.commandAction(defaultCommand, item);
                }
            }
        });

//        textViewValue.setBackgroundColor(Color.GREEN);
        textViewValue.setHighlightColor(Color.GREEN);
        textViewValue.setDrawingCacheBackgroundColor(Color.RED);
        textViewValue.setLinkTextColor(Color.BLUE);

        textViewValue.setTypeface(UIGlobal.APP_TYPEFACE);
        textViewValue.setTextSize(Font.getDefaultFont().getSize());
        textViewValue.setText(strValue);
        textViewValue.setTextColor(UIGlobal.TEXT_NORMAL);
        this.setOrientation(LinearLayout.VERTICAL);
        if (strName.equals("")) {
            this.addView(textViewValue, 0);
        } else {
            this.addView(textView, 0);
            this.addView(textViewValue, 1);
        }
    }

    public StringItem(String strName) {
        item = this;
        if (strName == null) {
            strName = "";
        }
        textView = new TextView(UIGlobal.APP_CONTEXT);
        textView.setTypeface(UIGlobal.APP_TYPEFACE, Typeface.BOLD);
        textView.setTextSize(Font.getDefaultFont().getSize());
        textView.setText(strName);
        textView.setTextColor(UIGlobal.TEXT_NORMAL);
        this.addView(textView, 0);

    }
    Item tmpitem;

    public StringItem(StringItem[] items) {
        item = this;

        this.setOrientation(LinearLayout.HORIZONTAL);

        int x = UIGlobal.DEVICE_WIDTH;
        x = x / items.length;
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                x, LayoutParams.WRAP_CONTENT);

        for (int i = 0; i < items.length; i++) {
            tmpitem = items[i];
            LinearLayout linearLayout = new LinearLayout(UIGlobal.APP_CONTEXT);
            linearLayout.setOrientation(LinearLayout.VERTICAL);

            textView = new TextView(UIGlobal.APP_CONTEXT);
            textView.setTypeface(UIGlobal.APP_TYPEFACE, Typeface.BOLD);
            textView.setTextSize(Font.getDefaultFont().getSize());
            textView.setTextColor(UIGlobal.TEXT_NORMAL);
            textView.setText(items[i].textView.getText());
            textView.setLayoutParams(params);
            textView.setGravity(Gravity.CENTER);
            textViewValue = new Button(UIGlobal.APP_CONTEXT);
            textViewValue.setClickable(true);
            textViewValue.setOnClickListener(new OnClickListener() {
                Item tmp = tmpitem;

                public void onClick(View vs) {
                    if (itemCommandListener != null) {
                        itemCommandListener.commandAction(tmp.defaultCommand, tmp);
                    }
                }
            });
//            textViewValue.setBackgroundColor(UIGlobal.BACKGROUND);
//            textViewValue.setHighlightColor(Color.GREEN);
            textViewValue.setTypeface(UIGlobal.APP_TYPEFACE);
            textViewValue.setTextSize(Font.getDefaultFont().getSize());
            textViewValue.setText(items[i].textViewValue.getText());
            textViewValue.setTextColor(UIGlobal.TEXT_NORMAL);
            textViewValue.setLayoutParams(params);
            linearLayout.addView(textView);
            linearLayout.addView(textViewValue);
            this.addView(linearLayout);
        }

    }

    public String getText() {
        if (this.textViewValue != null) {
            return textViewValue.getText().toString();
        } else {
            return getLabel();
        }
    }

    public void setText(String text) {
        if (this.textViewValue != null) {
            this.textViewValue.setText(text);
        } else {
            setLabel(text);
        }
    }

    public String getLabel() {
        return textView.getText().toString();
    }

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

    public View getView() {
        if (getText().equals("") || getText() == null) {
            return null;
        }
        return this;
    }
}
