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

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TextView; 
import com.loopj.android.http.RequestParams;
import com.qiyou.mb.android.QiYouConstant;
import com.qiyou.mb.android.R;
import com.qiyou.mb.android.beans.Track;
import com.qiyou.mb.android.beans.basic.GEO_bean;
import com.qiyou.mb.android.beans.basic.Group_bean;
import com.qiyou.mb.android.beans.basic.User_bean;
import com.qiyou.mb.android.utils.Utils;
import com.qiyou.mb.android.utils.enums.BBSCate;
import com.qiyou.mb.android.utils.exception.NoWebObjectException;
import com.qiyou.mb.android.utils.log.QLog;
import com.qiyou.mb.android.utils.server.RestMethods;
import com.umeng.socialize.utils.Log;

public class UserHome extends GroupHome /*TrackingBasic_fragment*/ {
/*	@Override
	public String getCurrentTag() {
		// TODO Auto-generated method stub
		setFTag();
		return TAG;
	}*/

	@Override
	public void setFTag() {
		// TODO Auto-generated method stub
		TAG="com.qiyou.UserHome";
//		FirstTabTextColor= getResources().getColorStateList(R.color.red);    
//		FirstTabBackGround=R.drawable.icon_user;
	}

	public static String getFTag() {
		return "com.qiyou.UserHome";
		
	}
	
//	public class UserProfile extends TrackList_fragment {
//	protected int currentLayout;
//	int userId;
	private User_bean user;
	
	public static UserHome newInstance(User_bean user/*,int userId*/)
	{
		
		UserHome f = new UserHome(); 
		f.user=user;
	 
	    return f;
	}
	public static UserHome newInstance()
	{
		
		UserHome f = new UserHome(); 
	 
 
	    return f;
	}
	
	
	public void setCurrentUser(User_bean user){
		this.user =user;
	 
//		initialWebQuery();
	}
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
 
		 
	}

	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {   
		 
		vContainer = inflater.inflate(R.layout.user_home, container, false);
		
		
		initialViews();  
		  title = user.getId()>0 && user.getId()==activity.app.user.getUserbean().getId()?"我的信息"
				  :getResources().getString(R.string.frg_userdetail) +"｜"+user.getUserName()  ;
		return vContainer;
		}		
	
	
	
	 
	
 
 
	 
	private TextView userTitle,  userCalori, userDist, faraway,userSvrno;
 
	@Override
	void  initialExtraViews(){
		
		userTitle  = (TextView)vContainer.findViewById(R.id.usr_name);
 		userCalori  = (TextView)vContainer.findViewById(R.id.usr_calorie); 
		userDist  = (TextView)vContainer.findViewById(R.id.usr_dist);
		faraway  = (TextView)vContainer.findViewById(R.id.usr_faraway);
		userSvrno  = (TextView)vContainer.findViewById(R.id.usr_svrno);
		 
		  
	}
	
	@Override
	void showWebObject(){
		if(user!=null){
			getDetails();	
		}
			
	}
	
 
	 
	
	
	
	@Override
	void addTabs(){ 
	  
//	 
			
			 tabhost.addTab(tabBbs);
			
			 tabhost.addTab(tabTrack); 
			 
			
			 tabhost.addTab(tabAct);
			 
			
			 tabhost.addTab(tabGrp);
			 tabhost.addTab(tabLive);
			 
 	 
//				  
		 
	}
 
 
//	 private  int currentActTab;
//	  //not to remember last selected tab.
//    @Override
//	  protected void setSelectedTab(){
//  	  	if(tabhost!=null&& tabhost.getCurrentTab()!=currentActTab){
//  	   		tabhost.setCurrentTab(currentActTab);
//  	   	} 
//	 }  
    
    @Override
  	void getQueryType(int queryType){
  		 isBBSQuery = queryType == 0; 
  		 isGroupQuery = queryType==3;
  		 isActivityQuery = queryType==2;
  		 isShareQuery =queryType==1;
  		 isLiveQuery =queryType==4;
//  		 currentActTab = currentTab;
  		 
  	}
//	@Override
//	void processTabQuery(){	
//		super.processTabQuery();
//	
//		if(isGroupQuery){
//			 lvWeb.setVisibility(View.VISIBLE); 
//				 //			 QLog.getLogger().d(dTAG, "grpData is Null? " + (grpData==null));
//				if(grpData==null)
////					doActQuery();
//					doGroupQuery();
//				else
//					showGrpData();
//				if(grpData!=null)
//					showNoRecText(grpData.size()==0);
//			 
//			 
//		 }
//	}
 
	@Override
	 void showObjDetail(){
		 activity.showUserDetails(user);
	 }
	
	@Override
	void getDetails(){
		
		this.BBScate = BBSCate.USER.toInt(); //act
		this.BBSSubcate = user.getId();
		this.BBSsubCateName =user.getUserName();
		 BBSSubcateOwner = user.getId();
		 
		userTitle.setText(user.getUserName()) ;
		userCalori.setText( String.format("%.0f",user.getCalorie()));
		userDist.setText( String.format("%.0f",user.getDistance()/1000)); 
		faraway.setText(  user.getLat()>0 && QiYouConstant.PV_LAST_LAT>0? String.format("%.1f",   Utils.getDistanceFromXtoY(   user.getLat(), user.getLon(),
					QiYouConstant.PV_LAST_LAT, QiYouConstant.PV_LAST_LON)/1000 ):"--" ) ;
//		userSvrno.setText(user.getId()+"") ;
		userSvrno.setText(String.format("%s %s", user.getGeo().getCityName(),user.getGeo().getDistrictName()).replace("null", ""));
		
		
	 
		
		
//		txtAccount.setText(user.getUserName());
////		this.edit_act_startGeo.setText(user.getGeo()==null?us);
//		if(user.getGeo()!=null){
//			urlFormat(R.id.act_txt_startGeo_map,R.id.act_txt_startGeo_share,user.getGeo().getSharedAddressUrl()); 
//			 
//				this.edit_act_startGeo.setText(
//						(TextUtils.isEmpty(user.getGeo().getCityName())?"":user.getGeo().getCityName())  
//						+  
//						(TextUtils.isEmpty(user.getGeo().getDistrictName())?"":user.getGeo().getDistrictName())
//						
//						/*+ user.getGeo().getStreetName()*/);
//			
//		}
//		 
//		edit_mail.setText(user.getMail());
//		
//		act_edit_description.setText(user.getUserDesc());
//		
//		edit_act_startTime.setText(Utils.getFormatedTime(user.getRegtime())); 
//		
////		edit_act_distance.setText(Utils.getFormatedDistance(user.getDistance())); 
//		edit_act_distance.setText(String.format("%.1f",  user.getDistance()/1000)); 
//
// 		edit_score.setText(user.getScore()+"");
// 		String imgUrl=user.getQqAbulmUrl();
// 		if(!TextUtils.isEmpty(imgUrl)){
// 			this.IL.DisplayImage(imgUrl, imgAbulm);	
// 		}
// 		
// 		act_svrNo.setText(user.getId()>0?user.getId()+"":"") ;
	}
	
 
 
	@Override	  
    void refresh(){
		
		
		
	     
		 if(currentTab==0){
			 requestUserFromWeb(user.getId());
			 return  ;
 
				  
		  }else if(isLiveQuery){
//			 showLive(); 
			 return  ;
		  } 
				 
	    
		 this.initialWebQuery();
		   
		
	}
	 
	 
 
	  
	
	
	
	
	RequestParams buildLiveQueryParames(){
		RequestParams params = new RequestParams(); 
//		final int GEO_CITY_TYPE=2;
	 
//	    if(activity.currentAcitiviy==null) return null;
		params.add("userId", String.valueOf(user.getId()));
		params.add("from", String.valueOf(lastMaxLiveTime)); 
		
		//			 QLog.getLogger().d(dTAG, TAG+" web query params: "+params);
	 
		return params;
	}
	
	
	 
	 @Override
	 void showMember(User_bean user){
    	 this.getDetails();
	  }
	 @Override
	 RequestParams buildShareQueryParames(){
	 
//			RequestParams params = new RequestParams(); 
		    
			RequestParams params = 	super.buildShareQueryParames();
			 if(user.getId()>0){
				 params.add("ownerId", String.valueOf(user.getId()));
			 }
		 
			//			 QLog.getLogger().d(dTAG, TAG+" web query params: "+params);
		 
			return params;
		}
	 
	 @Override
	  RequestParams buildActQueryParames(){
			RequestParams params = new RequestParams();  
			 
    		params.add("userId", String.valueOf(user.getId()));
				
			//			 QLog.getLogger().d(dTAG, TAG+" web query params: "+params);
		 
			return params;
		}
	 	RequestParams buildGroupQueryParames(){
			RequestParams params = super.buildGroupQueryParames() ;  
			params.add("ownerId", String.valueOf(user.getId()));
			return params ;
		 
			
			 
		} 
	 
	 
	/*@Override
	public boolean isShowBottomTabs() {
		// TODO Auto-generated method stub
		return false;
	}*/


	@Override
	public void setTabOnFiling() {
		// TODO Auto-generated method stub 
		 maxTabIndex =5;
		 MAPVIEW_INDEX=maxTabIndex;
		 
	}


	@Override
	public void onResume() { 
//        activity.app.currentActiveFragment =TAG;
		super.onResume();
//        activity.updateActionBarTitle(title);	
        
	   
	}

}
