package com.mobile.constellations.ui;

import java.util.ArrayList;


import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.haarman.listviewanimations.swinginadapters.prepared.SwingRightInAnimationAdapter;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.mobile.constellations.R;
import com.mobile.constellations.data.Contans;
import com.mobile.constellations.data.Soccer;
import com.mobile.constellations.util.ViewHolder;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.LoadedFrom;
import com.nostra13.universalimageloader.core.display.BitmapDisplayer;
import com.nostra13.universalimageloader.core.imageaware.ImageAware;

/**
 * 12星座漫画
 * @author Linda
 *
 */
public class SoccerImageActivity extends Activity {

	private ImageLoader imageLoader = ImageLoader.getInstance();
	private DisplayImageOptions options;
	private ArrayList<Soccer> list = new ArrayList<Soccer>();
	private MyAdapter myAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		super.onCreate(savedInstanceState);
		initOptions();
		//一步加载
		asynctaskInstance();
		setContentView(R.layout.activity_soccer_image);
		//重置标题
		resetTitlebar(); 
		//初始化UI
		initUI();
		showAd();
	}
	
	private void showAd(){

	}

	private void initUI() {
		ListView listView = (ListView) findViewById(R.id.listView1);
		myAdapter = new MyAdapter();
		SwingRightInAnimationAdapter swingRightInAnimationAdapter = new SwingRightInAnimationAdapter(myAdapter);
		swingRightInAnimationAdapter.setListView(listView);
		//listview设置适配器
		listView.setAdapter(swingRightInAnimationAdapter);
	}

	private void resetTitlebar() {
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.view_comm_titlebar);
		TextView title = (TextView) findViewById(R.id.titlebar_title);
		LinearLayout back = (LinearLayout) findViewById(R.id.titlebar_left_layout);
		title.setText("12星座之世界杯");
		back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	//异步加载
	private void asynctaskInstance() {
		AsyncHttpClient client = new AsyncHttpClient();
		client.post(Contans.PATH_SOCCER, new AsyncHttpResponseHandler() {

			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				//解析数据
				String result = new String(arg2);
				try {
					JSONObject jsonObject = new JSONObject(result);
					String soccer = jsonObject.getString("soccer");
					JSONArray jsonArray = new JSONArray(soccer);
					for (int i = 0; i < jsonArray.length(); i++) {
						//解析对应字段，并将值传入容器中
						JSONObject object = jsonArray.getJSONObject(i);
						String image = object.getString("image");
						Soccer myData = new Soccer(image);
						list.add(myData);
					}
					//通知适配器数据已经发生改变
					myAdapter.notifyDataSetChanged();
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2,
					Throwable arg3) {
			}
		});
	}

	private class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

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

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			//适配器发生改变
			if (convertView == null) {
				convertView = getLayoutInflater().inflate(
						R.layout.item_listview_soccer, null);
			}
			//找到对应行布局的控件
			ImageView image = ViewHolder.get(convertView, R.id.imageView1);
			//将容器的值显示到对应的控件上
			Soccer myData = list.get(position);
			imageLoader.displayImage(myData.image, image, options, null);
			return convertView;
		}
	}
	
	//图片url处理（第三方）
	private void initOptions() {
		options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.ic_stub)
				.showImageForEmptyUri(R.drawable.ic_empty)
				.showImageOnFail(R.drawable.ic_error).cacheInMemory(true)
				.cacheOnDisc(true).considerExifParams(true)
				.displayer(new SimpleBitmapDisplayer()).build();
	}

	//图片url处理（第三方）
	private final class SimpleBitmapDisplayer implements BitmapDisplayer {
		@Override
		public void display(Bitmap bitmap, ImageAware imageAware,
				LoadedFrom loadedFrom) {
			imageAware.setImageBitmap(bitmap);
		}
	}

}
