package com.wasu.cs.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.com.wasu.main.R;

/**
 * 包含tab
 */
public class TabFrame extends LinearLayout {

	private boolean mConfigured;
	private boolean mIsCustom;

	public TabFrame(Context context, AttributeSet attributes) {
		super(context, attributes);
	}

	@Override
	public void setSelected(boolean selected) {
		// No-op: the selected state is used to show that the tab is selected in
		// the logical sense,
		// whereas ListView would attempt to use that state to show the tab as
		// "highlighted" - we
		// use focus for that.
	}

	/**
	 * Sets the selected state of this view.
	 */
	public void select(boolean selected) {
		super.setSelected(selected);
	}

	/**
	 * Expands the content of this tab.
	 * <p>
	 * For regular tabs, this will simply reveal the tab's title and mark the
	 * icon as activated. For custom tabs, this will set their "activated"
	 * state.
	 *
	 * @see View#setActivated(boolean)
	 */
	public void expand(boolean expanded) {
		if (!mIsCustom) {
			getTitle().setVisibility(expanded ? View.VISIBLE : View.GONE);
		}
		setActivated(expanded);
	}

	private ImageView getIcon() {
		return (ImageView) findViewById(R.id.icon);
	}

	private TextView getTitle() {
		return (TextView) findViewById(R.id.title);
	}

	public void configureNormal(Drawable icon, CharSequence text) {
		markConfigured(false);
		getIcon().setImageDrawable(icon);
		getTitle().setText(text);
	}

	public void configureCustom(View content) {
		markConfigured(true);

		// The focused state should be rendered by the content.
		setBackgroundDrawable(null);

		// Prevent the content from receiving events, but let it reflect the
		// correct state.
		content.setFocusable(false);
		content.setFocusableInTouchMode(false);
		content.setClickable(false);
		content.setDuplicateParentStateEnabled(true);

		removeAllViews();
		addView(content);
	}

	private void markConfigured(boolean isCustom) {
		if (mConfigured) {
			throw new IllegalStateException("Frame already configured.");
		}
		mConfigured = true;
		mIsCustom = isCustom;
	}
}
