package com.cariad.cea.aircondition.views;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.MotionEvent;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class SimulationWindBottomView extends BaseSimulationWindView {
    public SimulationWindBottomView(@NonNull Context context) {
        super(context);
    }

    public SimulationWindBottomView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public SimulationWindBottomView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        touchXPosition = (float) getWidth() /2;
        touchYPosition = getHeight();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                touchDownX = event.getX();
                touchDownY = event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                touchXDistance = event.getX() - touchDownX;
                touchYDistance = event.getY() - touchDownY;
                if (touchXPosition + touchXDistance >= getWidth()) {
                    touchXDistance = getWidth() - touchXPosition;
                } else if (touchXPosition + touchXDistance <= 0) {
                    touchXDistance = 0 - touchXPosition;
                }

                if (touchYPosition + touchYDistance <= 0) {
                    touchYDistance = 0 - touchYPosition;
                } else if (touchYPosition + touchYDistance >= getHeight()) {
                    touchYDistance = getHeight() - touchYPosition;
                }
                // 根据当前拖动的位置，重绘当前图片
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                touchXPosition = touchXPosition+event.getX() - touchDownX;
                touchYPosition = touchYPosition+event.getY() - touchDownY;
                touchXDistance = 0f;
                touchYDistance = 0f;
                break;
        }
        return super.onTouchEvent(event);
    }

    @Override
    public void draw(@NonNull Canvas canvas) {
        super.draw(canvas);

        if (bufferBitmap == null) {
            bufferBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
            bufferCanvas = new Canvas(bufferBitmap);
        }
        float currentTouchPositionX = touchXPosition+touchXDistance;
        float currentTouchPositionY = touchYPosition+touchYDistance;
        if (currentTouchPositionX<= 0) {
            currentTouchPositionX = 0;
        } else if (currentTouchPositionX>= getWidth()) {
            currentTouchPositionX = getWidth();
        }

        if (currentTouchPositionY<= 0) {
            currentTouchPositionY = 0;
        } else if (currentTouchPositionY>= getHeight()) {
            currentTouchPositionY = getHeight();
        }
//        // 【0,0】状态下，默认图片显示的是向下展开的图片，点位显示在图片底部正中间
//        bufferCanvas.drawColor(getResources().getColor(android.R.color.transparent), PorterDuff.Mode.CLEAR);
//        bufferCanvas.drawCircle(currentTouchPositionX, currentTouchPositionY, 15, paint);

        // 根据当前的虚拟的触摸位置，对当前View通过matrix实现缩放、翻转操作
        canvas.drawBitmap(bufferBitmap, 0, 0, paint);
        Matrix matrix = new Matrix();
        // 根据当前点位，计算View的旋转角度和缩放比例，旋转角度根据X轴的偏移量获取，缩放比例按照Y的偏移量获取
        float degree = (float) Math.atan2((currentTouchPositionX-(getWidth()/2)), currentTouchPositionY);
        matrix.setSkew(degree, 0f, getWidth()/2, 0);
        float scale = currentTouchPositionY / getHeight();
        matrix.preScale(1, scale, getWidth()/2, 0);
        // 最开始对图片进行缩放处理，适配当前ImageView控件
        matrix.preScale(srcScale[0], srcScale[1]);
        setImageMatrix(matrix);
    }

}
