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.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
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.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.Home;
import com.mobile.constellations.util.ViewHolder;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class HomeFragment extends Fragment implements OnClickListener, OnItemClickListener {

	private View layout;
	private LinearLayout homeHeader;
	private MyAdapter myAdapter;
	private ImageLoader imageLoader = ImageLoader.getInstance();
	private DisplayImageOptions options;
	private ArrayList<Home> list = new ArrayList<Home>();

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		if (layout == null) {
			layout = inflater.inflate(R.layout.fragment_tab_home, null);
			resetTitlebar();
			initUI();
		}
		ViewGroup parent = (ViewGroup) layout.getParent();
		if (parent != null) {
			parent.removeView(layout);
		}
		return layout;
	}

	private void resetTitlebar() {
		View include_tilebar = layout.findViewById(R.id.include_titlebar);
		TextView title = (TextView) include_tilebar.findViewById(R.id.tv_title);
		title.setText("首页");
	}

	private void initUI() {
		ListView listHome = (ListView) layout.findViewById(R.id.listView1);
		View headHome = getActivity().getLayoutInflater().inflate(R.layout.header_listview_home, null);
		homeHeader = (LinearLayout) headHome.findViewById(R.id.home_header);
		LinearLayout baiyang = (LinearLayout) headHome.findViewById(R.id.baiyang);
		LinearLayout jinniu = (LinearLayout) headHome.findViewById(R.id.jinniu);
		LinearLayout shuangzi = (LinearLayout) headHome.findViewById(R.id.shuangzi);
		LinearLayout juxie = (LinearLayout) headHome.findViewById(R.id.juxie);
		LinearLayout shizi = (LinearLayout) headHome.findViewById(R.id.shizi);
		LinearLayout chunv = (LinearLayout) headHome.findViewById(R.id.chunv);
		LinearLayout tiancheng = (LinearLayout) headHome.findViewById(R.id.tiancheng);
		LinearLayout tianxie = (LinearLayout) headHome.findViewById(R.id.tianxie);
		LinearLayout sheshou = (LinearLayout) headHome.findViewById(R.id.sheshou);
		LinearLayout mojie = (LinearLayout) headHome.findViewById(R.id.mojie);
		LinearLayout shuiping = (LinearLayout) headHome.findViewById(R.id.shuiping);
		LinearLayout shuangyu = (LinearLayout) headHome.findViewById(R.id.shuangyu);
		TextView allman = (TextView) headHome.findViewById(R.id.allman);
		TextView allwoman = (TextView) headHome.findViewById(R.id.allwoman);
		asynctaskInstance();
		listHome.addHeaderView(headHome);
		myAdapter = new MyAdapter();
		listHome.setAdapter(myAdapter);
		listHome.setOnItemClickListener(this);
		baiyang.setOnClickListener(this);
		jinniu.setOnClickListener(this);
		shuangzi.setOnClickListener(this);
		juxie.setOnClickListener(this);
		shizi.setOnClickListener(this);
		chunv.setOnClickListener(this);
		tiancheng.setOnClickListener(this);
		tianxie.setOnClickListener(this);
		sheshou.setOnClickListener(this);
		mojie.setOnClickListener(this);
		shuiping.setOnClickListener(this);
		shuangyu.setOnClickListener(this);
		allman.setOnClickListener(this);
		allwoman.setOnClickListener(this);
		// UmengUpdateAgent.update(this);
	}

	// 数据的异步加载
	private void asynctaskInstance() {
		AsyncHttpClient client = new AsyncHttpClient();
		client.post(Contans.PATH_HOME, 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");
					String content = jsonObject.getString("content");
					if (code == 1) {
						JSONArray jsonArray = new JSONArray(content);
						for (int i = 0; i < jsonArray.length(); i++) {
							// 解析各字段，并将数据传入容器中
							JSONObject object = jsonArray.getJSONObject(i);
							String title = object.getString("title");
							String image = object.getString("image");
							String img = object.getString("img");
							String date = object.getString("date");
							String desc = object.getString("desc");
							String character1 = object.getString("character1");
							String character2 = object.getString("character2");
							String character3 = object.getString("character3");
							String character4 = object.getString("character4");
							String character5 = object.getString("character5");
							String character6 = object.getString("character6");
							String character7 = object.getString("character7");
							String character8 = object.getString("character8");
							String character9 = object.getString("character9");
							String character10 = object.getString("character10");
							String character11 = object.getString("character11");
							String character12 = object.getString("character12");
							String character13 = object.getString("character13");
							String character14 = object.getString("character14");
							Home myData = new Home(title, image, img, date, desc, character1, character2, character3, character4, character5, character6, character7, character8, character9,
									character10, character11, character12, character13, character14);
							list.add(myData);
						}
					}
					homeHeader.setVisibility(View.VISIBLE);
					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 = getActivity().getLayoutInflater().inflate(R.layout.item_listview_home, null);
			}
			// 找到对应行布局UI
			TextView desc = ViewHolder.get(convertView, R.id.textView1);
			ImageView img = ViewHolder.get(convertView, R.id.imageView1);
			// 讲容器的值显示到对应的控件
			Home myData = list.get(position);
			desc.setText("    " + myData.desc);
			imageLoader.displayImage(myData.img, img, options, null);
			return convertView;
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		// 跳转到白羊座界面
		case R.id.baiyang:
			CommConsDescActivity.intentCommConsDescActivity(getActivity(), Contans.PATH_BAIYANG);
			break;

		// 跳转到金牛座界面
		case R.id.jinniu:
			CommConsDescActivity.intentCommConsDescActivity(getActivity(), Contans.PATH_JINNIU);
			break;

		// 跳转到双子座界面
		case R.id.shuangzi:
			CommConsDescActivity.intentCommConsDescActivity(getActivity(), Contans.PATH_SHUANGZI);
			break;

		// 跳转到巨蟹座界面
		case R.id.juxie:
			CommConsDescActivity.intentCommConsDescActivity(getActivity(), Contans.PATH_JUXIE);
			break;

		// 跳转到狮子座界面
		case R.id.shizi:
			CommConsDescActivity.intentCommConsDescActivity(getActivity(), Contans.PATH_SHIZI);
			break;

		// 跳转到处女座界面
		case R.id.chunv:
			CommConsDescActivity.intentCommConsDescActivity(getActivity(), Contans.PATH_CHUNV);
			break;

		// 跳转到天秤座界面
		case R.id.tiancheng:
			CommConsDescActivity.intentCommConsDescActivity(getActivity(), Contans.PATH_TIANCHENG);
			break;

		// 跳转到天蝎座界面
		case R.id.tianxie:
			CommConsDescActivity.intentCommConsDescActivity(getActivity(), Contans.PATH_TIANXIE);
			break;

		// 跳转到射手座界面
		case R.id.sheshou:
			CommConsDescActivity.intentCommConsDescActivity(getActivity(), Contans.PATH_SHESHOU);
			break;

		// 跳转到摩羯座界面
		case R.id.mojie:
			CommConsDescActivity.intentCommConsDescActivity(getActivity(), Contans.PATH_MOJIE);
			break;
		// 跳转到水平座界面
		case R.id.shuiping:
			CommConsDescActivity.intentCommConsDescActivity(getActivity(), Contans.PATH_SHUIPING);
			break;

		// 跳转到双鱼座界面
		case R.id.shuangyu:
			CommConsDescActivity.intentCommConsDescActivity(getActivity(), Contans.PATH_SHUANGYU);
			break;

		// 跳转到12星座男忌讳女朋友问什么界面
		case R.id.allman:
			AllConstellationActivity.intentAllConActivity(getActivity(), Contans.PATH_ALL_MAN);
			break;

		// 跳转到12星座女遇到坏男人指数界面
		case R.id.allwoman:
			AllConstellationActivity.intentAllConActivity(getActivity(), Contans.PATH_ALL_WOMAN);
			break;

		// 没有数据的时候，点击图片监听事件实现重新加载数据
		case R.id.imageView1:
			asynctaskInstance();
			break;

		default:
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		if (position == 0) {
			return;
		}
		Home myData = list.get(position - 1);
		ConDescActivity.intentConDescActivity(getActivity(), myData);
	}

}
