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

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

import de.greenrobot.event.EventBus;

public class MyGame_LinearLayout extends LinearLayout {

	private LinearLayout mygame_ll;
	private RecyclerView mygame_rv_show;

	private int height;
	private int distance;
	private int total;

	private int position;

	Runnable show = new Runnable() {
		@Override
		public void run() {
			LayoutParams lparams = new LayoutParams(
					LayoutParams.MATCH_PARENT, height);
			lparams.height = mygame_ll.getHeight() + distance;
			if (lparams.height > total) {
				lparams.height = total;
			}
			mygame_ll.setLayoutParams(lparams);
			invalidate();
			if (lparams.height < total) {
				postDelayed(show, 10);
			} else {
				EventBus.getDefault().post(new EBShowDone("我的游戏", position));
			}
		}
	};

	Runnable hide = new Runnable() {
		@Override
		public void run() {
			LayoutParams lparams = new LayoutParams(
					LayoutParams.MATCH_PARENT, height);
			lparams.height = mygame_ll.getHeight() - distance;
			if (lparams.height < height) {
				lparams.height = height;
			}
			mygame_ll.setLayoutParams(lparams);
			invalidate();
			if (lparams.height > height) {
				postDelayed(hide, 10);
			} else {
				mygame_rv_show.setVisibility(View.GONE);
			}
		}
	};

	public MyGame_LinearLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		height = DisplayUtils.dip2px(getContext(), 66);
		distance = DisplayUtils.dip2px(getContext(), 5);
		total = DisplayUtils.dip2px(getContext(), 136);
	}

	@Override
	protected void onAttachedToWindow() {
		
		super.onAttachedToWindow();
		mygame_rv_show = (RecyclerView) findViewById(R.id.mygame_rv_show);
		mygame_ll = (LinearLayout) findViewById(R.id.mygame_ll);
	}

	public void showRecyclerView(int position) {
		this.position = position;
		mygame_rv_show.setVisibility(View.VISIBLE);
		removeCallbacks(hide);
		post(show);
	}

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