package com.lianxin.pubqq.nearby;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.Toast;

import com.lianxin.pubqq.R;
import com.lianxin.panqq.common.BaseActivity;
import com.lianxin.panqq.common.Utils;
import com.lianxin.panqq.widget.list.XListView;
import com.lianxin.panqq.widget.list.XListView.IXListViewListener;
//import com.lianxin.panqq.chat.utils.MyLogger;
import com.lianxin.panqq.common.GloableParams;
import com.lianxin.panqq.wifilist.IPScanBean;
import com.lianxin.panqq.wifilist.IPScanConnThread;
import com.lianxin.panqq.wifilist.ScanListAdapter;
import com.lianxin.panqq.wifilist.ScanListAdapter.OnMyButClickListener;

/**
 * 我的分类列表
 *
 */
public class ScanListActivity extends BaseActivity implements OnClickListener,IXListViewListener {
    
	private XListView mListView;
	private ScanListAdapter mAdapter;
	private List<IPScanBean> mIpList;//搜索用户列表
	//private int groupId = 10058;	
	//private int groupType = DefineUser.TYPE_CROWD;		
	public static final int FRIST_GET_DATE = 111;
	public static final int REFRESH_GET_DATE = 112;
	public static final int LOADMORE_GET_DATE = 113;
	
	//public static final int LOADMORE_GET_SEX = 114;
	
	private static int page = 0;
	//private static MyLogger Log = MyLogger.yLog();
	
    @Override
   	public void onCreate(Bundle savedInstanceState) {
      	 
   		super.onCreate(savedInstanceState);
   		setContentView(R.layout.wifi_activity_scanlist);
		mListView = (XListView)findViewById(R.id.user_xListView);
		mListView.setPullLoadEnable(true);

		//groupType = getIntent().getIntExtra("TypeType", DefineUser.TYPE_CROWD);
		//groupId = getIntent().getIntExtra("TypeID", 10001);			
	//	initData();	
		mIpList = new ArrayList<IPScanBean>();		
		mAdapter =  new ScanListAdapter(this, mIpList);
		mListView.setAdapter(mAdapter);
		mListView.setXListViewListener(this);
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View v, int position,
					long id) {
	            Toast.makeText(context, "I am ", Toast.LENGTH_SHORT).show(); 				
         	}
			
		});
		resetData();	//刷新远程数据
		mAdapter.setButListener(new OnMyButClickListener(){  

			@Override
			public void onButClicked(int position) {
				// TODO Auto-generated method stub
				onItemButtonClick(position);				
		
			}  
        });			
    }
 
	@Override
	public void onClick(View v) {
		int id = v.getId();
		if (id == R.id.img_back) {
			Utils.finish(ScanListActivity.this);
			//finish();
			//overridePendingTransition(R.anim.push_right_in,
			//		R.anim.push_right_out);
		} else if (id == R.id.img_right) {
		}
	}     
    
	public void onItemButtonClick(int position) {
		// TODO Auto-generated method stub

    }	
	
	private void initData() {
		//IPScan mWifiScan = new IPScan();
		// 获得Wifi列表信息
		//typeList = mWifiScan.scan();
		
	
	} 
	
    //第一次加载数据
 
    //刷新远程数据
    private void resetData() {
	   	
		IPScanConnThread task = new IPScanConnThread(this,mIpList);
		task.setnetCallBack(new IPScanConnThread.OnDataListener() {
			@Override
			public void onSelect(int n) {

				runOnUiThread(new Runnable() {
					public void run() {
						mAdapter.notifyDataSetChanged();
						mListView.setSelection(1);
					}
				});				
			}
			public void onProcess(final String message) {
				runOnUiThread(new Runnable() {
					public void run() {
						Utils.showLongToast(ScanListActivity.this, message);
					}
				});						
			}			
		});
		task.startIPScanThread();	
	} 

	@Override
	public void onRefresh() { 
		mListView.setPullLoadEnable(false);
		 mListView.setPullRefreshEnable(false);
	    //geneItems(REFRESH_GET_DATE); 
        if(TextUtils.isEmpty(GloableParams.m_szServerIp)) 		 
 		    LoadLocalItems(REFRESH_GET_DATE); 
        else
 		    getRemoteItems(REFRESH_GET_DATE);	    
	}
 
	@SuppressLint("SimpleDateFormat")
	private void onLoadStatus(int allCount,int newCount) {
		// TODO Auto-generated method stub
		mListView.stopRefresh();
		mListView.stopLoadMore();
		if(newCount>=0){
		    SimpleDateFormat  formatter = new SimpleDateFormat("yyyy年MM月dd日   HH:mm:ss");     
		    Date  curDate  =  new Date(System.currentTimeMillis());//获取当前时间     
		    String   str   =   formatter.format(curDate);     
		    mListView.setRefreshTime(str);
		    mListView.setFooterCount(allCount, newCount);	
		}
		mListView.setPullLoadEnable(true);
		mListView.setPullRefreshEnable(true);
	}	
	
	@Override
	public void onLoadMore() { 
		mListView.setPullLoadEnable(false);
		mListView.setPullRefreshEnable(false);

        if(TextUtils.isEmpty(GloableParams.m_szServerIp)) 		 
 		    LoadLocalItems(LOADMORE_GET_DATE); 
        else
 		    getRemoteItems(LOADMORE_GET_DATE); 		
	}

	private void getRemoteItems(final int ACTION) {

	}	
	
	private void LoadLocalItems(final int ACTION) {



	}



 
}
 