/**
 * 
 */
package com.qiyou.mb.android.ui.fragments;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;

import com.qiyou.mb.android.R;
import com.qiyou.mb.android.utils.NoScrollGridView;
import com.qiyou.mb.android.utils.enums.TrackType;
//import android.widget.ImageView;

/**
 * @author mwang
 *
 */
public class TrackList_fragment extends /*TrackMap_fragment *//*TrackingBasic_fragment*/  Querybase_fragment 
															 {
//	  public static final String TAG ="com.qiyou.TrackList_fragment";
//	  public static final String dTAG ="com.qiyou";
	@Override
	public void setFTag(){
		TAG="com.qiyou.TrackList_fragment";
//		FirstTabTextColor= getResources().getColorStateList(R.color.red);   
//		FirstTabBackGround=R.drawable.icon_databse;
	}
	
	public static String getFTag(){
		return "com.qiyou.TrackList_fragment";
	}
	@Override
	public String getCurrentTag(){
		return "com.qiyou.TrackList_fragment";
	}
	
	    private   NoScrollGridView picGrid;

	    
	    
	   /* private EditText edUserName;
	    private EditText edSvrNo;
	    private EditText webCity;
	    
	    private ImageButton btn_webquery;
	    		ImageButton btn_pageup;
	    		ImageButton btn_pagedown;
	    private CheckBox chk_web_pic; 
	    private CheckBox chk_my_tracks;
	    boolean isShowPic; 
	    private boolean isShowMytrackOnly; 
	   
	   
	    private LinearLayout ll_web_others;
	    private LinearLayout ll_webinput;*/
	
//	    private String webUserName="";
//	    private String webSvrNo="";
	    @Override
		public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
 			if(activity==null){
 			    activity = (com.qiyou.mb.android.ui.MainActivity)getActivity();
  			}
			firstLoad=true;
			if(activity!=null &&activity.app!=null){
			 webTrackType = activity.app.getLastTracktype();
//			 IL= new com.qiyou.mb.android.utils.ImageLoader(activity);
			} 
			 IL= new com.qiyou.mb.android.utils.ImageLoader(getActivity());
	    }
	    
	  @Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
		  
			  vContainer = inflater.inflate(R.layout.track_list, container, false);	 
//			  noRec = (TextView) vContainer.findViewById(R.id.trakList_tv_norec);  
			  localTracklist =(ListView)vContainer.findViewById(R.id.tr_list);  
			  localTracklist.setOnItemClickListener(trackListItemListener);
			  
		 
				
			  chk_show_pic = (CheckBox)vContainer.findViewById(R.id.fm_chk_show_pic);  
			  chk_show_pic.setOnCheckedChangeListener(m_checkboxListener);
			  chk_show_map = (CheckBox)vContainer.findViewById(R.id.fm_chk_show_map);  
			  chk_show_map.setOnCheckedChangeListener(m_checkboxListener);
 			  initialView();
			  showCategoryList(cateLv,null);
			  activity.updateActionBarTitle(R.string.frg_list);	  
			  return vContainer;
	  }
	  
	 
	  
	  CheckBox.OnCheckedChangeListener m_checkboxListener =new  CheckBox.OnCheckedChangeListener(){
		    @Override
			public void onCheckedChanged(CompoundButton buttonView,
						boolean isChecked) {
					// TODO Auto-generated method stub
		    	switch (buttonView.getId() ){
	    		/*case R.id.fm_chk_web_pic:
	    			isShowPic = !chk_web_pic.isChecked();	
//	    			 doWebTrackQuery();
	    			break;*/
	    		case R.id.fm_chk_show_pic:
	    			doTrackQuery();
	    			break;
	    		case R.id.fm_chk_show_map:
	    			doTrackQuery();
	    			break;
	    		 
	    			 
		    	}
		    	 
		    	 
		  }
		    
	   }; 
//	  @Override
//		protected void initialView(){ 
//	        super.initialView(); 
//	  }
	  
	   
		
	 @Override
	  public void onActivityCreated(Bundle savedInstanceState) {
	    super.onActivityCreated(savedInstanceState);
	  
 
	    getLoaderManager().initLoader(0, null, this);
 	 }
	 
	 
	 
	 
	 
	    final String  dateTypeTab ="轨迹";
		final String catesTab ="标签";
//		final String inactiveTab ="回收站";
		final String routeTab ="路书";
 
		protected void initialTabhost(){

			tabhost = (TabHost)vContainer.findViewById(R.id.queryTabhost);
			if(tabhost==null)return;
			tabhost.setup(); 
	        
	        TabHost.TabSpec statTab = tabhost.newTabSpec(dateTypeTab);  
	        	statTab.setIndicator(dateTypeTab);
	        	statTab.setContent(R.id.tab_data_type_picker);
	        	tabhost.addTab(statTab); 
	        	
			  TabHost.TabSpec t1 = tabhost.newTabSpec(routeTab);  
			 t1.setIndicator(routeTab);
			 t1.setContent(R.id.tab_route);
			 tabhost.addTab(t1); 
			 
			 TabHost.TabSpec t2 = tabhost.newTabSpec(catesTab);  
			 t2.setIndicator(catesTab);
			 t2.setContent(R.id.tab_categories);
			 tabhost.addTab(t2);
			 
		 
			 
			 
		    initialTabhostStyle(tabhost);
			 
	 

		    tabhost.setOnTabChangedListener(new OnTabChangeListener() {
	            @Override
	            public void onTabChanged(String tabId  ) {
	 
 
	            	 onMyTabChanged(tabId);
	            	 
 
	            	 
	            	
	            }
	        });
	        
		}  
	  
//	 String getShortString(String str,int lng){
////		 String value =cursor.getString(6);
//		 str =TextUtils.isEmpty(str)?str:str.length()>lng?str.substring(0,lng)+"...":str;
//		 str =TextUtils.isEmpty(str)?str:"  "+str;
//		 return   str;
//	 }
	 
	/*  *//**
	   * {@inheritDoc}
	   * When a venue is clicked, fetch the details from your server and display the detail page.
	   *//*
	  @Override
	  public void onListItemClick(ListView l, View v, int position, long theid) {
	    super.onListItemClick(l, v, position, theid);
	   //  activity.app.setCurrentTrack(null);
	    // Find the ID and Reference of the selected track.
 
	    Cursor c = adapter.getCursor();
	    c.moveToPosition(position);	 
	    String id = c.getString(c.getColumnIndex(TrackProvider.KEY_ID));	 
	    QLog.getLogger().d(dTAG, TAG+ "start request for track of id "+ id  ); 
	     
	    // Request the parent Activity display the track detail UI.
	    activity.showTrackDetail(id);
	  }
	   */ 
	   // 条目上单击处理方法.  
//	 int selectedTrackItem=-1;
//	 int selectedActItem=-1;
//	 int selectMemberItem=-1;
//	 int selectActShareItem=-1;
//	 int selectGroupItem=-1;
//	 boolean isActShareQuery=false;
	 
		
	    
	    @Override
	    void showDetailTrack(int trackId){
 
//	    	QLog.getLogger().d(dTAG, TAG+String.format("  to show track detail....is NormalQuery ？ %s,is isRouteQuery ? %s ,is isByCatesQuery? %s " , isNormalQuery,isRouteQuery,isByCatesQuery));
			
	    	switchTabOptions(currentTab);
	
	    	if(isNormalQuery){
//		    		if(lastTypeId==TrackType.GPX.toInt())
//		    			activity.showGpxTrackDetail(lastTypeId,trackId,false);
//		    		else
		    			activity.showTrackDetail(lastTypeId,trackId);
	    	}else
	    		if( isRouteQuery /*||isInactiveQuery*/ ||isByCatesQuery ){
		    		int trackType =lastTypeId;
		    		if(activity.app.getCurrentTrack().getTrackBean().getTrackId()==trackId){
		    			trackType =activity.app.getCurrentTrack().getTrackBean().getType().toInt();
		    		}
		    		if( isRouteQuery || trackType==TrackType.GPX.toInt()||activity.app.getCurrentTrack().isLushu())
		    			activity.showGpxTrackDetail(TrackType.GPX.toInt(),trackId,false);
		    		else
		    			activity.showTrackDetail(trackType,trackId);
	    	} 
	    		/*else if (this.isWebTrackQuery||this.isActShareQuery){
	       
	            this.activity.showGpxTrackDetail( this.svrTrackType , trackId, true);
	          }*/
	    	
	    	
	    	
	    	
	    	
	    	
	    }
	   /* private OnClickListener avatarClickListener = new OnClickListener() {


	    	@SuppressWarnings("unchecked")

	    	public void onClick(View v) {

	    	Map item = (Map) v.getTag();//还记得上面的那个setTag么？我们在这里给丫取出来

	    	Intent mIntent = new Intent(AdapterWithHashMap.this.context,

	    	MySelf.class);

	    	mIntent.putExtra("user_id", item.get("uid").toString());


	    	AdapterWithHashMap.this.context.startActivity(mIntent);

	    	}

	    	};
	    */
	  
	    
	
	@Override
	public void onResume() { 
//        activity.app.currentActiveFragment =TAG;
		super.onResume();
		activity.updateActionBarTitle( getResources().getString(R.string.frg_list));
//	   	QLog.getLogger().e(dTAG,TAG+" app.currentActiveFragment of trackList: "+TAG);
	   	
	   
	}
	@Override
	public void onPause(){
		super.onPause();
		if(progressDialog!=null)progressDialog.dismiss();
	}
	 
//	   com.qiyou.mb.android.utils.ImageLoader IL ; 	 
	  
	   
	

	
	

	
	
	 
			
			
			
			
	  @Override
		public boolean isShowBottomTabs() {
			// TODO Auto-generated method stub
			return false;
		}
		@Override
		public void setTabOnFiling() {
			 
			 maxTabIndex = 3;
			
		}
		@Override
		protected void setSelectedTab(){
		 	if(tabhost!=null&& tabhost.getCurrentTab()!=currentTab){
		 		tabhost.setCurrentTab(currentTab);
		   	}
		}
		
		
		
		@Override
		 protected void onMyTabChanged(String tabId){
 			super.onMyTabChanged(tabId);
			/*if(tabhost==null)return;  
			 initialTabhostStyle(tabhost);
			 setSelectedTabhostStyle(tabhost);
			 currentTab= tabhost.getCurrentTab();*/
 			processTabQuery();
 			
			 
		 }
		
		void processTabQuery(){
			if(progressDialog!=null)
				 progressDialog.dismiss();
//			 noRec.setVisibility(View.GONE); 
		
			 switchTabOptions(currentTab);
			 
			 localTracklist.setVisibility(View.GONE);
		    
		     
		     canclick =true;
			 if(currentTab==0){
				 this.localTracklist.setVisibility(View.VISIBLE);
				 doTrackQuery();			 
			 }else if(isByCatesQuery){
				 this.localTracklist.setVisibility(View.VISIBLE);
				 doCatesQuery();
//			 }else if(isInactiveQuery){
//				 doInactiveQuery();
			 }else if(isRouteQuery){
				 this.localTracklist.setVisibility(View.VISIBLE);
				 doRouteQuery();			 
			 } 
		}
	  

		void switchTabOptions(int queryType){ 
			 isNormalQuery =queryType ==0;
			 isRouteQuery =queryType ==1;
			 isByCatesQuery =queryType ==2;
		    
		}

		@Override
		public void doQuery() {
			// TODO Auto-generated method stub
			doTrackQuery();
		}
}

