package com.gh.common.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

import com.gh.gamecenter.R;

/**
 * Created by LGT on 2016/9/9.
 */
public class CardRelativeLayout extends RelativeLayout {

	private Drawable topDrawable, bottomDrawable;
	private Drawable leftTopDrawable, rightTopDrawable, leftBottomDrawable, rightBottomDrawable;

	private int mLeft, mTop, mRight, mBottom;

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

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

	public CardRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init(context, attrs);
	}

	private void init(Context context, AttributeSet attrs) {
		setWillNotDraw(false);
		topDrawable = getContext().getResources().getDrawable(R.drawable.frame_top);
		bottomDrawable = getContext().getResources().getDrawable(R.drawable.frame_bottom);

		leftTopDrawable = getContext().getResources().getDrawable(R.drawable.frame_left_top_square);
		rightTopDrawable = getContext().getResources().getDrawable(R.drawable.frame_right_top_square);
		leftBottomDrawable = getContext().getResources().getDrawable(R.drawable.frame_left_bottom_square);
		rightBottomDrawable = getContext().getResources().getDrawable(R.drawable.frame_right_bottom_square);

		if (attrs != null) {
			TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CardLayout);
			mLeft = ta.getDimensionPixelSize(R.styleable.CardLayout_left, 0);
			mTop = ta.getDimensionPixelSize(R.styleable.CardLayout_top, 0);
			mRight = ta.getDimensionPixelSize(R.styleable.CardLayout_right, 0);
			mBottom = ta.getDimensionPixelSize(R.styleable.CardLayout_bottom, 0);
			ta.recycle();
		}
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		if (mTop != 0) {
			if (leftBottomDrawable != null) {
				leftBottomDrawable.setBounds(0, 0, mLeft, mTop);
				leftBottomDrawable.draw(canvas);
			}

			if (bottomDrawable != null) {
				bottomDrawable.setBounds(mLeft, 0, getWidth() - mRight, mTop);
				bottomDrawable.draw(canvas);
			}

			if (rightBottomDrawable != null) {
				rightBottomDrawable.setBounds(getWidth() - mRight, 0, getWidth(), mTop);
				rightBottomDrawable.draw(canvas);
			}
		}
		if (mBottom != 0) {
			if (leftTopDrawable != null) {
				leftTopDrawable.setBounds(0, getHeight() - mBottom, mLeft, getHeight());
				leftTopDrawable.draw(canvas);
			}

			if (topDrawable != null) {
				topDrawable.setBounds(mLeft, getHeight() - mBottom, getWidth() - mRight, getHeight());
				topDrawable.draw(canvas);
			}

			if (rightTopDrawable != null) {
				rightTopDrawable.setBounds(getWidth() - mRight, getHeight() - mBottom, getWidth(), getHeight());
				rightTopDrawable.draw(canvas);
			}
		}
	}

	public void setmBottom(int mBottom) {
		this.mBottom = mBottom;
	}

	public void setmTop(int mTop) {
		this.mTop = mTop;
	}
}
