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

public class ImageItem extends Item {
    /*
     public final static int LAYOUT_CENTER = 1;
     public final static int LAYOUT_DEFAULT = 2;
     public final static int LAYOUT_LEFT = 3;
     //    public final static int LAYOUT_NEWLINE_AFTER = 4;
     //    public final static int LAYOUT_NEWLINE_BEFORE = 5;
     public final static int LAYOUT_RIGHT = 6;
     */

    private Image image;
    private String label, altText;
    private int layout;
    private int appearanceMode;
    private TextView textView;
    private ImageButton imageView;
    ImageItem item;
    LinearLayout.LayoutParams params;
    int mode = 0;

    public ImageItem(String label, Image image, int layout, String altText,
            int appearanceMode) {
        this(label, image, layout, altText);
        this.appearanceMode = appearanceMode;
    }

    public ImageItem(String label, Image image, int layout, String altText) {
        item = this;
        mode = 0;

        try {
            this.label = label;
            this.image = image;
            this.layout = layout;
            super.text = this.altText = altText;
            this.setOrientation(LinearLayout.VERTICAL);




            imageView = new ImageButton(UIGlobal.APP_CONTEXT);
//            imageView.setBackgroundColor(UIGlobal.BACKGROUND);
            imageView.setClickable(true);
            if (image != null) {
                imageView.setImageBitmap(image.getBitmap());
            }



            textView = new TextView(UIGlobal.APP_CONTEXT);
            textView.setTypeface(UIGlobal.APP_TYPEFACE, Typeface.BOLD);
            textView.setTextSize(Font.getDefaultFont().getSize());
            textView.setTextColor(UIGlobal.TEXT_NORMAL);
            params = new LinearLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            params.gravity = Gravity.CENTER;

            if (LAYOUT_LEFT == layout) {
                textView.setGravity(params.gravity = Gravity.LEFT);
            } else if (LAYOUT_RIGHT == layout) {
                textView.setGravity(params.gravity = Gravity.RIGHT);
            } else {
                textView.setGravity(params.gravity = Gravity.CENTER);
            }


            imageView.setLayoutParams(params);
           

            imageView.setOnClickListener(new OnClickListener() {
                ImageItem tmp = item;

                public void onClick(View vs) {
                    if (itemCommandListener != null) {


                        itemCommandListener.commandAction(tmp.defaultCommand, tmp);
                    }
                }
            });
            this.addView(textView);

            this.addView(imageView);

            if (altText != null) {
                setAltText(altText);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    ImageItem tmpitem;
    ImageItem[] items;

    public ImageItem(ImageItem[] it) {
        if (it == null || it.length == 0) {
            return;
        }
        mode = 1;

        item = this;
        this.items = it;
        try {
            this.label = "";
            this.image = null;
            this.layout = Item.LAYOUT_CENTER;
            super.text = this.altText = "";

            this.setOrientation(LinearLayout.HORIZONTAL);

            int x = UIGlobal.DEVICE_WIDTH;
            x = x / items.length;


            for (int i = 0; i < items.length; i++) {
                tmpitem = items[i].item;
                System.out.println(tmpitem.textView.getText());

                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(tmpitem.textView.getText());



                imageView = new ImageButton(UIGlobal.APP_CONTEXT);
//                imageView.setBackgroundColor(UIGlobal.BACKGROUND);
                imageView.setClickable(true);
                if (tmpitem.image != null) {
                    imageView.setImageBitmap(tmpitem.image.getBitmap());
                }

                params = new LinearLayout.LayoutParams(
                        x, LayoutParams.WRAP_CONTENT);
                params.gravity = Gravity.CENTER;


                textView.setGravity(params.gravity = Gravity.CENTER);

                imageView.setLayoutParams(params);

                imageView.setOnClickListener(new OnClickListener() {
                    ImageItem tmp = tmpitem;

                    public void onClick(View vs) {
                        if (itemCommandListener != null) {

                            itemCommandListener.commandAction(tmp.defaultCommand, tmp);
                        }
                    }
                });
                linearLayout.addView(textView);
                linearLayout.addView(imageView);
                this.addView(linearLayout);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public String getLabel() {
        return label;
    }

    @Override
    public void setLabel(String label) {
        this.label = label;
    }

    public Image getImage() {
        return this.image;
    }

    public void setImage(Image image) {
        if (image == null) {
            return;
        }
        this.image = image;
        imageView.setImageBitmap(image.getBitmap());
    }

//     imageView.setImageBitmap(image.getBitmap());
    public void setAltText(String altText) {
        super.text = this.altText = altText;
        textView.setText(altText);
    }

    public String getAltText() {
        return altText;
    }

    public int getLayout() {
        return layout;
    }

    public void setLayout(int layout) {
        this.layout = layout;
        params.gravity = Gravity.CENTER;

        if (LAYOUT_LEFT == layout) {
            textView.setGravity(params.gravity = Gravity.LEFT);
        } else if (LAYOUT_RIGHT == layout) {
            textView.setGravity(params.gravity = Gravity.RIGHT);
        } else {
            textView.setGravity(params.gravity = Gravity.CENTER);
        }
        imageView.setLayoutParams(params);
    }

    public int getAppearanceMode() {
        return appearanceMode;
    }

    public void setAppearanceMode(int appearanceMode) {
        this.appearanceMode = appearanceMode;
    }

    public View getView() {

        this.removeAllViews();
        if (mode == 0) {
            if (image == null) {
                return null;
            }
            imageView = new ImageButton(UIGlobal.APP_CONTEXT);
//            imageView.setBackgroundColor(UIGlobal.BACKGROUND);
            imageView.setClickable(true);
            if (image != null) {
                imageView.setImageBitmap(image.getBitmap());
            }
            imageView.setOnClickListener(new OnClickListener() {
                ImageItem tmp = item;

                public void onClick(View vs) {
                    if (itemCommandListener != null) {


                        itemCommandListener.commandAction(tmp.defaultCommand, tmp);
                    }
                }
            });

            this.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);
            params = new LinearLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            params.gravity = Gravity.CENTER;

            if (LAYOUT_LEFT == layout) {
                textView.setGravity(params.gravity = Gravity.LEFT);
            } else if (LAYOUT_RIGHT == layout) {
                textView.setGravity(params.gravity = Gravity.RIGHT);
            } else {
                textView.setGravity(params.gravity = Gravity.CENTER);
            }
            imageView.setLayoutParams(params);
            this.addView(textView);

            this.addView(imageView);

            if (altText != null) {
                setAltText(altText);
            }
        } else if (mode == 1) {
            this.setOrientation(LinearLayout.HORIZONTAL);

            int x = UIGlobal.DEVICE_WIDTH;
            x = x / items.length;


            for (int i = 0; i < items.length; i++) {
                tmpitem = items[i].item;
                System.out.println(tmpitem.textView.getText());

                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(tmpitem.textView.getText());



                imageView = new ImageButton(UIGlobal.APP_CONTEXT);
//                imageView.setBackgroundColor(UIGlobal.BACKGROUND);
                imageView.setClickable(true);
                if (tmpitem.image != null) {
                    imageView.setImageBitmap(tmpitem.image.getBitmap());
                }

                params = new LinearLayout.LayoutParams(
                        x, LayoutParams.WRAP_CONTENT);
                params.gravity = Gravity.CENTER;


                textView.setGravity(params.gravity = Gravity.CENTER);

                imageView.setLayoutParams(params);

                imageView.setOnClickListener(new OnClickListener() {
                    ImageItem tmp = tmpitem;

                    public void onClick(View vs) {
                        if (itemCommandListener != null) {


                            itemCommandListener.commandAction(tmp.defaultCommand, tmp);
                        }
                    }
                });
                linearLayout.addView(textView);
                linearLayout.addView(imageView);
                this.addView(linearLayout);
            }

        }
        return this;
    }
}
