/*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 java.util.List;

import android.R;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.TextUtils.TruncateAt;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Scroller;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

public class Form extends Screen {

    private String title;
    ArrayList<Item> items = new ArrayList<Item>();
//    boolean bUpdate = true;

    public Form(String title) {
        this.items = new ArrayList<Item>();
        this.title = title;
    }

    public Form(String title, Item[] items) {
        this(title);
        for (int i = 0; i < items.length; i++) {
            append(items[i]);
        }
        UpdateView();
    }

    @Override
    public String getTitle() {
        return this.title;
    }

    public void setTitle(String title) {
        this.title = title;

    }

    public int append(Image icon) {
        Item item = new ImageItem(
                "", icon,
                Item.LAYOUT_CENTER, "",
                Item.BUTTON);
        this.items.add(item);
        UpdateView();
        return this.items.size() - 1;

    }

    public int append(Item item) {
        this.items.add(item);
        UpdateView();
        return this.items.size() - 1;

    }

    public int append(String text) {
        StringItem stringitem = new StringItem(text);
        this.items.add(stringitem);
        UpdateView();
        return this.items.size() - 1;
    }

    public void UpdateView() {
        bUpdate = true;
    }
    ScrollView scrollView = new ScrollView(UIGlobal.APP_CONTEXT);
    LinearLayout linearLayout = new LinearLayout(UIGlobal.APP_CONTEXT);

    @Override
    public View getView() {

        if (bUpdate == true) {
            scrollView.invalidate();
            scrollView.removeAllViews();

            linearLayout.removeAllViews();


            scrollView.setLayoutParams(new LayoutParams(
                    LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

            scrollView.setBackgroundColor(UIGlobal.BACKGROUND);
            
            
            linearLayout.requestFocusFromTouch();
            linearLayout.setLayoutParams(new LayoutParams(
                    LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
            linearLayout.setOrientation(LinearLayout.VERTICAL);

            //linearLayout.setBackgroundColor(UIGlobal.BACKGROUND);
            int iControlIndex = 0;
            TextView textView = new TextView(UIGlobal.APP_CONTEXT);
            textView.setText(this.title);
            textView.setTypeface(UIGlobal.APP_TYPEFACE, Typeface.BOLD);
            textView.setTextSize(Font.getDefaultFont().getSize());
            textView.setTextColor(UIGlobal.TEXT_NORMAL);

            textView.setGravity(Gravity.CENTER);


            linearLayout.addView(textView, iControlIndex);
            iControlIndex++;

            if (this.ticker != null) {

                TextView tvTicker = new TextView(UIGlobal.APP_CONTEXT);
                tvTicker.setEllipsize(TruncateAt.MARQUEE);
                tvTicker.setMarqueeRepeatLimit(-1);
                tvTicker.setTypeface(UIGlobal.APP_TYPEFACE);
                tvTicker.setTextSize(Font.getDefaultFont().getSize());
                tvTicker.setTextColor(Color.RED);
                tvTicker.setText(this.ticker.getString());

                linearLayout.addView(tvTicker, iControlIndex);
                iControlIndex++;
            }
            for (int i = 0; i < this.items.size(); i++) {
                Item item = (Item) this.items.get(i);
                if (item.getParent() != null) {
                }

//               if(item instanceof StringItem) System.out.println("'"+getTitle()+"' "+item.getText());
                View v = item.getView();
                if (v == null) {
                    continue;
                }
                linearLayout.addView(v, iControlIndex);
                iControlIndex++;
            }
            scrollView.addView(linearLayout);
            bUpdate = false;
            linearLayout.setVisibility(VISIBLE);
            linearLayout.requestLayout();
            scrollView.invalidate();
        }
        //scrollView.setBackgroundResource(UIGlobal.bg);
        return scrollView;
    }

    public Item get(int pos) {
        return (Item) this.items.get(pos);
    }

    public void delete(int itemNum) {
        this.items.remove(itemNum);
        UpdateView();
    }

    public void deleteAll() {
        this.items.clear();
        UpdateView();
    }

    public void insert(int itemNum, Item item) {
        this.items.add(itemNum, item);
        UpdateView();
    }

    public void set(int itemNum, Item item) {
        this.items.set(itemNum, item);
        UpdateView();
    }

    public int size() {
        return this.items.size();
    }
}
