package com.yw.fastviewlibrary.view;

/**
 * Created by heinigger on 2017/1/20.
 */

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import com.yw.fastviewlibrary.R;

public class CircleLayerView extends View {
    //外层的图片
    private Drawable moutDrawble;
    //内层的图片
    private Drawable minDrawable;
    //圆心的中心位置
    private int mCenterX;
    private int mCenterY;

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

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

    public CircleLayerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        intProperties(context, attrs);
    }

    private void intProperties(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CircleLayerView);

        int count = typedArray.getIndexCount();

        for (int requestId = 0; requestId < count; requestId++) {
            int attr = typedArray.getIndex(requestId);
            if (attr == R.styleable.CircleLayerView_circle_in_src) {
                minDrawable = typedArray.getDrawable(attr);
                minDrawable.setBounds(0, 0, minDrawable.getIntrinsicWidth(), minDrawable.getIntrinsicHeight());
            } else if (attr == R.styleable.CircleLayerView_circle_out_src) {
                moutDrawble = typedArray.getDrawable(attr);
                moutDrawble.setBounds(0, 0, moutDrawble.getIntrinsicWidth(), moutDrawble.getIntrinsicHeight());
            }
        }
        typedArray.recycle();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (MotionEvent.ACTION_DOWN == event.getAction()) {//事件的点击处理
            //相对于原点的点击位置
            int relativeX = mCenterX - (int) event.getX();
            int relativeY = mCenterY - (int) event.getY();

            int relativeRadius = (int) Math.sqrt(Math.pow(relativeX, 2) + Math.pow(relativeY, 2));
            Log.v("mine", event.getX() + ":" + event.getY() + ":" + relativeRadius + "半径呢:" + minDrawable.getIntrinsicHeight() + ":" + moutDrawble.getIntrinsicHeight());
            if (relativeRadius < minDrawable.getIntrinsicHeight()) {//内层的半径
                Toast.makeText(getContext(), "我点击了内层的图片", Toast.LENGTH_SHORT).show();
            } else if (relativeRadius < moutDrawble.getIntrinsicHeight()) {//外层的半径
                Toast.makeText(getContext(), "我点击了外层的图片", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(getContext(), "我点击了空白的地方", Toast.LENGTH_SHORT).show();
            }

        }
        return super.onTouchEvent(event);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = resolveSize(moutDrawble.getIntrinsicWidth() + getPaddingLeft() + getPaddingRight(), widthMeasureSpec);
        int height = resolveSize(moutDrawble.getIntrinsicHeight() + getPaddingTop() + getPaddingBottom(), heightMeasureSpec);

        mCenterX = width / 2;
        mCenterY = height;

        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        moutDrawble.draw(canvas);
        int radiusOffset = moutDrawble.getIntrinsicHeight() - minDrawable.getIntrinsicHeight();
        canvas.translate(radiusOffset, radiusOffset);
        minDrawable.draw(canvas);
    }
}
