package com.abbidot.tracker.widget;

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.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.appcompat.widget.AppCompatImageView;

import com.abbidot.baselibrary.util.LogUtil;
import com.abbidot.tracker.R;


/**
 * Created by lenovo on 2019/2/15.
 * desc: 选色器
 * author: lli
 */
public class ColorPickerRectangle extends AppCompatImageView implements View.OnTouchListener {

    private Bitmap bitmap;
    //当前显示选中小圆半径
    private final float mSmallRadius;
    private float mSelectCircleX = 0;
    private float mSelectCircleY = 0;

    private Bitmap bp;//色轮图片
    private int bw, bh;//色轮图片的尺寸
    private float x, y, radio;
    private OnColorSelectListener onColorSelectListener;
    private final Paint paint;

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

    public ColorPickerRectangle(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ColorPickerRectangle(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        //禁用硬件加速,防止返回到界面多次执行onDraw
        setLayerType(LAYER_TYPE_SOFTWARE, null);

        bp = BitmapFactory.decodeResource(context.getResources(), R.drawable.bg_color);

        bw = bp.getWidth();
        bh = bp.getHeight();

        x = bw / 2f;
        y = bh / 2f;
        radio = Math.min(x, y);

        setImageBitmap(bp);

        setOnTouchListener(this);

        paint = new Paint();
        paint.setStrokeWidth(10);
        paint.setColor(Color.RED);
        paint.setAntiAlias(true);

        setClickable(true);

        bitmap = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.ico_color_selector);
        mSmallRadius = Math.min(bitmap.getWidth() / 2f, bitmap.getHeight() / 2f);
    }

    //    float lastX = 0;
    //    float lastY = 0;
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        float xx = event.getX();
        float yy = event.getY();

        if (!inCircle(xx, yy)) {
            return false;
        }

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                //父节点不拦截子节点,解决父控件是滑动控件的冲突
                v.getParent().requestDisallowInterceptTouchEvent(true);
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
                //父节点拦截子节点
                v.getParent().requestDisallowInterceptTouchEvent(false);
                if (onColorSelectListener != null) {
                    onColorSelectListener.onColorSelect(getColor(xx, yy));
                }
                break;
        }
        x = xx;
        y = yy;
        invalidate();

        return true;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        // x = getMeasuredWidth() / 2f;
        // y = getMeasuredWidth() / 2f;
        // radio = Math.min(x, y);
        LogUtil.Companion.e("onMeasure");
    }


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

        //        canvas.drawPoint(x, y, paint);

        //第一次进来默认不显示选中的小圆圈
        if (mSelectCircleX == 0 && mSelectCircleY == 0) {
            mSelectCircleX = x - mSmallRadius;
            mSelectCircleY = y - mSmallRadius;
            canvas.drawBitmap(bitmap, 0, 0, paint);
        } else
            canvas.drawBitmap(bitmap, x - mSmallRadius, y - mSmallRadius, paint);
    }

    private int getColor(float x, float y) {

        this.x = x;
        this.y = y;

        int w = getWidth();
        int h = getHeight();

        int dx = (int) ((x / w) * bw);
        int dy = (int) ((y / h) * bh);

        int color = Color.BLACK;

        try {
            color = bp.getPixel(dx, dy);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return color;
    }

    private boolean inCircle(float x, float y) {
        float cx = getWidth() / 2f;
        float cy = getHeight() / 2f;
        float d =
                (float) Math.abs(Math.sqrt((x - cx) * (x - cx) + (y - cy) * (y - cy))) + mSmallRadius;
        return d <= radio;
    }

    public interface OnColorSelectListener {
        public void onColorSelect(int color);
    }

    public OnColorSelectListener getOnColorSelectListener() {
        return onColorSelectListener;
    }

    public void setOnColorSelectListener(OnColorSelectListener onColorSelectListener) {
        this.onColorSelectListener = onColorSelectListener;
    }

    /**
     * 回收Bitmap内存
     */
    public void recycle() {

        if (bitmap != null) {
            if (!bitmap.isRecycled()) {
                bitmap.recycle();
            }
            bitmap = null;
        }

        if (bp != null) {
            if (!bp.isRecycled()) {
                bp.recycle();
            }
            bp = null;
        }
    }

}
