package com.wasu.cs.widget;

import java.util.List;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;

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

import com.wasu.cs.model.RecommendModel.Recommend;
import com.wasu.module.log.WLog;
import com.wasu.widgets.focuswidget.CustomFocusGridView;

public class SearchNotResultRecommendLayout extends RelativeLayout {
	private final String TAG = "SearchNotResultRecommendLayout";

	public CustomFocusGridView getFgv() {
		return mFgv;
	}

	private CustomFocusGridView mFgv;

	public SearchNotResultRecommendLayout(Context context) {
		super(context);
		initLayout(context);
	}

	public SearchNotResultRecommendLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		initLayout(context);
	}

	public SearchNotResultRecommendLayout(Context context, AttributeSet attrs,
			int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		initLayout(context);
	}

	private void initLayout(Context context) {
		LayoutInflater.from(context).inflate(R.layout.layout_search_not_result_recommend, this, true);

		mFgv = (CustomFocusGridView) findViewById(R.id.search_not_result_recommend_fgv);
		mFgv.setFocusHightlightDrawable(R.drawable.tv_select_focus);
		mFgv.setFocusShadowDrawable(R.drawable.focus_shadow);
		mFgv.postAnimation(200, null);
		//mFgv.setOnScrollListener(new PauseOnScrollListener(ImageFetcherModule.getInstance().getImageLoader(), true, true));
		mFgv.setOnScrollListener(new com.wasu.frescoimagefetchermodule.PauseOnScrollListener(true,true));
		mFgv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Object tag = view.getTag();
				if (tag instanceof Recommend) {
					Recommend recommend = (Recommend) tag;

					IntentMap.startIntent(
							getContext(),
							null,
							recommend.getLayout(),
							recommend.getJsonUrl(),
							null);
				} else {
					WLog.w(TAG, "onItemClick() unknow tag: " + tag);
				}
			}
		});
		
		// ...
		mFgv.setNextFocusLeftId(R.id.search_input_gv);
	}
	
	/**
	 * show the recommend view
	 * @param recommends
	 */
	public void show(List<Recommend> recommends) {
		if (null != recommends && !recommends.isEmpty()) {
			NotResultAdapter adapter = new NotResultAdapter();
			adapter.setData(recommends);
			mFgv.setAdapter(adapter);
			adapter.notifyDataSetChanged();
		} else {
			WLog.w(TAG," show() no recommends found");
		}
	}

	class NotResultAdapter extends BaseAdapter {
		
		private List<Recommend> mRecommends;
		public void setData(List<Recommend> recommends) {
			mRecommends = recommends;
		}

		@Override
		public int getCount() {
			return null == mRecommends ? 0 : mRecommends.size();
		}

		@Override
		public Object getItem(int position) {
			return null == mRecommends ? null : mRecommends.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			PosterItemLayout layout = null;
			if (null == convertView) {
				layout = new PosterItemLayout(getContext());
				convertView = layout;
			}
			
			AbsListView.LayoutParams params = new AbsListView.LayoutParams(getResources().getDimensionPixelOffset(R.dimen.d_183dp), getResources().getDimensionPixelOffset(R.dimen.d_241dp));
			layout = (PosterItemLayout) convertView;
			layout.setLayoutParams(params);

			layout.setTag(getItem(position));
			layout.setImageUrl(mRecommends.get(position).getPicUrl());
			layout.setName(mRecommends.get(position).getTitle());
			
			return layout;
		}
	}

}
