package com.mydoodle;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

public class DrawingView extends View {

	private Paint paint = new Paint();
	private Path path = new Path();
	private Canvas canvas = null;
	private Point point = new Point();
	private boolean isFirstPoint = true;
	
	public DrawingView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		

		
		// TODO Auto-generated constructor stub
	}

	
	public DrawingView(Context context) {
		super(context);

		// TODO Auto-generated constructor stub
	}

	public DrawingView(Context context, AttributeSet attrs) {
		super(context, attrs);
		Toast.makeText(getContext(), "DrawingView(Context context)", Toast.LENGTH_SHORT).show();
		paint.setAntiAlias(true);
		paint.setStrokeWidth(6f);
		paint.setColor(Color.BLACK);
		
		paint.setStyle(Paint.Style.STROKE);
		paint.setStrokeJoin(Paint.Join.ROUND);
		setBackgroundColor(Color.WHITE);
		// TODO Auto-generated constructor stub
	}

	public void clear()
	{
		paint.setColor(Color.BLACK);
		
		invalidate();
		//paint.setColor(Color.WHITE);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		this.canvas = canvas;
		//super.onDraw(canvas);
		
		
		canvas.drawPath(path, paint);
	}

	public void changeColor(int color)
	{
		paint.setColor(color);
		
		//path.reset();
		
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		
		//int action = event.getAction();
		//MotionEvent.ACTION_CANCEL;
		//MotionEvent.ACTION_DOWN;
		//MotionEvent.ACTION_MASK;
		//MotionEvent.ACTION_MOVE;
		
		float eventX = event.getX();
		float eventY = event.getY();
		
		int actionIndex = event.getActionIndex();
		//Toast.makeText(getContext(), "actionIndex:"+actionIndex, Toast.LENGTH_SHORT).show();
		Log.e("onTouchEvent:actionIndex", ""+event.getPointerId(actionIndex));
		int actionMasked = event.getActionMasked();
		if(actionMasked == MotionEvent.ACTION_DOWN)
		{
			//path = new Path();
			path.moveTo(eventX, eventY);
			if(!isFirstPoint) {
				point.x = (int)eventX;
				point.y = (int)eventY;
			}else{
				point = new Point();
				isFirstPoint = false;
			}
			
			
			//Toast.makeText(getContext(), "ACTION_DOWN", Toast.LENGTH_SHORT).show();
		}
		if(actionMasked == MotionEvent.ACTION_CANCEL)
		{
			Toast.makeText(getContext(), "ACTION_CANCEL", Toast.LENGTH_SHORT).show();
		}
		if(actionMasked == MotionEvent.ACTION_MOVE)
		{
			path.quadTo(point.x, point.y, (eventX + point.x) / 2,	(eventY + point.y) / 2);

			point.x = (int)eventX;
			point.y = (int)eventY;

			//path.lineTo(eventX, eventY);
			//Toast.makeText(getContext(), "ACTION_MOVE", Toast.LENGTH_SHORT).show();
		}
		if (actionMasked == MotionEvent.ACTION_UP )
		{
			canvas.drawPath(path, paint);
			//path.reset();
			
		}
		invalidate();
		return true;
		//return super.onTouchEvent(event);
	}

	
	



}
