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.EditText;
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.Group_bean;
import com.qiyou.mb.android.utils.Utils;
import com.qiyou.mb.android.utils.enums.BBSCate;
import com.qiyou.mb.android.utils.log.QLog;
import com.qiyou.mb.android.utils.server.RestMethods;

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

	@Override
	public void setFTag() {
		// TODO Auto-generated method stub
		TAG="com.qiyou.GroupHome";
//		FirstTabTextColor= getResources().getColorStateList(R.color.red);    
//		FirstTabBackGround=R.drawable.icon_group;
	}
	 
	public static String getFTag() {
		return "com.qiyou.GroupHome";
	}
	
	
	
	private Group_bean currentGroup ;
	public static GroupHome newInstance(Group_bean  group) {
		
		GroupHome f = new GroupHome();
	    f.currentGroup =group;
	    return f;
	}
	
	public static GroupHome newInstance() {
		
		GroupHome f = new GroupHome(); 
	    return f;
	}
	
	public void setCurrentGroup(Group_bean  group){
		currentGroup =group;
//		initialWebQuery();
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
//		activity =(MainActivity)getActivity();
	
//		 if(activity.app!=null)activity.app.currentActiveFragment =TAG;
//		 currentLayout =R.layout.trackingbasic_fragment;
		//			 QLog.getLogger().d(dTAG,TAG+" "+ "onCreate");
//		drawTrack=true;
	}

	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {   
		 
		 if (currentGroup == null) {	      
			  
			 currentGroup = new Group_bean();
		     
		    }
		  
		     title =  getResources().getString(R.string.frg_groupdetail)
		    		 +"｜"+currentGroup.getName();
		    
		     
		currentLayout =R.layout.group_home;
		vContainer = inflater.inflate(currentLayout, container, false);
		
	
		initialViews( ); 
//	 	initialLLViews();
//		 initialMapview();
		
		return vContainer;
		}		
	
	
	private TextView grpTitle, grpLevel, grpTime, grpDist,grpSvrno;
	
	/*@Override
	void initialMapview(){
		super.initialMapview();
	 
	}*/
	
	EditText edit_grpspeed;
	@Override
	void  initialExtraViews(){
	 
		edit_grpspeed = (EditText)vContainer.findViewById(R.id.act_edit_speed); 
		
		
		
		grpLevel  = (TextView)vContainer.findViewById(R.id.grp_level);
		grpTime  = (TextView)vContainer.findViewById(R.id.grp_time);
		grpDist  = (TextView)vContainer.findViewById(R.id.grp_dist); 
		grpTitle  = (TextView)vContainer.findViewById(R.id.grp_name);
		grpSvrno  = (TextView)vContainer.findViewById(R.id.grp_svrno);
		
	 
		
		
		
		
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);  
		   
	}
	@Override
	Group_bean getCurrentGroup(){
		return currentGroup;
	}
	
	
	@Override
	void showWebObject(){
 
			
			getDetails(); 
			
	}
	@Override
	void mapViewOnActivityCreated(){
		 
		if(currentGroup!=null && currentGroup.getLat()>0){
			locateMapCenter(currentGroup.getLat(),currentGroup.getLon());
		 
 
	    
		}else{ 
			if(QiYouConstant.PV_LAST_LAT>0 ){
				locateMapCenter(QiYouConstant.PV_LAST_LAT,QiYouConstant.PV_LAST_LON);
			}
		}
	}
	
	@Override
	protected void	initialViews(){
		
		webTab="队伍";
		roadbookTab ="活动";
		super.initialViews();
	}
  
	@Override
	void addTabs(){
		
//		newTabId=1;
//		 tabBbs = tabhost.newTabSpec(bbsTab);  
//		 tabBbs.setIndicator(bbsTab);
////		 tabBbs.setContent(R.id.tab_bbs); 
//		 tabBbs.setContent(tabResourceId); 
		 
		 tabhost.addTab(tabBbs); 
		 	 
		 tabhost.addTab(tabUser); 
		 tabhost.addTab(tabAct);  
		 tabhost.addTab(tabTrack); 
		 
		 tabhost.addTab(tabLive); 
		 
	}
  
 
 
	@Override
	void getDetails(){
		
		this.BBScate = BBSCate.GROUP.toInt(); //act
		this.BBSSubcate = currentGroup.getId();
		this.BBSsubCateName =currentGroup.getName();
		 BBSSubcateOwner = currentGroup.getUserId();
		grpTitle.setText(currentGroup.getName());
		grpLevel.setText(currentGroup.getLevel()+"");
		grpTime.setText( String.format("%.1f",  currentGroup.getSportTime()/1000/3600.0) );
//		grpSvrno.setText(currentGroup.getId()+"");
		grpSvrno.setText(String.format("距你 %s", Utils.getDistanceRange(/*ab.getDistrange()*/0, currentGroup.getLat(), currentGroup.getLon())));
		grpDist.setText(  String.format("%.1f",  currentGroup.getDistance()/1000) );
		
		
		
		 
		/*
		if(currentGroup.getId()>0  ){
			act_creator.setText(currentGroup.getUserName());
		}else{
			act_creator.setText(activity.app.user.getUserbean().getUserName());
		}
		
		act_edit_title.setText(currentGroup.getName());
//		this.edit_act_endGeo.setText(currentGroup.getEndAddress());
//		this.edit_act_startGeo.setText(currentGroup.getStartAddress()); 
		this.edit_act_startGeo.setText(currentGroup.getPlace());
		urlFormat(R.id.act_txt_startGeo_map,R.id.act_txt_startGeo_share,currentGroup.getGeo().getSharedAddressUrl());
		
		if(currentGroup.getId()>0){
			act_svrNo.setText(currentGroup.getId()+"");
		}
		 
		act_edit_description.setText(currentGroup.getDesc());
//		if(currentGroup.getId()>0)
		act_levlRatingBar.setRating(currentGroup.getLevel());
		
		edit_act_startTime.setText(Utils.getFormatedTime(currentGroup.getCreated())); 
		
		edit_act_distance.setText(String.format("%.1f",currentGroup.getDistance()/1000));  
		//edit_act_distance.setText( Utils.getFormatedDistance(currentGroup.getDistance()) );  
		if(currentGroup.getSportTime()>0){
			float spd =currentGroup.getDistance()/(currentGroup.getSportTime()/1000) *3.6f;
			edit_grpspeed.setText(String.format("%.1f",  spd));
		}*/
	}
	 
	@Override
	void drawActNaviRoadbook(Track gpxTrack){
		return;
	}
	 
 
	 
	@Override
	void removeUser(int userId){ 
		doMemberOperation(currentGroup.getId(),currentGroup.getName(),currentGroup.getUserId(),userId, activity.app.user.getUserbean().getUserName(),RestMethods.GRP_P_DEL_MEMBER);
	}
	 
//	 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;
  
		 isMemberQuery =queryType==1;
		 isActivityQuery = queryType==2;
		 isShareQuery =queryType==3;
		 isLiveQuery =queryType==4;
//		 currentab = currentTab;
		 
	}
	
	void doWebQuery(){ //do roadbook query
		initialWebListView(R.id.tr_listWeb);
		  
		  if( trackData==null)
			 
				doWebTrackQuery();
			else
				showWebTrackListData(trackData);
			if(trackData!=null)
				showNoRecText(trackData.size()==0);
		  
	} 
 
	 /*
	   * 
	   * build query for activities  the group users in 
	   * @see com.qiyou.mb.android.ui.fragments.WebList_fragment#buildNearByQueryParames()
	   */
	@Override
	  RequestParams buildActQueryParames(){
			RequestParams params = this.buildBasicParames(pageNo, -1);  
			 
     		params.add("groupId", String.valueOf(currentGroup.getId()));
				
			//			 QLog.getLogger().d(dTAG, TAG+" web query params: "+params);
		 
			return params;
		}
	
/*	 RequestParams buildActNearByQueryParames(){
		 	if(svrNumber<=0){
		 		return null;
		 	}
			RequestParams params = new RequestParams();   
			params.add("page", String.valueOf(pageNo));
			params.add("pagesize", String.valueOf(pageSize));
			 //QiYouConstant.PV_LAST_LAT,QiYouConstant.PV_LAST_LON
//			params.add("lat",String.valueOf(QiYouConstant.PV_LAST_LAT));
//			params.add("lon",String.valueOf(QiYouConstant.PV_LAST_LON));
			params.add("memberId",String.valueOf(activity.app.user.getUserbean().getId())); 
			
			params.add("groupId",String.valueOf(svrNumber)); 
		 
			//			 QLog.getLogger().d(dTAG, TAG+" web query params: "+params);
		 
			return params;
		}
	 */

	@Override
	RequestParams buildShareQueryParames(){
 
		RequestParams params = 	super.buildShareQueryParames() ;
		
		
		if(currentGroup != null &&  currentGroup.getId()>0 ) {
			params.add("groupId", String.valueOf(currentGroup.getId()));
		}
 		   
	    QLog.getLogger().d(dTAG, TAG+" web query params: "+params);
	 
		return params;
	}
	
	@Override
	RequestParams buildMemberQueryParames(){
	    
		  int groupId=currentGroup==null?-1:currentGroup.getId();
 			
			if(groupId<=0 ) {
				return null;
			}
			
		
			
			RequestParams params = new RequestParams();  
			params.add("groupId", String.valueOf(groupId));
 //			String svrNo= edSvrNo.getText().toString(); 
		 
			params.add("page", String.valueOf(pageNo));
			params.add("pagesize", String.valueOf(pageSize));
			 //QiYouConstant.PV_LAST_LAT,QiYouConstant.PV_LAST_LON
		 
//			params.add("actid",String.valueOf(actId )); 
		 
			//			 QLog.getLogger().d(dTAG, TAG+" web query params: "+params);
		 
			return params;
		}
	
	@Override
	void showLive(){
		
		doLiveQuery();
	}
	 
	
	
	@Override
	RequestParams buildLiveQueryParames(){
		RequestParams params = new RequestParams(); 
//		final int GEO_CITY_TYPE=2;
	 
	    if(currentGroup==null) return null;
		params.add("groupId", String.valueOf(currentGroup.getId()));
		params.add("from", String.valueOf(lastMaxLiveTime)); 
		
		//			 QLog.getLogger().d(dTAG, TAG+" web query params: "+params);
	 
		return params;
	}
	 
	@Override
	 void showObjDetail(){
		 activity.showGroup(currentGroup);
	 }
//	 @Override
//	  void showGroup(Group_bean group) { 
//		 getDetails();  
//	  }
	 
	@Override 
	boolean validDate(){
		return true;
	}
	@Override	  
    void refresh(){
		  if(this.isShareQuery){
			  if(currentGroup==null ||currentGroup.getId()==0 )return  ;
			  	 
		 }else if(isLiveQuery){
			 showLive(); 
			 return  ;
		  } 
				 
	    
		 this.initialWebQuery();
		   
		
	}

	 
	 
 @Override
	public boolean isShowBottomTabs() {
		// TODO Auto-generated method stub
		return false;
	}
 
	

	@Override
	public void setTabOnFiling() {
		// TODO Auto-generated method stub
//		int size =currentGroup!=null && currentGroup.getId()>0?4:2;
//		 maxTabIndex =size;
//		 MAPVIEW_INDEX=currentGroup!=null && currentGroup.getId()>0?4:-1;
		 maxTabIndex =5;
		 MAPVIEW_INDEX=5;
		 
		 
	}

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