package com.example.lessonThree;

import android.graphics.*;
import android.graphics.drawable.*;
import android.graphics.drawable.shapes.*;

public class SpaceCreature 
{
    private long _lastTime;
	private boolean _isWalking;
	private boolean _isGoingUp;
	
	float _legLength;
	float _restingFootPosition;
	float _restingLegPosition;
	
	float _rightFootPosition;
	float _rightLegPosition;
	
	public SpaceCreature()
	{
		_legLength = 65;
		_restingLegPosition = 65;		
		_restingFootPosition = 130;
		_rightLegPosition = _restingLegPosition;
		_rightFootPosition = _restingFootPosition;
		
		SetWalking(true);
    	_lastTime = System.currentTimeMillis() + 100;
	}
	
	public void SetWalking(boolean isWalking)
	{
		_isWalking = isWalking;
		_isGoingUp = true;
	}
	
	public void DoSomething()
	{
		if ( _isWalking )
		{
	        long now = System.currentTimeMillis();
	        if (_lastTime > now) 
	        	return;
	        
	        double elapsed = (now - _lastTime) / 1000.0;            
	     
	        if (elapsed > .05)
	        {
	            _lastTime = now;
	            
	            if (_isGoingUp)
	            {
	            	if ( _rightLegPosition > (_restingLegPosition - 10) )
	            	{
	            		_rightLegPosition = _rightLegPosition - 1;
	            		_rightFootPosition = _rightFootPosition - 1;
	            		return;
	            	}
	            	else
	            		_isGoingUp = false;
	            }

            	if ( _rightLegPosition != _restingLegPosition )
            	{
            		_rightLegPosition = _rightLegPosition + 1;
            		_rightFootPosition = _rightFootPosition + 1;            		
            		return;
            	}
            	else
            		_isGoingUp = true;
	        }
		}
	}
	
	public void Draw(Canvas canvas)
	{
    	// MIX MY PAINT
    	Paint greenPaint = new Paint();
        greenPaint.setStrokeWidth(5);
        greenPaint.setColor(Color.rgb(124, 216, 96));
    	        
       	// DRAW THE alienBody
        ShapeDrawable alienBody = new ShapeDrawable(new OvalShape());
        alienBody.getPaint().setColor(greenPaint.getColor());	
        // 1st Number = left corner
        // 2nd Number = top corner
        // 3rd Number = right corner
        // 4th Number = bottom corner
        alienBody.setBounds(50, 50, 100, 100);        
        alienBody.draw(canvas); 
                     
        // DRAW THE LEFT AND RIGHT ARMS
        // start x (right-left)
        // start y (up-down)
        // end x
        // end y
        canvas.drawLine(52, 65, 52, 120, greenPaint);
        canvas.drawLine(98, 65, 98, 120, greenPaint);
        
        // DRAW THE LEGS
        canvas.drawLine(65, 65, 65, 130, greenPaint);
        canvas.drawLine(85, _rightLegPosition, 85, _rightLegPosition + _legLength, greenPaint);
                
        // DRAW THE FEET
        canvas.drawLine(60, 130, 70, 130, greenPaint);
        canvas.drawLine(83, _rightFootPosition, 92, _rightFootPosition, greenPaint);

        // DRAW THE EYE
        ShapeDrawable leftEye = new ShapeDrawable(new OvalShape());
        leftEye.getPaint().setColor(Color.WHITE);	
        leftEye.setBounds(60, 60, 80, 80);        
        leftEye.draw(canvas);
        
        // DRAW THE PUPIL
        ShapeDrawable pupil = new ShapeDrawable(new OvalShape());
        pupil.getPaint().setColor(Color.BLACK);	
        pupil.setBounds(68, 70, 73, 75);        
        pupil.draw(canvas);             		
	}
}