/*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.bluetooth.BluetoothClass.Device;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;

public abstract class Displayable extends LinearLayout {

    public ArrayList<Command> aListCommands = new ArrayList<Command>();
    public CommandListener commandListener;
    public ItemCommandListener itemCommandListener;
    public ItemStateListener itemStateListener;
    Ticker ticker;
    int height;
    String title;
    int width;
    boolean shown;
    boolean bUpdate = false;
    //Device device;

    public Displayable() {
        super(UIGlobal.APP_CONTEXT);
        width =UIGlobal.DEVICE_WIDTH;
        height  = UIGlobal.DEVICE_HEIGHT;
       
    }

    public String getTitle() {
        return title;
    }

    public void UpdateView() {
//        bUpdate = true;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public boolean isShown(boolean shown) {
        return shown;
    }

    public void addCommand(Command cmd) {
        aListCommands.add(aListCommands.size(), cmd);
    }

    public void removeCommand(Command cmd) {
        aListCommands.remove(cmd);
    }

    public View getView() {
//        bUpdate = false;
        return this;
    }

    public void arrangeCommands() {
        boolean bBackFound = false;
        boolean bCancelFound = false;
        boolean bExitFound = false;

        Iterator<Command> itr = aListCommands.iterator();
        Command cmdToRemove = null;
        while (itr.hasNext()) {
            Command cmditem =  itr.next();
            if (cmditem.getLabel().toUpperCase().equals("BACK")) {
                bBackFound = true;
                cmdToRemove = cmditem;
            }
        }

        if (bBackFound == true) {
            if (aListCommands.indexOf(cmdToRemove) != (aListCommands.size() - 1)) {
                aListCommands.remove(cmdToRemove);
                aListCommands.add(cmdToRemove);
                return;
            }
        }

        itr = aListCommands.iterator();
        cmdToRemove = null;
        while (itr.hasNext()) {
            Command cmditem =  itr.next();
            if (cmditem.getLabel().toUpperCase().equals("CANCEL")) {
                bCancelFound = true;
                cmdToRemove = cmditem;
            }
        }

        if (bCancelFound == true) {
            if (aListCommands.indexOf(cmdToRemove) != (aListCommands.size() - 1)) {
                aListCommands.remove(cmdToRemove);
                aListCommands.add(cmdToRemove);
                return;
            }
        }

        itr = aListCommands.iterator();
        cmdToRemove = null;
        while (itr.hasNext()) {
            Command cmditem =  itr.next();
            if (cmditem.getLabel().toUpperCase().equals("EXIT")) {
                bExitFound = true;
                cmdToRemove = cmditem;
            }
        }

        if (bExitFound == true) {
            if (aListCommands.indexOf(cmdToRemove) != (aListCommands.size() - 1)) {
                aListCommands.remove(cmdToRemove);
                aListCommands.add(cmdToRemove);
                return;
            }
        }

    }

    public void setCommandListener(CommandListener l) {
        this.commandListener = l;
    }

    public CommandListener getCommandListener() {
        return this.commandListener;
    }

    public void setItemCommandListener(ItemCommandListener l) {
        this.itemCommandListener = l;
    }

    public ItemCommandListener getItemCommandListener() {
        return this.itemCommandListener;
    }

    public void setItemStateListener(ItemStateListener l) {
        this.itemStateListener = l;
    }

    public ItemStateListener getItemStateListener() {
        return this.itemStateListener;
    }

    public Ticker getTicker() {
        return ticker;
    }

    public void setTicker(Ticker ticker) {
        this.ticker = ticker;
    }

    protected void sizeChanged(int w, int h) {
        updateWidthAndHeight();
        sizeChanged(width, height);
    }

    private void updateWidthAndHeight() {
        // Should be implemented
    }
}
