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.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.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
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.SwingLeftInAnimationAdapter;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.mobile.constellations.R;
import com.mobile.constellations.data.CommConsDesc;
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;
import com.zdp.aseo.content.AseoZdpAseo;

public class CommConsDescActivity extends Activity implements OnItemClickListener {

	private MyAdapter myAdapter;
	public static String URL = "url";
	private ImageLoader imageLoader = ImageLoader.getInstance();
	private DisplayImageOptions options;
	private ArrayList<CommConsDesc> list = new ArrayList<CommConsDesc>();
	private ImageView img_header;
	private String txturl;
	private String name;
	private TextView title;

	public static void intentCommConsDescActivity(Context context, String url) {
		Intent intent = new Intent(context, CommConsDescActivity.class);
		intent.putExtra(URL, url);
		context.startActivity(intent);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		super.onCreate(savedInstanceState);
		String url = getIntent().getStringExtra(URL);
		initOptions();
		asynctaskInstance(url);
		setContentView(R.layout.activity_comm_cons_desc);
		resetTitlebar();
		showAd();
		AseoZdpAseo.initType(this, AseoZdpAseo.SCREEN_TYPE);
		ListView listView = (ListView) findViewById(R.id.listView1);
		View header = getLayoutInflater().inflate(R.layout.header_listview_commconsdesc, null);
		img_header = (ImageView) header.findViewById(R.id.imageView1);
		listView.addHeaderView(header);
		myAdapter = new MyAdapter();
		SwingLeftInAnimationAdapter swingLeftInAnimationAdapter = new SwingLeftInAnimationAdapter(myAdapter);
		swingLeftInAnimationAdapter.setListView(listView);
		listView.setAdapter(swingLeftInAnimationAdapter);
		listView.setOnItemClickListener(this);
	}

	private void showAd() {

	}

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

	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);
					int code = jsonObject.getInt("code");
					if (code == 1) {
						name = jsonObject.getString("name");
						txturl = jsonObject.getString("txturl");
						String image = jsonObject.getString("image");
						String content = jsonObject.getString("content");
						JSONArray jsonArray = new JSONArray(content);
						for (int i = 0; i < jsonArray.length(); i++) {
							JSONObject object = jsonArray.getJSONObject(i);
							String title = object.getString("title");
							CommConsDesc myData = new CommConsDesc(title);
							list.add(myData);
						}
						title.setText(name);
						imageLoader.displayImage(image, img_header, options, null);
					}
					myAdapter.notifyDataSetChanged();
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}

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

	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();
	}

	private final class SimpleBitmapDisplayer implements BitmapDisplayer {
		@Override
		public void display(Bitmap bitmap, ImageAware imageAware, LoadedFrom loadedFrom) {
			imageAware.setImageBitmap(bitmap);
		}
	}

	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_commconsdesc, null);
			}
			TextView title = ViewHolder.get(convertView, R.id.title);
			CommConsDesc myData = list.get(position);
			title.setText(myData.title);
			return convertView;
		}

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		if (position == 0) {
			return;
		}
		ConsContentActivity.intentConsContentActivity(this, txturl + position + ".txt");
	}

}
