package com.wasu.cs.ui;

import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.wasu.cs.adapter.ContAdapter;
import com.wasu.cs.adapter.ContAdapter.ViewInterface;
import com.wasu.cs.model.FilmSpecialModel;
import com.wasu.cs.protocol.FilmSpecialProtocol;
import com.wasu.cs.protocol.FilmSpecialProtocol.FilmSpecialFetchCallback;
import com.wasu.cs.widget.FilmSpecialItem;
import com.wasu.cs.widget.HorizontalListView;
import com.wasu.frescoimagefetchermodule.FrescoImageFetcherModule;
import com.wasu.module.log.WLog;
import com.wasu.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

import cn.com.wasu.main.ErrorMap;
import cn.com.wasu.main.IntentConstant;
import cn.com.wasu.main.R;

/**
 * 
 * @ClassName: ActivityFilmSpecial 
 * @Description: 影视剧专题
 * @author 海迪
 * @date 2015年7月22日 下午3:34:53 
 *
 */
public class ActivityFilmSpecial extends ActivityBase {
	private String TAG = "ActivityFilmSpecial";
	private Context context;

	private ImageView fsBg;
	private HorizontalListView hListView;
	private List<FilmSpecialModel> filmList = new ArrayList<FilmSpecialModel>();
	private String bgImg;
	private String jsonUrl = null;


	@Override
	protected void doCreate(Bundle savedInstanceState) {
		WLog.i(TAG,"doCreate()");

		setContentView(R.layout.activity_film_special);
		context = this;
		jsonUrl = getIntent().getStringExtra(IntentConstant.DATAURI.value());
		reqLabelAreaData();
	}

	/**
	 * 
	 * @author 海迪
	 * @Title: reqLabelAreaData 
	 * @Description: 向后台请求数据， 成功后，初始化Activity
	 * @param 
	 * @return void
	 * @throws 
	 *
	 */
	private void reqLabelAreaData() {
		
		if(jsonUrl==null || StringUtils.isBlank(jsonUrl)) {
			finish();
		} else {
			jsonUrl = jsonUrl.trim() + "&page=1&psize=200";
		}
		
		showLoading();
//		String url = "http://121.40.195.74/?s=2005&p=sntAssetList&k=1&v=1&catId=280571&eid=149&ecatId=276043&page=1&psize=100";
		
		FilmSpecialProtocol.fetch(jsonUrl, new FilmSpecialFetchCallback() {

			@Override
			public void onResult(boolean successed, FilmSpecialProtocol filmSpecialProtocol) {
				hideLoading();
				if(successed) {
					List<FilmSpecialModel> fsList = filmSpecialProtocol.getFilmSpecialList();
					
					if(fsList != null) {
						filmList.addAll(fsList);
					}
					
					bgImg = filmSpecialProtocol.getBgImage();

					init();
				} else {
					
					if(filmSpecialProtocol != null) {
						showErrorExitDlg(ErrorMap.mapError(context, filmSpecialProtocol.getCode(), filmSpecialProtocol.getMessage()));
					} else {
						showErrorExitDlg(getResources().getString(R.string.error_unknown));
					}
					
				}
			}
		});
	}

	/**
	 * 
	 * @author 海迪
	 * @Title: init 
	 * @Description: 初始化Activity
	 * @param 
	 * @return void
	 * @throws 
	 *
	 */
	private void init() {
		
		fsBg = (ImageView) findViewById(R.id.fs_bg);
		
		if(!StringUtils.isBlank(bgImg)) {
			FrescoImageFetcherModule.getInstance().attachImage(bgImg, fsBg);
		}
		hListView = (HorizontalListView) findViewById(R.id.h_list_view);
		ContAdapter contAdapter = new ContAdapter(filmList);
		contAdapter.setViewInterface(new ViewInterface() {

			@Override
			public View getView(int position, Object dataObject,
					View convertView, ViewGroup parent) {
				
				FilmSpecialModel model = (FilmSpecialModel) dataObject;
				
				FilmSpecialItem item = null;
				if (convertView == null) {
					item = new FilmSpecialItem(context);
				} else {
					item = (FilmSpecialItem) convertView;
				}
				
				item.setHorizontalListView(hListView);
				item.initData(model);

				return item;
			}
		});
		
		hListView.setAdapter(contAdapter);

		if (filmList.size()<=5) {
			hListView.getChildAt(0).requestFocus();
		}else{
		hListView.getChildAt(1).requestFocus();
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (hListView!=null) {
			hListView.onKeyDown(keyCode, event);	
		}
		return super.onKeyDown(keyCode, event);
	}


	@Override
	protected void onDestroy() {
		super.onDestroy();
        if (hListView != null) {
            hListView.removeAllViews();
            hListView = null;
        }
        filmList = null;
	}
}
