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.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
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.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.haarman.listviewanimations.swinginadapters.prepared.ScaleInAnimationAdapter;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.mobile.constellations.R;
import com.mobile.constellations.data.Allconstellation;
import com.mobile.constellations.util.ViewHolder;

/**
 * 12星座新闻
 * @author Linda
 *
 */
public class AllConstellationActivity extends Activity {

	public static String PATH_URL = "allconstellationurl";
	private ArrayList<Allconstellation> list = new ArrayList<Allconstellation>();
	private MyAdapter myAdapter;
	private TextView tv_head;

	//// 静态，全局调用，直接跳转到AllConstellationActivity，并传入url参数
	public static void intentAllConActivity(Context context, String url) {
		Intent intent = new Intent(context, AllConstellationActivity.class);
		intent.putExtra(PATH_URL, url);
		context.startActivity(intent);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		super.onCreate(savedInstanceState);
		// 得到其他界面传来的url参数
		String url = getIntent().getStringExtra(PATH_URL);
		//开启异步加载
		asynctaskInstance(url);
		setContentView(R.layout.activity_all_constellation);
		//重置标题
		resetTitlebar(); 
		showAd();
		ListView listView = (ListView) findViewById(R.id.listView1);
		View header = getLayoutInflater().inflate(R.layout.header_listview_allconstellation, null);
		tv_head = (TextView) header.findViewById(R.id.textView1);
		//listview添加header布局
		listView.addHeaderView(header);
		myAdapter = new MyAdapter();
		ScaleInAnimationAdapter scaleInAnimationAdapter = new ScaleInAnimationAdapter(myAdapter, 0f);
		scaleInAnimationAdapter.setListView(listView);
		//listview设置适配器
		listView.setAdapter(scaleInAnimationAdapter);
	}

	private void showAd(){

	}
	
	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 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_allconstellation, null);
			}
			//查找行布局对应控件
			TextView title = ViewHolder.get(convertView, R.id.title);
			TextView constellation = ViewHolder.get(convertView,
					R.id.constellation);
			TextView desc = ViewHolder.get(convertView, R.id.desc);
			//将容器的值显示到对应控件上
			Allconstellation myData = list.get(position);
			title.setText(myData.title);
			title.setVisibility(TextUtils.isEmpty(myData.title) ? View.GONE	: View.VISIBLE);
			constellation.setText(myData.constellation);
			desc.setText(myData.desc);
			return convertView;
		}

	}

	//异步加载数据
	private void asynctaskInstance(String url) {
		AsyncHttpClient client = new AsyncHttpClient();
		client.post(url, new AsyncHttpResponseHandler() {

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

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

}
