package com.dubiousoft.sakana;

import android.app.Activity;
import android.content.Context;
import android.graphics.Point;
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 ReticleView extends FrameLayout 
	implements OnTouchListener
{
	private int m_minSize = 150;
	private int m_maxSize = 240;
	private int m_currentSize = m_minSize;
	private int m_destSize = m_minSize;
	private Point m_dragPosition;
	
	public ReticleView(final Context context) {
        this(context, null);
    }

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

    public ReticleView(final Context context, final AttributeSet attrs,
            final int defStyle) {
        super(context, attrs, defStyle);
        m_currentSize = m_destSize - 8;	// hack
    }

    public void onUiUpdate() {
		calculateSize();
		ImageView reticule = (ImageView) findViewById(R.id.reticule_1);
		reticule.setRotation( reticule.getRotation() + 1 );
		reticule = (ImageView) findViewById(R.id.reticule_2);
		reticule.setRotation( (float) (reticule.getRotation() - 1.5) );
    }
    
    public Point getPosition() {
    	return new Point( (int)(getX() + m_currentSize/2.0), (int)(getY() + m_currentSize/2.0) );
    }
    
    private void calculateSize() {
    	if (m_currentSize == m_destSize) {
    		return;
    	}
    	
    	final int HALF_DIFF = 4;
    	if (m_currentSize < m_destSize) {
    		m_currentSize += 2*HALF_DIFF;
    		if (m_currentSize > m_destSize) {
    			m_currentSize = m_destSize;
    		}
    		setX( getX() - HALF_DIFF );
    		setY( getY() - HALF_DIFF );    		
    	}
    	else if (m_currentSize > m_destSize) {
    		m_currentSize -= 2*HALF_DIFF;
    		if (m_currentSize < m_destSize) {
    			m_currentSize = m_destSize;
    		}
    		setX( getX() + HALF_DIFF );
    		setY( getY() + HALF_DIFF );    		
    	}
		LayoutParams layout = new LayoutParams( m_currentSize, m_currentSize );
		ImageView reticule = (ImageView) findViewById(R.id.reticule_0);
    	reticule.setLayoutParams( layout );
		reticule = (ImageView) findViewById(R.id.reticule_1);
    	reticule.setLayoutParams( layout );
		reticule = (ImageView) findViewById(R.id.reticule_2);
    	reticule.setLayoutParams( layout );
    }
    
    @Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		((Activity)getContext()).getLayoutInflater().inflate(R.layout.reticle_view, this);

		ImageView reticule = (ImageView) findViewById(R.id.reticule_0);
    	reticule.setX( 0 );
    	reticule.setY( 0 );
		reticule = (ImageView) findViewById(R.id.reticule_1);
    	reticule.setX( 0 );
    	reticule.setY( 0 );
		reticule = (ImageView) findViewById(R.id.reticule_2);
    	reticule.setX( 0 );
    	reticule.setY( 0 );
    	calculateSize();
    	
    	setOnTouchListener( this );
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			{
				int [] global = new int[2];
				getLocationOnScreen( global );
				m_dragPosition = new Point( (int)event.getX()+global[0], (int)event.getY()+global[1] );
				m_destSize = m_maxSize;
			}
			break;
		case MotionEvent.ACTION_UP:
			m_destSize = m_minSize;
			break;
		case MotionEvent.ACTION_MOVE:
			{
				int [] global = new int[2];
				getLocationOnScreen( global );
				Point position = new Point( (int)event.getX()+global[0], (int)event.getY()+global[1] );
				Point diff = new Point( position.x-m_dragPosition.x, position.y-m_dragPosition.y );
				setX( getX() + diff.x );
				setY( getY() + diff.y );
				m_dragPosition = position;
			}
			break;
		}
		return true;
	}
}
