package cisc181.simpledraw;

/*

SimpleDraw app -- drawing and touch interactivity in a single thread

Christopher Rasmussen
copyright 2015, University of Delaware

*/

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.lang.Override;

public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback {

    Paint mPaint;
    Bitmap bmap;
    float bmapDegrees = 0;
    boolean touchDown = false;
    float touchX, touchY;

    public MySurfaceView(Context context, AttributeSet attrs) {

        super(context, attrs);

        SurfaceHolder holder = getHolder();
        holder.addCallback(this);

        mPaint = new Paint();
        mPaint.setStrokeWidth(3);

        // load simple icon to draw

        bmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);

    }


    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }

    // this is called at least once after surfaceCreated()

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

        Canvas canvas = holder.lockCanvas();
        myDraw(canvas);
        holder.unlockCanvasAndPost(canvas);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {


    }

    // respond to the SurfaceView being clicked/dragged

    public boolean onTouchEvent(MotionEvent e) {

        // determine whether touch is still active or has just ended

        if (e.getAction() == MotionEvent.ACTION_DOWN) {
            touchDown = true;
            touchX = e.getX();
            touchY = e.getY();
        }
        else if (e.getAction() == MotionEvent.ACTION_MOVE) {
            touchX = e.getX();
            touchY = e.getY();
        }
        else if (e.getAction() == MotionEvent.ACTION_UP) {
            touchDown = false;
        }
        else {
            return false;
        }

        // force a redraw

        SurfaceHolder holder = getHolder();
        Canvas canvas = holder.lockCanvas();
        myDraw(canvas);
        holder.unlockCanvasAndPost(canvas);

        return true;
    }

    // do drawing on current canvas

    public void myDraw(Canvas canvas) {

        // clear background

        canvas.drawColor(Color.BLACK);

        // "center" circle...change color depending on touch status

        if (touchDown) {
            mPaint.setColor(Color.GREEN);
        } else {
            mPaint.setColor(Color.RED);
        }
        canvas.drawCircle(canvas.getWidth() / 2, canvas.getHeight() / 2, 100, mPaint);

        // rectangle

        mPaint.setColor(Color.BLUE);
        canvas.drawRect(canvas.getWidth() / 4, canvas.getHeight() / 4, 3 * canvas.getWidth() / 4, canvas.getHeight() / 4 + 100, mPaint);

        // image

        float iconX = canvas.getWidth() / 2 - bmap.getWidth() / 2;
        float iconY = canvas.getHeight() / 2 - bmap.getHeight() / 2;

        // save() and restore() used here so that ONLY bitmap is rotated...otherwise text would be, too

        canvas.save();
        canvas.rotate(bmapDegrees, iconX + bmap.getWidth() / 2, iconY + bmap.getHeight() / 2);
        canvas.drawBitmap(bmap, iconX, iconY, mPaint);
        canvas.restore();

        // rotate icon a little each time it's drawn

        bmapDegrees += 10;

        // text

        mPaint.setColor(Color.MAGENTA);
        mPaint.setTextSize(64);
        Rect r = new Rect();
        String s = "Hello, canvas";

        // measure how big string is so that we can properly center it

        mPaint.getTextBounds(s, 0, s.length(), r);
        canvas.drawText("Hello, canvas", canvas.getWidth() / 2 - r.width() / 2, 3 * canvas.getHeight() / 4, mPaint);

        // "touch-following" shape...drawn last so that it is on top of everything

        if (touchDown) {
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setColor(Color.WHITE);
            canvas.drawRect(touchX - 100, touchY - 100, touchX + 100, touchY + 100, mPaint);
            mPaint.setStyle(Paint.Style.FILL);
        }
    }

}