package com.example.lab4;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import java.util.ArrayList;
import java.util.List;


public class DrawingView extends View {

    private Bitmap mBitmap;
    private Canvas mBitmapCanvas;
    private Paint mPaintLine;
    private Paint mPaintCircle;

    private Path mPath;
    private float mStartX, mStartY;

    private int currentColor = 0xFFFF0000;

    private final List<float[]> strokeEndpoints = new ArrayList<>();

    public DrawingView(Context context, AttributeSet attrs) {
        super(context, attrs);

        mPaintLine = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaintLine.setStyle(Paint.Style.STROKE);
        mPaintLine.setStrokeWidth(5);

        mPaintCircle = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaintCircle.setStyle(Paint.Style.FILL);

        mPath = new Path();
    }

    public void setCurrentColor(int color) {
        currentColor = color;
    }

    public void clearCanvas() {
        if (mBitmap != null) {
            strokeEndpoints.clear();
            mBitmap.eraseColor(0xFFFFFFFF);
            mPath.reset();
            invalidate();
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        if (mBitmap != null) mBitmap.recycle();
        mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        mBitmap.eraseColor(0xFFFFFFFF); // biało
        mBitmapCanvas = new Canvas(mBitmap);
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mPath = new Path();
                mPath.moveTo(x, y);
                mStartX = x;
                mStartY = y;
                invalidate();
                return true;

            case MotionEvent.ACTION_MOVE:
                mPath.lineTo(x, y);
                invalidate();
                return true;

            case MotionEvent.ACTION_UP:
                mPaintLine.setColor(currentColor);
                mBitmapCanvas.drawPath(mPath, mPaintLine);

                strokeEndpoints.add(new float[]{mStartX, mStartY, x, y, currentColor});
                mPath.reset();
                invalidate();
                return true;
        }
        return super.onTouchEvent(event);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (mBitmap != null) {
            canvas.drawBitmap(mBitmap, 0, 0, null);
        }

        if (!mPath.isEmpty()) {
            mPaintLine.setColor(currentColor);
            canvas.drawPath(mPath, mPaintLine);
        }

        for (float[] data : strokeEndpoints) {
            mPaintCircle.setColor((int) data[4]);
            canvas.drawCircle(data[0], data[1], 15, mPaintCircle);
            canvas.drawCircle(data[2], data[3], 15, mPaintCircle);
        }
    }
}
