package com.gh.common.view;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.gh.common.util.DisplayUtils;
import com.gh.gamecenter.R;

public class Concern_LinearLayout extends LinearLayout {

	private RecyclerView concern_rv_recommend;
	private ImageView concern_iv_delete;

	private int height;
	private int distance;

	Runnable show = new Runnable() {
		@Override
		public void run() {
			LayoutParams lparams = new LayoutParams(
					LayoutParams.MATCH_PARENT, height);
			lparams.height = concern_rv_recommend.getHeight() + distance;
			if (lparams.height > height) {
				lparams.height = height;
			}
			concern_rv_recommend.setLayoutParams(lparams);
			invalidate();
			if (concern_rv_recommend.getHeight() < height) {
				postDelayed(show, 10);
			} else {
				concern_iv_delete.setImageResource(R.drawable.concern_down);
			}
		}
	};

	Runnable hide = new Runnable() {
		@Override
		public void run() {
			LayoutParams lparams = new LayoutParams(
					LayoutParams.MATCH_PARENT, height);
			lparams.height = concern_rv_recommend.getHeight() - distance;
			if (lparams.height < 0) {
				lparams.height = 0;
			}
			concern_rv_recommend.setLayoutParams(lparams);
			invalidate();
			if (concern_rv_recommend.getHeight() > 0) {
				postDelayed(hide, 10);
			} else {
				concern_rv_recommend.setVisibility(View.GONE);
				concern_iv_delete.setImageResource(R.drawable.concern_up);
			}
		}
	};

	public Concern_LinearLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		height = DisplayUtils.dip2px(context, 121);
		distance = DisplayUtils.dip2px(context, 6);
	}

	@Override
	protected void onAttachedToWindow() {
		super.onAttachedToWindow();
		concern_rv_recommend = (RecyclerView) findViewById(R.id.concern_rv_recommend);
		concern_iv_delete = (ImageView) findViewById(R.id.concern_iv_delete);
	}

	public void showRecyclerView() {
		concern_rv_recommend.setVisibility(View.VISIBLE);
		removeCallbacks(hide);
		post(show);
	}

	public void hideRecyclerView() {
		removeCallbacks(show);
		post(hide);
	}
}
