package com.dubiousoft.sakana;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.FrameLayout;
import android.widget.ImageView;

public class SliderView extends FrameLayout
 	implements OnTouchListener {

	boolean m_sliderDown = false;
	int m_dragY = 0;
	int MIN_SLIDER_POS;
	int MAX_SLIDER_POS;
	
	public SliderView(final Context context) {
        this(context, null);
    }

    public SliderView(final Context context, final AttributeSet attrs) {
        this(context, attrs, R.style.AppTheme);
    }

    public SliderView(final Context context, final AttributeSet attrs,
            final int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		((Activity)getContext()).getLayoutInflater().inflate(R.layout.slider_view, this);
    }
    
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    	super.onLayout( changed, left, top, right, bottom );
    	
    	FrameLayout frame = (FrameLayout) findViewById(R.id.slider_frame);    	
    	ImageView slider = (ImageView) findViewById(R.id.slider_1);
    	// Calling setLayoutParams from inside onLayout causes a warning spew.
    	// We only need to call it once to set the height.
    	if (changed) {
    		slider.setLayoutParams( new LayoutParams(slider.getWidth(), (int)(frame.getHeight()/5.0) ) );
    	}
		slider.setX( (int)(getWidth()/2.0 - slider.getWidth()/2.0) );
		slider.setY( (int)(frame.getHeight()/2.0 - slider.getHeight()/2.0) );

		// The image for the slider has a bit of a white border, this is
		// to correct for that so it goes all the way to the top and bottom.
		int sliderWhiteSpace = slider.getHeight()/10;
		MIN_SLIDER_POS = -sliderWhiteSpace;
		MAX_SLIDER_POS = frame.getHeight()-slider.getHeight()+sliderWhiteSpace;
		
		slider.setOnTouchListener( this );    	
    }

	@Override
	public boolean onTouch(View view, MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			m_sliderDown = true;
			{
				ImageView slider = (ImageView) findViewById(R.id.slider_1);
				int [] global = new int[2];
				slider.getLocationOnScreen( global );
				m_dragY = global[1] + (int)event.getY();
			}
			break;
		case MotionEvent.ACTION_UP:
			m_sliderDown = false;
			break;
		case MotionEvent.ACTION_MOVE:
			if (m_sliderDown) {
				ImageView slider = (ImageView) findViewById(R.id.slider_1);
				int [] global = new int[2];
				slider.getLocationOnScreen( global );

				int newY = (int)event.getY() + global[1];
				int diff = newY - m_dragY;
				slider.setY( slider.getY() + diff );
				m_dragY = newY;
				
				if (slider.getY() < MIN_SLIDER_POS) {
					slider.setY( MIN_SLIDER_POS );
				}
				else if (slider.getY() > MAX_SLIDER_POS) {
					slider.setY( MAX_SLIDER_POS );
				}		
			}
			break;
		}
		return true;
	}
	
	public float getSliderPos() {
		ImageView slider = (ImageView) findViewById(R.id.slider_1);
		float y = slider.getY() - MIN_SLIDER_POS;
		
		float retVal = y/(MAX_SLIDER_POS - MIN_SLIDER_POS);
		return retVal;
	}
    
	public void setTopIcon( int resourceId ) {
		ImageView icon = (ImageView) findViewById( R.id.slider_top_icon );
		icon.setImageResource( resourceId );
	}

	public void setBottomIcon( int resourceId ) {
		ImageView icon = (ImageView) findViewById( R.id.slider_bottom_icon );
		icon.setImageResource( resourceId );
	}
}
