package com.gh.gamecenter.common.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import com.gh.gamecenter.core.utils.DisplayUtils;

public class CropImageBorderView extends View {

    /**
     * 水平方向与View的边距
     */
    private int mHorizontalPadding;
    /**
     * 垂直方向与View的边距
     */
    private int mVerticalPadding;
    /**
     * 绘制的矩形的宽度
     */
    private int mWidth;
    /**
     * 边框的宽度 单位dp
     */
    private int mBorderWidth = 1;

    private Paint mPaint, mPaintRect, mAnglePaint;

    private float mRatio = 1; // 宽高比

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

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

    public CropImageBorderView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mBorderWidth = (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, mBorderWidth, getResources()
                        .getDisplayMetrics());

        // 绘制阴影
        mPaintRect = new Paint();
        mPaintRect.setARGB(80, 0, 0, 0);
        mPaintRect.setStyle(Style.FILL);

        // 绘制边框
        mPaint = new Paint();
        mPaint.setStyle(Style.STROKE);
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.WHITE);
        mPaint.setStrokeWidth(DisplayUtils.dip2px(0.5f));

        // 绘制边框
        mAnglePaint = new Paint();
        mAnglePaint.setStyle(Style.FILL);
        mAnglePaint.setColor(Color.WHITE);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 计算矩形区域的宽度
        mWidth = getWidth() - 2 * mHorizontalPadding;
        // 计算距离屏幕垂直边界 的边距

        mVerticalPadding = (getHeight() - (int) (mWidth * mRatio)) / 2;

        // 绘制左边
        canvas.drawRect(0, 0, mHorizontalPadding, getHeight()
                - mVerticalPadding, mPaintRect);
        // 绘制右边
        canvas.drawRect(getWidth() - mHorizontalPadding, mVerticalPadding,
                getWidth(), getHeight(), mPaintRect);
        // 绘制上边
        canvas.drawRect(mHorizontalPadding, 0, getWidth(), mVerticalPadding,
                mPaintRect);
        // 绘制下边
        canvas.drawRect(0, getHeight() - mVerticalPadding, getWidth()
                - mHorizontalPadding, getHeight(), mPaintRect);

        // 绘制边框
        canvas.drawRect(mHorizontalPadding, mVerticalPadding,
                mHorizontalPadding + mWidth, getHeight() - mVerticalPadding, mPaint);

        // 左上
        canvas.drawRect(mHorizontalPadding - DisplayUtils.dip2px(1F)
                , mVerticalPadding - DisplayUtils.dip2px(1F)
                , mHorizontalPadding + DisplayUtils.dip2px(16F)
                , mVerticalPadding + DisplayUtils.dip2px(1F), mAnglePaint);
        canvas.drawRect(mHorizontalPadding - DisplayUtils.dip2px(1F)
                , mVerticalPadding - DisplayUtils.dip2px(1F)
                , mHorizontalPadding + DisplayUtils.dip2px(1F)
                , mVerticalPadding + DisplayUtils.dip2px(16F), mAnglePaint);

        // 右上
        canvas.drawRect(mHorizontalPadding + mWidth - DisplayUtils.dip2px(16F)
                , mVerticalPadding - DisplayUtils.dip2px(1F)
                , mHorizontalPadding + mWidth
                , mVerticalPadding + DisplayUtils.dip2px(1F), mAnglePaint);
        canvas.drawRect(mHorizontalPadding + mWidth - DisplayUtils.dip2px(1F)
                , mVerticalPadding - DisplayUtils.dip2px(1F)
                , mHorizontalPadding + mWidth + DisplayUtils.dip2px(1F)
                , mVerticalPadding + DisplayUtils.dip2px(16F), mAnglePaint);

        // 左下
        canvas.drawRect(mHorizontalPadding - DisplayUtils.dip2px(1F)
                , getHeight() - mVerticalPadding - DisplayUtils.dip2px(16F)
                , mHorizontalPadding + DisplayUtils.dip2px(1F)
                , getHeight() - mVerticalPadding - DisplayUtils.dip2px(1F), mAnglePaint);
        canvas.drawRect(mHorizontalPadding - DisplayUtils.dip2px(1F)
                , getHeight() - mVerticalPadding - DisplayUtils.dip2px(1F)
                , mHorizontalPadding + DisplayUtils.dip2px(16F)
                , getHeight() - mVerticalPadding + DisplayUtils.dip2px(1F), mAnglePaint);

        // 右下
        canvas.drawRect(mHorizontalPadding + mWidth - DisplayUtils.dip2px(16F)
                , getHeight() - mVerticalPadding - DisplayUtils.dip2px(1F)
                , mHorizontalPadding + mWidth
                , getHeight() - mVerticalPadding + DisplayUtils.dip2px(1F), mAnglePaint);
        canvas.drawRect(mHorizontalPadding + mWidth - DisplayUtils.dip2px(1F)
                , getHeight() - mVerticalPadding - DisplayUtils.dip2px(16F)
                , mHorizontalPadding + mWidth + DisplayUtils.dip2px(1F)
                , getHeight() - mVerticalPadding + DisplayUtils.dip2px(1F), mAnglePaint);
    }

    public void setHorizontalPadding(int mHorizontalPadding, float ratio) {
        this.mHorizontalPadding = mHorizontalPadding;
        this.mRatio = ratio;
    }
}
