package com.usai.redant.CommonEditor;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapRegionDecoder;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by macmini1 on 2017/8/31.
 */

public class BrushView extends View {


    private Paint brush = new Paint();
    private Path path = new Path();

    private float minX;
    private float minY;
    private float maxX;
    private float maxY;
    private float lineWidth;

    public BrushView(Context context) {
        this(context,null);
    }

    public BrushView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public BrushView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        lineWidth = 10.f;
        init();
    }


    private void init() {

        // fix saved jpg file seen black
        // 没有颜色的位置默认使用的是白色的替代的，但是保存成png后，没有颜色的位置使用的是黑色替代的，所以会看到生成的图片是全黑的。
        setBackgroundColor(Color.WHITE);

        // 抗锯齿
        brush.setAntiAlias(true);
        brush.setColor(Color.BLACK);
        brush.setStyle(Paint.Style.STROKE);
        brush.setStrokeJoin(Paint.Join.ROUND);
        brush.setStrokeWidth(lineWidth);
        brush.setStrokeCap(Paint.Cap.ROUND); // 线条两端圆角

        minX = Float.MAX_VALUE;
        minY = Float.MAX_VALUE;

        maxX = Float.MIN_VALUE;
        maxY = Float.MIN_VALUE;


    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        float x = event.getX();
        float y = event.getY();



        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN: {
                path.moveTo(x,y);
                checkPoin(x,y);
                return true;
            }

            case MotionEvent.ACTION_MOVE: {
                path.lineTo(x,y);
                checkPoin(x,y);
                ispainted = true;
            }
            break;
            case MotionEvent.ACTION_UP:
                break;
            default:
            return false;
        }

        postInvalidate();

        return false;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawPath(path,brush);

    }

    private void checkPoin(float x,float y) {

        if (x < minX) {
            minX = x;
        }

        if (y < minY) {
            minY = y;
        }

        if (x > maxX) {
            maxX = x;
        }

        if (y > maxY) {
            maxY = y;
        }


    }

    public void setBrushColor(int color) {
        brush.setColor(color);
    }

    public void setLineWidth(float width) {
        brush.setStrokeWidth(width);
    }

    public void clear() {

        ispainted = false;
        path.reset();
        postInvalidate();

    }

    private boolean ispainted = false;

    public boolean checkCanSave() {

        return ispainted;
    }

    public Bitmap getDrawBitmap() {

//        setDrawingCacheEnabled(true);
//        buildDrawingCache();
//        Bitmap b = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
//        Canvas c = new Canvas(b);
//        draw(c);
//        Bitmap bitmap = b;


        setDrawingCacheEnabled(true);
        buildDrawingCache();
        Bitmap bitmap = getDrawingCache();

        // 裁剪绘图区域
        String path = Utils.cacheTmpImage(bitmap);

        int width = getWidth();
        int height = getHeight();

        // 裁剪区域，默认原图
        Rect cropRect = new Rect(0, 0, width, height);

        try {
            final Matrix outputMatrix = new Matrix();//用于最图图片的精确缩放
            InputStream inputStream = new FileInputStream(path);
            final BitmapFactory.Options ops = new BitmapFactory.Options();
            BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(inputStream, false);

            cropRect.left = (int) (minX - lineWidth * 0.5);
            cropRect.top = (int) (minY - lineWidth * 0.5);
            cropRect.right = (int) (maxX + lineWidth * 0.5);
            cropRect.bottom = (int) (maxY + lineWidth * 0.5);

            final Bitmap source = decoder.decodeRegion(cropRect, ops);
            return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), outputMatrix, false);

        } catch (IOException e) {
            e.printStackTrace();
        }

        return bitmap;

    }

}
