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.LinearLayout;

import com.gh.gamecenter.R;

/**
 * Created by LGT on 2016/8/31.
 */
public class CardLinearLayout extends LinearLayout {

	private Drawable mLeftDrawable, mTopDrawable, mRightDrawable, mBottomDrawable, mCenterDrawable;
	private Drawable mLeftTopDrawable, mRightTopDrawable, mLeftBottomDrawable, mRightBottomDrawable;

	private int mLeft, mTop, mRight, mBottom;

	private boolean isBottom;

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

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

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

	private void init(Context context, AttributeSet attrs) {
		setWillNotDraw(false);
		isBottom = true;

		mLeftDrawable = getContext().getResources().getDrawable(R.drawable.frame_left);
		mTopDrawable = getContext().getResources().getDrawable(R.drawable.frame_top);
		mRightDrawable = getContext().getResources().getDrawable(R.drawable.frame_right);
		mBottomDrawable = getContext().getResources().getDrawable(R.drawable.frame_bottom);
		mCenterDrawable = getContext().getResources().getDrawable(R.drawable.frame_center);

		mLeftTopDrawable = getContext().getResources().getDrawable(R.drawable.frame_left_top_square);
		mRightTopDrawable = getContext().getResources().getDrawable(R.drawable.frame_right_top_square);
		mLeftBottomDrawable = getContext().getResources().getDrawable(R.drawable.frame_left_bottom_square);
		mRightBottomDrawable = 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();
		}
		setPadding(getPaddingLeft() + mLeft, getPaddingTop() + mTop, getPaddingRight() + mRight, getPaddingBottom() + mBottom);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		if (mLeft != 0 && mTop != 0 && mRight != 0 && mBottom != 0) {
			// 左、上、右、下
			if (mLeftTopDrawable != null) {
				mLeftTopDrawable.setBounds(0, 0, mLeft, mTop);
				mLeftTopDrawable.draw(canvas);
			}

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

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

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

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

			if (mLeftBottomDrawable != null) {
				mLeftBottomDrawable.setBounds(0, getHeight() - mBottom, mLeft, getHeight());
				mLeftBottomDrawable.draw(canvas);
			}

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

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

		} else if (mLeft != 0 && mTop != 0 && mRight != 0) {
			// 左、上、右
			if (mLeftTopDrawable != null) {
				mLeftTopDrawable.setBounds(0, 0, mLeft, mTop);
				mLeftTopDrawable.draw(canvas);
			}

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

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

			if (mLeftDrawable != null) {
				mLeftDrawable.setBounds(0, mTop, mLeft, getHeight());
				mLeftDrawable.draw(canvas);
			}

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

		} else if (mLeft != 0 && mBottom != 0 && mRight != 0) {
			// 左、下、右
			if (mLeftDrawable != null) {
				mLeftDrawable.setBounds(0, 0, mLeft, getHeight() - mBottom);
				mLeftDrawable.draw(canvas);
			}

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

			if (mLeftBottomDrawable != null) {
				mLeftBottomDrawable.setBounds(0, getHeight() - mBottom, mLeft, getHeight());
				mLeftBottomDrawable.draw(canvas);
			}

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

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

		} else if (mLeft != 0 && mRight != 0) {
			// 左、右
			if (mLeftDrawable != null) {
				mLeftDrawable.setBounds(0, 0, mLeft, getHeight());
				mLeftDrawable.draw(canvas);
			}

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

	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int newHeight = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(heightMeasureSpec) + mTop + mBottom,
				MeasureSpec.getMode(heightMeasureSpec));
		super.onMeasure(widthMeasureSpec, newHeight);
	}

	public void setBottom(boolean bottom) {
		isBottom = bottom;
	}

	public void setShadow(int left, int top, int right, int bottom) {
		setPadding(getPaddingLeft() - mLeft + left,
				getPaddingTop() - mTop + top,
				getPaddingRight() - mRight + right,
				getPaddingBottom() - mBottom + bottom);
		this.mLeft = left;
		this.mTop = top;
		this.mRight = right;
		this.mBottom = bottom;
	}

	public void setmLeft(int left) {
		setPadding(getPaddingLeft() - mLeft + left, getPaddingTop(), getPaddingRight(), getPaddingBottom());
		this.mLeft = left;
	}

	public void setmTop(int top) {
		setPadding(getPaddingLeft(), getPaddingTop() - mTop + top, getPaddingRight(), getPaddingBottom());
		this.mTop = top;
	}

	public void setmRight(int right) {
		setPadding(getPaddingLeft(), getPaddingTop(), getPaddingRight() - mRight + right, getPaddingBottom());
		this.mRight = right;
	}

	public void setmBottom(int bottom) {
		setPadding(getPaddingLeft(), getPaddingTop(), getPaddingRight(), getPaddingBottom() - mBottom + bottom);
		this.mBottom = bottom;
	}

	public int getmLeft() {
		return mLeft;
	}

	public int getmTop() {
		return mTop;
	}

	public int getmRight() {
		return mRight;
	}

	public int getmBottom() {
		return mBottom;
	}
}