package com.algobase.widgets;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;



public class IntegerPicker extends LinearLayout {

   public abstract static class OnValueChangedListener {
     public void onValueChanged(int x) {}
   }

     TextView tv_label;
     TextView tv_text;
     AutoRepeatButton button_plus;
     AutoRepeatButton button_minus;

     //int text_size = 16;
     int text_size = 17;
     int min_value = 0;
     int max_value = 99;
     int value = 0;

     String label = "";
     String unit = "";

     OnValueChangedListener listener = null;

     public void setOnValueChangedListener(OnValueChangedListener L) 
     { listener = L; }

     void new_value_handler(int value) {
        if (listener != null) listener.onValueChanged(value);
     }


   public void setMinValue(int x) { min_value = x; }
   public void setMaxValue(int x) { max_value = x; }

   public void setValue(int x) 
   { value = x; 
     if (unit.equals(""))
        tv_text.setText(String.format("%4d",value));
     else
        tv_text.setText(String.format("%2d %s",value,unit));
    }

   public int getValue() 
   { String str = tv_text.getText().toString();
     if (!unit.equals("")) str = str.replace(unit,"");
     return Integer.parseInt(str.trim());
    }

   public void setLabel(String s) 
   { label = s;
     tv_label.setText(s); 
    }

   public void setUnit(String s) 
   { unit = s;
     setValue(value);
    }

   public IntegerPicker(Context context) { 
      super(context);
      constructor(context);
   }

   public IntegerPicker(Context context, AttributeSet attrs) {
      super(context, attrs);
      constructor(context);
   }

   public IntegerPicker(Context context, AttributeSet attrs, int defStyle) {
      super(context, attrs, defStyle);
      constructor(context);
   }



   private int DipToPixels(float dp)
   { /*
       return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                                    dp, getResources().getDisplayMetrics());
      */
      float dpi = getResources().getDisplayMetrics().densityDpi;
      return (int)(0.5 + dp * (dpi/160f));
     }



   private void constructor(Context context)
   { 
     setOrientation(LinearLayout.HORIZONTAL);
     tv_label = new TextView(context);
     tv_label.setTextSize(text_size);
     tv_label.setSingleLine(true);
     //tv_label.setTextColor(Color.WHITE);
     //tv_label.setText("  " + label);

     tv_text = new TextView(context);
     tv_text.setTypeface(Typeface.MONOSPACE);
     tv_text.setTextSize(text_size);
     tv_text.setSingleLine(true);
     //tv_text.setTextColor(Color.WHITE);
     //tv_text.setText(String.format("%2d",val));

     button_plus = new AutoRepeatButton(context);
     button_plus.setTypeface(null,Typeface.BOLD);
     button_plus.setTextSize(24);
     button_plus.setPadding(-4,-14,0,0);
     button_plus.setText(" + ");
     button_plus.setOnClickListener(
          new OnClickListener() {
              @Override 
              public void onClick(View v) 
              { int val = getValue();
                if (val < max_value)
                { setValue(val+1);
                  new_value_handler(value);
                 }
               }
     });


     button_minus = new AutoRepeatButton(context);
     button_minus.setTypeface(null,Typeface.BOLD);
     button_minus.setTextSize(24);
     button_minus.setPadding(-4,-14,0,0);
     button_minus.setText(" - ");
     button_minus.setOnClickListener(
          new OnClickListener() {
              @Override 
              public void onClick(View v) 
              { int val = getValue();
                if (val > min_value)
                { setValue(val-1);
                  new_value_handler(value);
                 }
               }
     });


     LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
                   //DipToPixels(100), LinearLayout.LayoutParams.WRAP_CONTENT);
                     DipToPixels(96), LinearLayout.LayoutParams.WRAP_CONTENT);

     params1.leftMargin=DipToPixels(2);
     params1.rightMargin=DipToPixels(2);
     params1.topMargin=DipToPixels(2);

     LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(
                    DipToPixels(60), LinearLayout.LayoutParams.WRAP_CONTENT);
     params2.rightMargin=DipToPixels(2);
     params2.topMargin=DipToPixels(2);

     LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(
                     //DipToPixels(50),DipToPixels(50));
                     DipToPixels(45),DipToPixels(45));
     params3.topMargin=DipToPixels(3);

     addView(tv_label,params1);
     addView(tv_text,params2);
     addView(button_minus,params3);
     addView(button_plus,params3);
 }

}
