package com.lianxin.pubqq.main;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
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.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lianxin.pubqq.activity.InviteListActivity;
import com.lianxin.panqq.client.callback.listCallBack;
import com.lianxin.panqq.common.GloableParams;
import com.lianxin.panqq.common.Utils;
import com.lianxin.pubqq.main.MainActivity;
import com.lianxin.pubqq.R;
import com.lianxin.pubqq.main.adpter.RecentAdpter;
import com.lianxin.panqq.list.ExRecentListView;
import com.lianxin.panqq.main.bean.RecentItem;
import com.lianxin.pubqq.chat.EMChatActivity;
import com.lianxin.panqq.common.NetUtil;
//最新消息
public class Fragment_Recent extends Fragment implements OnClickListener,
              OnItemClickListener {
	private Activity ctx;
	private View layout, layout_head;;
	public RelativeLayout errorItem;
	public TextView errorText;
    protected ListView listView;
	private ExRecentListView mListView;	
	private RecentAdpter adpter;
	private List<RecentItem> recentList = GloableParams.RecentList;	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (layout == null) {
			ctx = this.getActivity();
			layout = ctx.getLayoutInflater().inflate(R.layout.fragment_msg,
					null);
	        mListView = (ExRecentListView) layout.findViewById(R.id.xListView); 
			mListView.setPullLoadEnable(true);	        
			listView = (ListView)mListView;	
	
			
			errorItem = (RelativeLayout) layout
					.findViewById(R.id.rl_error_item);
			errorText = (TextView) errorItem
					.findViewById(R.id.tv_connect_errormsg);		
			mListView.initData();			
			initViews();			
			setOnListener();	
		} else {
			ViewGroup parent = (ViewGroup) layout.getParent();
			if (parent != null) {
				parent.removeView(layout);
			}
		}
		return layout;
	}

	@Override
	public void onResume() {
		super.onResume();
		//mListView.initData();		
		//initViews();
	}

	/**
	 * 刷新页面
	 */
	public void refresh() {
		
		mListView.resetData();
		adpter.notifyDataSetChanged();		
	}

	public void refreshNewUser() {
		recentList.clear();

		mListView.resetData();	
		adpter.notifyDataSetChanged();		
	}	
	
	private void initViews() {
	
		adpter = new RecentAdpter(getActivity(), recentList);		
		listView.setAdapter(adpter);
		mListView.setDataAdapter(recentList,adpter);		
		mListView.setStatusListener(new listCallBack(){
			@Override
			public void onSuccess(int statusCode, byte[] recvBuf) {
		
			}
			@Override
			public void onFailure(int errorCode, String errorMessage) {
				// TODO Auto-generated method stub
				
			}
		});		
	
	}
	
	private void setOnListener() {
		listView.setOnItemClickListener(this);
		errorItem.setOnClickListener(this);

	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position,
			long arg3) {
			
		if ( position <= 0) {
	
		} else {
			((MainActivity) getActivity()).updateUnreadLabel();
			RecentItem conversation = recentList.get(position-1);

//			Hashtable<String, String> ChatRecord = adpter.getChatRecord();
			int nChatType = conversation.getChatType();
			int nChatId = conversation.getUserId();
			String nChatName = conversation.getName();
			int nNewMsg = conversation.getNewNum();	
	
			if(nChatId<1000&&nChatType<5)
			{
	
				Intent intent = new Intent(getActivity(), InviteListActivity.class);
			   // startActivity(intent);	
			    Utils.start_Activity(getActivity(),intent); 			    
			}
			else
			{
		//		Intent intent = new Intent(getActivity(), ChatActivity.class);				
			Intent intent = new Intent(getActivity(), EMChatActivity.class);
			intent.putExtra("Chat_ID",nChatId);
			intent.putExtra("Chat_Type",nChatType);
			intent.putExtra("Chat_Name",nChatName);
		
			//getActivity().startActivity(intent);
			Utils.start_Activity(getActivity(),intent); 			
			}
	
		}
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.rl_error_item) {
			NetUtil.openSetNetWork(getActivity());
		}
	}

}
