package com.cariad.cea.aircondition.views;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Camera;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.util.Log;

import pl.droidsonroids.gif.GifImageView;

public class BaseSimulationWindView extends GifImageView {
    protected float touchXPosition/*取值范围是-1/2到1/2的宽度*/, touchYPosition/*取值范围是0到-2倍的高度*/; // 当前图片沿X轴翻转和沿Y轴拉伸的初始值，默认都是0
    protected Bitmap bufferBitmap;
    protected Canvas bufferCanvas; // 双缓冲方式绘制
    protected Camera mCamera;
    protected Paint paint; // 绘制标准线的画笔
    protected float touchDownX, touchDownY, touchXDistance, touchYDistance;
    protected float[] srcScale = new float[2];
    public BaseSimulationWindView(Context context) {
        super(context);
        init(context);
    }

    public BaseSimulationWindView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public BaseSimulationWindView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    public BaseSimulationWindView(Context context, AttributeSet attrs, int defStyle, int defStyleRes) {
        super(context, attrs, defStyle, defStyleRes);
        init(context);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        srcScale = preSrcScale();
    }

    protected void init(Context context) {
        if (paint == null) {
            paint = new Paint();
            paint.setColor(getResources().getColor(android.R.color.holo_green_dark));
            // 设置抗锯齿
            paint.setAntiAlias(true);
        }
//        BitmapFactory.decodeResource()
    }

    /**
     * 因为图片资源可能和ImageView控件不匹配,需要记录初始状态下图片资源在X轴和Y轴被缩放的比例
     * */
    protected float[] preSrcScale() {
        // 首先计算ImageView中图片的大小和ImageView控件的大小比例，缩放该图片
        AnimationDrawable bitmapDrawable = (AnimationDrawable) getDrawable();
        Log.d("WindowBottomView", "bitmap-width:"+(bitmapDrawable.getBounds().right-bitmapDrawable.getBounds().left)+" bitmap-height:"+(bitmapDrawable.getBounds().bottom-bitmapDrawable.getBounds().top)+"view-width:"+getWidth()+" view-height:"+getHeight());
        // 宽度和高度的缩放比例
        float scaleWidth = (float) getWidth()/(bitmapDrawable.getBounds().right-bitmapDrawable.getBounds().left) ;
        float scaleHeight = (float) getHeight()/(bitmapDrawable.getBounds().bottom-bitmapDrawable.getBounds().top) ;
        return new float[]{scaleWidth, scaleHeight};
    }
}
