/*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 java.util.ArrayList;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MIDPListAdapter extends BaseAdapter {

	java.util.List<ListItem> arrayListItems;
    public boolean center = false;

    public MIDPListAdapter(java.util.List<ListItem> alListItems) {
        mContext = UIGlobal.APP_CONTEXT;
        this.arrayListItems = alListItems;

    }

    public int getCount() {
        return arrayListItems.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ListItem templistitem =  arrayListItems.get(position);
        MIDPListItemView itemview;
        itemview = new MIDPListItemView(mContext, templistitem.stringPart,
                templistitem.imagePart, templistitem.bSelected, templistitem.font, center);

        return itemview;

    }
    private Context mContext;
}

class MIDPListItemView extends LinearLayout {

    String stringSection;
    Image imageSection;
    boolean selected;
    TextView textViewStringSection;
    ImageView imageViewSection;

    public MIDPListItemView(Context context, String stringPart,
            Image imagePart, boolean bSelected, Font font, boolean center) {

        super(context);

        this.requestFocusFromTouch();
        this.stringSection = stringPart;
        this.imageSection = imagePart;
        this.selected = bSelected;
        if (center) {
            this.setOrientation(LinearLayout.VERTICAL);
        } else {
            this.setOrientation(LinearLayout.HORIZONTAL);
        }
        setPadding(0, 6, 0, 6);
        this.setMinimumHeight(30);

        LinearLayout.LayoutParams Params = new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        Params.setMargins(6, 0, 6, 0);

        if (imagePart != null) {
            imageViewSection = new ImageView(context);
            imageViewSection.setImageBitmap(imagePart.getBitmap());
            if (center) {
                imageViewSection.setScaleType(ScaleType.CENTER);
            }

            addView(imageViewSection, Params);
        }

        Params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

        LinearLayout PanelV = new LinearLayout(context);
        PanelV.setOrientation(LinearLayout.VERTICAL);
        PanelV.setGravity(Gravity.BOTTOM);

        textViewStringSection = new TextView(context);
        textViewStringSection.setTypeface(UIGlobal.APP_TYPEFACE);
        
        textViewStringSection.setTextSize(Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_ITALIC,
        				Font.SIZE_LARGE).getSize());
        
        textViewStringSection.setTextColor(UIGlobal.TEXT_NORMAL);
        textViewStringSection.setText(stringPart);
        if (font != null) {
            textViewStringSection.setTypeface(font.typeface);
        }

        if (center) {
            textViewStringSection.setGravity(Gravity.CENTER);
        }

        PanelV.addView(textViewStringSection);

        addView(PanelV, Params);
        if (center) {
            setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
        }



    }

    @Override
    public boolean isSelected() {
        return selected;
    }

    @Override
    public void setSelected(boolean selected) {
        this.selected = selected;
    }

    public String getStringPart() {
        return stringSection;
    }

    public void setStringPart(String stringPart) {
        this.stringSection = stringPart;
    }

    public Image getImagePart() {
        return imageSection;
    }

    public void setImagePart(Image imagePart) {
        this.imageSection = imagePart;
    }
}
