/*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.graphics.drawable.ClipDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Gauge extends Item {

    private String label;
    int min, max;
    private TextView textView;
    private ProgressBar bar;
    Item item;

    public Gauge(String label) {
        item = this;

        try {
            this.label = label;
            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);
            textView.setGravity(Gravity.CENTER);
            textView.setText(label);
            this.addView(textView);
            /*
             Drawable background = new ColorDrawable(Color.BLACK);
             Drawable progress = new ColorDrawable(Color.GRAY);
             ClipDrawable clipProgress = new ClipDrawable(progress, Gravity.LEFT, ClipDrawable.HORIZONTAL);
             LayerDrawable layerlist = new LayerDrawable(new Drawable[]{background, clipProgress});
             layerlist.setId(0, android.R.id.background);
             layerlist.setId(1, android.R.id.progress);
             */
            bar = new ProgressBar(UIGlobal.APP_CONTEXT, null, android.R.attr.progressBarStyleHorizontal);
            bar.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                    LayoutParams.WRAP_CONTENT));
//            bar.setBackgroundColor(UIGlobal.TEXT_NORMAL);
//            bar.setProgressDrawable(layerlist);
            bar.setVisibility(VISIBLE);
            bar.requestLayout();
            this.addView(bar);



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

    public Gauge(String label, boolean interactive, int min,
            int max) {
        this(label);

        setMax(max);
        setValue(min);

    }

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

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

    public int getValue() {
        return min;
    }

    public void setValue(int min) {
        this.min = min;
        bar.setProgress(min);
//        bar.incrementProgressBy(1);
//        bar.invalidate();
//        bar.postInvalidate();

//        


    }

    public int getMax() {
        return max;
    }

    public void setMax(int max) {
        this.max = max;
        bar.setMax(max);
    }
}
