/*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.Iterator;

import android.R;
import android.graphics.Typeface;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ToggleButton;

public class ChoiceGroup extends Item implements Choice {

    String label;
    int choiceType;
    int fitPolicy;
    ArrayList listItems = new ArrayList();
    ArrayList checkBoxes = new ArrayList();
    ArrayList radioButtons = new ArrayList();
    TextView textView;
    RadioGroup radioGroup = new RadioGroup(UIGlobal.APP_CONTEXT);
    boolean bUpdate = true;
    boolean bUpdateSelection = true;
    ListChoice lc;
    int choice = 0;

    public ChoiceGroup(String label, int choiceType) {
        this.label = label;
        this.choiceType = choiceType;
        lc = new ListChoice(this, label);
        if (this.choiceType == Choice.POPUP) {

            butt = new Button(UIGlobal.APP_CONTEXT);

            butt.setTypeface(UIGlobal.APP_TYPEFACE);
            butt.setTextSize(Font.getDefaultFont().getSize());
            butt.setTextColor(UIGlobal.TEXT_NORMAL);
        }
        UpdateView();
    }

    public ChoiceGroup(String label, int choiceType, String[] stringElements,
            Image[] imageElements) {
        this.label = label;
        this.choiceType = choiceType;
        lc = new ListChoice(this, label, stringElements,
                imageElements != null ? imageElements : null);

        if (this.choiceType == Choice.POPUP) {

            butt = new Button(UIGlobal.APP_CONTEXT);

            butt.setTypeface(UIGlobal.APP_TYPEFACE);
            butt.setTextSize(Font.getDefaultFont().getSize());
            butt.setTextColor(UIGlobal.TEXT_NORMAL);
        }

        for (int i = 0; i < stringElements.length; i++) {
            ListItem templistitem = new ListItem(stringElements[i],
                    imageElements != null ? imageElements[i] : null, false);
            listItems.add(templistitem);
        }
        UpdateView();
    }

    public void UpdateView() {
        bUpdate = true;
    }

    public void UpdateSelection() {
        bUpdateSelection = true;
    }
    Button butt;

    public void UpdateDisplay() {

        this.setOrientation(LinearLayout.VERTICAL);
        this.removeAllViews();

        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(this.label);
        this.addView(textView, 0);

        if (this.choiceType == Choice.MULTIPLE) {
            checkBoxes = new ArrayList();
            Iterator itr = listItems.iterator();
            int i = 0;
            while (itr.hasNext()) {
                ListItem templistitem = (ListItem) itr.next();
                CheckBox checkbox = new CheckBox(UIGlobal.APP_CONTEXT);
                checkbox.setTypeface(UIGlobal.APP_TYPEFACE);
                checkbox.setTextSize(Font.getDefaultFont().getSize());
                checkbox.setTextColor(UIGlobal.TEXT_NORMAL);
                checkbox.append(templistitem.stringPart);
                checkbox.setChecked(templistitem.bSelected);
                checkBoxes.add(checkbox);
                checkbox.setId(i);
                this.addView(checkbox, i + 1);
                i++;
            }
        } else if (this.choiceType == Choice.POPUP) {
            Iterator itr = listItems.iterator();
            int i = 0;
            //lc = new ListChoice(this, label);
            butt = new Button(UIGlobal.APP_CONTEXT);

            butt.setTypeface(UIGlobal.APP_TYPEFACE);
            butt.setTextSize(Font.getDefaultFont().getSize());
            butt.setTextColor(UIGlobal.TEXT_NORMAL);


            while (itr.hasNext()) {
                ListItem templistitem = (ListItem) itr.next();
                if (lc.size() != listItems.size()) {
                    lc.append(templistitem.stringPart, templistitem.imagePart);
                }

                lc.setSelectedIndex(i, templistitem.bSelected);

                i++;
            }

            System.out.println("UpdateDisplay: " + getString(getSelectedIndex()));

            butt.append(getString(getSelectedIndex()));


            butt.setOnClickListener(new OnClickListener() {
                public void onClick(View vs) {
                    Instances.getUIInstance().setCurrent(lc);
                }
            });


            this.addView(butt, 1);

        } else {
            radioButtons = new ArrayList();
            Iterator itr = listItems.iterator();
            int i = 0;
            while (itr.hasNext()) {
                ListItem templistitem = (ListItem) itr.next();
                RadioButton radiobutton = new RadioButton(UIGlobal.APP_CONTEXT);
                radiobutton.setTypeface(UIGlobal.APP_TYPEFACE);
                radiobutton.setTextSize(Font.getDefaultFont().getSize());
                radiobutton.setTextColor(UIGlobal.TEXT_NORMAL);
                radiobutton.append(templistitem.stringPart);
                radiobutton.setChecked(templistitem.bSelected);
                radioButtons.add(radiobutton);
                radiobutton.setId(i);
                radioGroup.addView(radiobutton, i);
                i++;
            }
            this.addView(radioGroup, 1);
        }
    }

    public void UpdateSelectionDisplay() {
        if (this.choiceType == Choice.MULTIPLE) {
            for (int i = 0; i < listItems.size(); i++) {
                CheckBox checkbox = (CheckBox) checkBoxes.get(i);
                ListItem templistitem = (ListItem) listItems.get(i);
                if (templistitem.bSelected != checkbox.isChecked()) {
                    checkbox.setChecked(templistitem.bSelected);
                }
            }
        } else if (this.choiceType == Choice.POPUP) {
            lc.UpdateSelection();

            String text = getString(getSelectedIndex());
            //System.out.println("UpdateSelectionDisplay: " + text);
            char[] chars = text.toCharArray();
            butt.setText(chars, 0, chars.length);



        } else {
            for (int i = 0; i < listItems.size(); i++) {
                RadioButton radiobutton = (RadioButton) radioButtons.get(i);
                ListItem templistitem = (ListItem) listItems.get(i);
                if (templistitem.bSelected != radiobutton.isChecked()) {
                    radiobutton.setChecked(templistitem.bSelected);
                }
            }
        }
    }

    @Override
    public View getView() {
        if (bUpdate == true) {
            UpdateDisplay();
        }
        if (bUpdateSelection == true) {
            UpdateSelectionDisplay();
        }
        return this;
    }

    public void RefreshList() {
        if (this.choiceType == Choice.MULTIPLE) {
            for (int i = 0; i < listItems.size() && checkBoxes.size() != 0; i++) {
                CheckBox checkbox = (CheckBox) checkBoxes.get(i);
                ListItem templistitem = (ListItem) listItems.get(i);
                if (templistitem.bSelected != checkbox.isChecked()) {
                    templistitem.bSelected = checkbox.isChecked();
                    listItems.set(i, templistitem);
                }
            }
        } else if (this.choiceType == Choice.POPUP) {
            lc.RefreshList();

        } else {


            for (int i = 0; i < listItems.size() && radioButtons.size() != 0; i++) {
                RadioButton radiobutton = (RadioButton) radioButtons.get(i);
                ListItem templistitem = (ListItem) listItems.get(i);
                if (templistitem.bSelected != radiobutton.isChecked()) {
                    templistitem.bSelected = radiobutton.isChecked();
                    listItems.set(i, templistitem);
                }
            }
        }
    }

    public int append(String stringPart, Image imagePart) {
        ListItem templistitem = new ListItem(stringPart, imagePart, false);
        listItems.add(templistitem);
        UpdateView();
        if (this.choiceType == Choice.POPUP) {

            lc.append(stringPart, imagePart);

        }
        return (listItems.size() - 1);
    }

    public void delete(int elementNum) {
        listItems.remove(elementNum);

        UpdateView();
        if (this.choiceType == Choice.POPUP) {

            lc.delete(elementNum);

        }
    }

    public void deleteAll() {
        listItems.clear();
        UpdateView();
        if (this.choiceType == Choice.POPUP) {

            lc.deleteAll();

        }
    }

    public int getFitPolicy() {
        return 0;
    }

    // Font getFont(int elementNum)
    public Image getImage(int elementNum) {
        ListItem templistitem = (ListItem) listItems.get(elementNum);
        return templistitem.imagePart;
    }

    public int getSelectedFlags(boolean[] selectedArray_return) {
        RefreshList();


        if (this.choiceType == Choice.POPUP && lc.size() == listItems.size()) {

            return lc.getSelectedFlags(selectedArray_return);

        }


        int intSelectedCount = 0;
        int i = 0;
        Iterator itr = listItems.iterator();
        while (itr.hasNext()) {
            ListItem templistitem = (ListItem) itr.next();
            selectedArray_return[i] = templistitem.bSelected;
            if (templistitem.bSelected) {
                intSelectedCount++;
            }
        }
        return intSelectedCount;
    }

    public int getSelectedIndex() {
        // (this.choiceType == Choice.POPUP || this.choiceType == Choice.EXCLUSIVE) {
        return this.choice;
        // }
/*
         RefreshList();

         if (this.choiceType == Choice.POPUP && lc.size() == listItems.size()) {

         return lc.getSelectedIndex();

         }
         int i = 0;
         Iterator itr = listItems.iterator();
         while (itr.hasNext()) {
         ListItem templistitem = (ListItem) itr.next();
         if (templistitem.bSelected == true) {
         return i;
         }
         i++;
         }
         return i;
         */
    }

    public String getString(int elementNum) {


        ListItem templistitem = (ListItem) listItems.get(elementNum);
        return templistitem.stringPart;
    }

    public void insert(int elementNum, String stringPart, Image imagePart) {
        ListItem templistitem = new ListItem(stringPart, imagePart, false);
        listItems.add(elementNum, templistitem);
        UpdateView();
        if (this.choiceType == Choice.POPUP) {

            lc.insert(elementNum, stringPart, imagePart);

        }
    }

    public boolean isSelected(int elementNum) {

        if (this.choiceType == Choice.POPUP) {
            if (lc.size() == 0) {
                return false;
            } else if (elementNum < 0 || elementNum > lc.size()) {
                return false;
            } else {
                RefreshList();
                ListItem templistitem = (ListItem) listItems.get(elementNum);
                return templistitem.bSelected;
            }

        } else {
            if (listItems.size() == 0) {
                return false;
            } else if (elementNum < 0 || elementNum > listItems.size()) {
                return false;
            } else {
                RefreshList();
                ListItem templistitem = (ListItem) listItems.get(elementNum);
                return templistitem.bSelected;
            }
        }

    }

    public void set(int elementNum, String stringPart, Image imagePart) {
        ListItem templistitem = new ListItem(stringPart, imagePart, false);
        listItems.set(elementNum, templistitem);
        UpdateView();
        if (this.choiceType == Choice.POPUP) {

            lc.set(elementNum, stringPart, imagePart);

        }
    }

    public void setFont(int elementNum, Font font) {
    }

    public void setSelectedFlags(boolean[] selectedArray) {
        if (this.choiceType == Choice.POPUP) {

            lc.setSelectedFlags(selectedArray);
        }

        for (int i = 0; i < listItems.size(); i++) {
            ListItem templistitem = (ListItem) listItems.get(i);
            templistitem.bSelected = selectedArray[i];
            listItems.set(i, templistitem);
            if (templistitem.bSelected) {
                choice = i;
            }
            if (templistitem.bSelected && this.choiceType == Choice.POPUP) {

                String text = templistitem.stringPart;
                //System.out.println("setSelectedFlags: " + text);
                char[] chars = text.toCharArray();
                butt.setText(chars, 0, chars.length);
            }

        }
        UpdateSelection();

        //System.out.println("setFlags(), selected flag is "+this.getSelectedIndex());

    }

    public int size() {
        return listItems.size();
    }

    @Override
    public void setSelectedIndex(int elementNum, boolean selected) {
        // TODO Auto-generated method stub
        if (selected) {
            choice = elementNum;
        }

        if (this.choiceType == Choice.POPUP) {

            lc.setSelectedIndex(elementNum, selected);
        }

        for (int i = 0; i < listItems.size(); i++) {
            ListItem templistitem = (ListItem) listItems.get(i);
            if (elementNum == i) {
                templistitem.bSelected = selected;
                listItems.set(i, templistitem);


                if (templistitem.bSelected && this.choiceType == Choice.POPUP) {

                    String text = templistitem.stringPart;
                    //System.out.println("setSelectedIndex: " + text);
                    char[] chars = text.toCharArray();
                    butt.setText(chars, 0, chars.length);

                }



            }
        }
        UpdateSelection();
    }

    @Override
    public void setFitPolicy(int fitPolicy) {
        // TODO Auto-generated method stub
        this.fitPolicy = fitPolicy;
    }

    @Override
    public Font getFont(int elementNum) {
        // TODO Auto-generated method stub
        return null;
    }
    public boolean quick = !true;

    final class ListChoice extends List implements CommandListener {
////////////////////////////

        ChoiceGroup ch;

        ListChoice(ChoiceGroup ch, String titlePart, String[] stringPart, Image[] imagePart) {
            super(titlePart, List.IMPLICIT, stringPart, imagePart);
            this.ch = ch;
            super.setCommandListener(this);
            super.setQuickSearch(quick);
        }

        ListChoice(ChoiceGroup ch, String titlePart) {
            super(titlePart, List.IMPLICIT);
            this.ch = ch;
            super.setCommandListener(this);
            super.setQuickSearch(quick);
        }

        public final void commandAction(Command c, Displayable d) {
            ch.setSelectedIndex(super.getSelectedIndex(), true);
            //String text = super.getString(super.getSelectedIndex());
            // char[] chars = text.toCharArray();
            //  ch.butt.setText(chars, 0, chars.length);

            Instances.getUIInstance().setCurrent(Instances.getUIInstance().PREV_DISPLAYABLE);
        }
//////////////////////////
    }
}
