package com.wasu.cs.widget;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wasu.authsdk.AuthSDK;
import com.wasu.authsdk.IAuthInterface;
import com.wasu.cs.model.SpecialColumnModel;
import com.wasu.util.ConfigUtils;

import cn.com.wasu.main.AppUtil;
import cn.com.wasu.main.IntentMap;
import cn.com.wasu.main.R;

public class BottomFunctionView extends FrameLayout {
	private ImageView iconView, userIconView;
	private ImageView selectedView;
	private TextView title;
	private RelativeLayout rlBottomView;
	private Context mContext;

	public BottomFunctionView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context, attrs);
	}

	public BottomFunctionView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context, attrs);
	}

	public BottomFunctionView(Context context) {
		super(context);
		init(context, null);
	}

	protected void init(Context context, AttributeSet attrs) {
		mContext = context;
		LayoutInflater.from(context).inflate(R.layout.bottom_function_view, this);
		rlBottomView = (RelativeLayout) findViewById(R.id.rlBottomView);
		title = (TextView) findViewById(R.id.title);
		iconView = (ImageView) findViewById(R.id.icon);
		userIconView = (ImageView) findViewById(R.id.icon_user);
		selectedView = (ImageView) findViewById(R.id.selected);

		setFocusable(true);
		setClickable(true);

//		setOnFocusChangeListener(new OnFocusChangeListener() {
//			@Override
//			public void onFocusChange(View v, boolean hasFocus) {
//				if (hasFocus) {
//					((BottomFunctionView) v).getSelectedView().setVisibility(View.VISIBLE);
//				} else {
//					((BottomFunctionView) v).getSelectedView().setVisibility(View.GONE);
//				}
//			}
//		});

	}

	public SpecialColumnModel userCenterModel;

	public void bindUserData(final SpecialColumnModel c) {
		if (c == null) {
			return;
		}

		// 判断用户是否登录
		if (AuthSDK.getInstance().getValue(IAuthInterface.KEY_USERKEY) != null && AuthSDK.getInstance().getValue(IAuthInterface.KEY_TOKEN) != null) {
			// 如果已经登陆
			String headUrl = ConfigUtils.getString(mContext, "usercenter", "headUrl");
			if (TextUtils.isEmpty(headUrl)) {
				if (!TextUtils.isEmpty(c.getContent())) {
					rlBottomView.setVisibility(View.VISIBLE);
					iconView.setVisibility(View.VISIBLE);
					userIconView.setVisibility(View.GONE);
//					ImageFetcherModule.setRouteDisplayImager(iconView, c.getContent(), true);

				}
			} else {
				rlBottomView.setVisibility(View.VISIBLE);
				iconView.setVisibility(View.GONE);
				userIconView.setVisibility(View.VISIBLE);
//				ImageFetcherModule.setRouteDisplayImager(userIconView, headUrl, true);
				// ImageLoader.getInstance().displayImage(headUrl,
				// getIconView());
			}
			title.setText(c.getTitle());
		} else {
			if (!TextUtils.isEmpty(c.getContent())) {
				rlBottomView.setVisibility(View.VISIBLE);
				iconView.setVisibility(View.VISIBLE);
				userIconView.setVisibility(View.GONE);
//				ImageFetcherModule.setRouteDisplayImager(iconView, c.getContent(), true);
			}
			title.setText("未登录");
		}

	}

	public boolean isUserCenterView;

	public void bindData(final SpecialColumnModel c) {
		if (c == null) {
			return;
		}
		final String jsonUrl = c.getJsonUrl();
		if (c.getLayout().equals("Business_Hall")) {
			// 是用户数据的view
			isUserCenterView = true;
			userCenterModel = c;
			bindUserData(c);
		} else {
			isUserCenterView = false;
			if (!TextUtils.isEmpty(c.getContent())) {
				rlBottomView.setVisibility(View.VISIBLE);
				iconView.setVisibility(View.VISIBLE);
				userIconView.setVisibility(View.GONE);

				if (c.getJsonUrl() != null) {
					if (c.getTitle().equals("看过")) {
						iconView.setImageResource(R.drawable.bottom_history);
					} else if (c.getTitle().equals("收藏")) {
						iconView.setImageResource(R.drawable.bottom_collection);
					} else if (c.getTitle().equals("搜索")) {
						iconView.setImageResource(R.drawable.bottom_search);
					}
				}

				if (!"".equals(c.getContent())) {
//					ImageFetcherModule.setRouteDisplayImager(iconView, c.getContent(), true);
				}
			}
			title.setText(c.getTitle());

		}
		setContentDescription(c.getName());
		if (isUserCenterView) {

		}

		setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (null != jsonUrl && !"".equals(jsonUrl)) {
					// 播放路径入口统计
					AppUtil.playEnter = c.getTitle();
					if (c.getLayout().equals("Business_Hall")) {
						IntentMap.startIntent(mContext, null, "Wasu_UserCenter", c.getJsonUrl(), null);
					}else{
						IntentMap.startIntent(mContext, null, c.getLayout(), c.getJsonUrl(), null);
					}
				}
			}
		});

	}

	public ImageView getIconView() {
		return iconView;
	}

	public ImageView getSelectedView() {
		return selectedView;
	}
}
