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

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.GridLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TextView;

import com.qiyou.mb.android.QiYouConstant;
import com.qiyou.mb.android.R;
import com.qiyou.mb.android.beans.basic.Media;
import com.qiyou.mb.android.beans.basic.Waypoint_bean;
import com.qiyou.mb.android.utils.base.IlocationFragment;
import com.qiyou.mb.android.utils.enums.TrackStatus;
import com.qiyou.mb.android.utils.enums.WorkingTrackType;
import com.qiyou.mb.android.utils.enums.mediaType;
import com.qiyou.mb.android.utils.image.QimageView;
import com.qiyou.mb.android.utils.log.QLog;

public class Track_fragment extends  TrackTabs_fragment   implements IlocationFragment {
//	  public static final String TAG ="com.qiyou.track_fragment";
//	  public static final String dTAG ="com.qiyou";  
	@Override
	public void setFTag(){
		TAG="com.qiyou.track_fragment";
	}
	public static String getFTag(){
		return "com.qiyou.track_fragment";
	}
	@Override
	public String getCurrentTag(){
		
//		   QLog.getLogger().e(dTAG,TAG+" Track_fragment, app.currentActiveFragment: "+activity.app.currentActiveFragment);
		activity.updateActionBarTitle(R.string.frg_tracking);	 
		return "com.qiyou.track_fragment";
		
		
	}
	
	public Track_fragment() {
		// TODO Auto-generated constructor stub
		workingTrackType= WorkingTrackType.RECORDING;
	}
	 
	
	 
	 
	
	public static Track_fragment newInstance(int trackId)
	{
		
		Track_fragment f = new Track_fragment();
		Bundle args = new Bundle(); 
	    args.putInt(QiYouConstant.EXTRA_KEY_ID, trackId);
	    f.setArguments(args);
	    return f;
	}
	 
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);		
		isShowEdit=false;
		if(activity==null){
			    activity = (com.qiyou.mb.android.ui.MainActivity)getActivity();
	    }
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) { 
		vContainer = inflater.inflate(R.layout.track_fragment, container, false);
		super. onCreateView( inflater,  container, savedInstanceState);
		mTrack =activity.app.getRecordingTrack();
//		  vContainer = inflater.inflate(R.layout.track_fragment, container, false);
//		  initialViews(); 
//		  initialTabhost();
//		  initialFootmark(); 
		
		initialNaviControlles();
		 if(!isPlayback)
			 chk_showChats.setChecked(true);
		return vContainer;
		}
	
	 @Override
	  public void onActivityCreated(Bundle savedInstanceState) {
	    super.onActivityCreated(savedInstanceState);
//	    if(!isPlayback)showFullMap();
//	    showData(true);
	 }
	 
	 
		@Override
		protected void initialViews(){
			super.initialViews(); 
			
			 td_btnShowChartKm1 = (Button) vContainer.findViewById(R.id.td_btnShowChartKm1);  
			 if(td_btnShowChartKm1!=null)
				 td_btnShowChartKm1.setOnClickListener(ButtonOnClickListener);
			 td_btnShowChartSpeed1 = (Button) vContainer.findViewById(R.id.td_btnShowChartSpeed1);  
			 if(td_btnShowChartSpeed1!=null)
				 td_btnShowChartSpeed1.setOnClickListener(ButtonOnClickListener);
			 td_btnShowChartEle1 = (Button) vContainer.findViewById(R.id.td_btnShowChartEle1);  
			 if(td_btnShowChartEle1!=null)
				 td_btnShowChartEle1.setOnClickListener(ButtonOnClickListener);  
			 
			  btnMapLive.setVisibility(View.VISIBLE);
			  btnLiveList.setVisibility(View.VISIBLE);
			  btnMapLoadRoadbook.setVisibility(View.VISIBLE);
			 
		}
		
		
	 @Override  
		protected void showMapViews(){
			 
 		    super.showMapViews();
//			LinearLayout tm_ll_top_txt =(LinearLayout)vContainer.findViewById(R.id.tm_ll_top_txt); 
//			tm_ll_top_txt.setVisibility( View.VISIBLE );
		    GridLayout grid_top_txt =(GridLayout)vContainer.findViewById(R.id.grid_top_txt);
			grid_top_txt.setVisibility( View.VISIBLE );
 			if(tabhost!=null && tabhost.getCurrentTabTag().equalsIgnoreCase(tabData)){
				LinearLayout trackDetail =(LinearLayout)vContainer.findViewById(R.id.ll_recording_data);
	//			tm_ll_top_txt.setVisibility(ll.getVisibility()==View.GONE?View.VISIBLE:View.GONE);
				trackDetail.setVisibility( View.VISIBLE );
 			}
 		
		    if(btnLocation!=null)btnLocation.setVisibility(View.VISIBLE);
		    ((TextView)this.vContainer.findViewById(R.id.tm_top_speed_title)).setText("时速");
//			takeMapScreenshot();
			 vContainer.findViewById(R.id.ll_right_btns).setVisibility(View.VISIBLE);
			 
		
	    	 this.btnMapLive.setImageResource(QiYouConstant.PV_BL_SHARE_LOCATION?
	    			 							R.drawable.icon_radio:R.drawable.icon_radio_in);
			
	    	  // 开启定位图层
	         mBaiduMap.setMyLocationEnabled(true);
		} 
	  
	
	 @Override
	protected void initialTabhost(){
		 tabData="运动";
		 tabhost = (TabHost)vContainer.findViewById(R.id.tabhost);
		 tabhost.setup();  
	        TabHost.TabSpec dataTab = tabhost.newTabSpec(tabData); // This param will  be used as tabId. 
	        dataTab.setIndicator(tabData);
	        dataTab.setContent(R.id.ll_recording_data);
	        tabhost.addTab(dataTab);
	        super.initialTabhost();  
	        initialTabhostStyle(tabhost);
		}
  
	 @Override
	 protected void setWorkingTrack(){
		   mTrack =activity.app.getRecordingTrack();
		   mTrack_bean = mTrack.getTrackBean();
	   }
	 @Override
	  protected void updateMapMarkers(){
	/*	 if( activity.app.lastLocation==null || System.currentTimeMillis()-activity.app.lastLocation.getTime() > QiYouConstant.MAX_GPS_OFF_TIME*5){
			 mMapController.setRotation(360);
             mMapView.refresh();
		 }*/
 	
	    }
	   
	 
	 protected void initialNaviControlles(){
		 btnPlaySpeech = (ImageButton) vContainer.findViewById(R.id.navi_btnSpeech);		  
		 btnPlaySpeech.setOnClickListener(ButtonOnClickListener);
		 btnNaviStop = (ImageButton) vContainer.findViewById(R.id.navi_btnStop);
		 btnNaviStop.setOnClickListener(ButtonOnClickListener);
		 btnNaviPause = (ImageButton) vContainer.findViewById(R.id.navi_btnPause);
		 btnNaviPause.setOnClickListener(ButtonOnClickListener); 
 
//		 btnPlaySpeech.setImageResource(QiYouConstant.CAN_SPEAK&&QiYouConstant.PV_INT_NAVI_SPEAK_MODEL!=2?R.drawable.icon_voice:R.drawable.icon_novoice);
		 
	 }
	 
	 @Override
	    void stopNavi(){
		 
		 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
         builder.setTitle("结束导航？").setIcon(android.R.drawable.ic_dialog_info) 
                 .setNegativeButton("取消", null);
         builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

             public void onClick(DialogInterface dialog, int which) { 
 
				 activity.app.isNaving=false;
				    ll_navi_progress.setVisibility(View.GONE);
			    	activity.stopNaviService();
				   
             }
         });
         builder.show();
         
		 
		 
		 
		 	
	    }
	 @Override
	    void startPauseNavi(){
		 activity.app.isNavPaused =! activity.app.isNavPaused;
			
	    	if ( activity.app.isNavPaused) {				
	    		txt_nav_info.setText("导航暂停");
	    		btnNaviPause.setImageResource(R.drawable.icon_plresume); 
				  
			} else {		 
				btnNaviPause.setImageResource(R.drawable.icon_plpause);
				txt_nav_info.setText("导航继续");
				activity.app.speech("导航继续",1);
			}  
	    	
	    	 
		    }
		    
	  
	 
	 
	 
	 @Override
		public void onResume() {
			super.onResume();	
			perfomRotate(361);
//			activity.updateTabVisibility(false);
//			showFullMap();
//		   	activity.app.currentActiveFragment =getFTag();  
//		   	mTrack_bean = activity.app.getRecordingTrack().getTrackBean();
//		    setWorkingTrack();
//		    mTrack =activity.app.getRecordingTrack();
//		    mMapView.onResume();
//	 		  drawTrack= true;
//	 		if(!isDrawingTrack)  isDrawingTrack =false;
	 		
//		    if(canLocationMap)updateUI(activity.app.lastLocation, false);  	
//			updateMapMarkers() ;
		}
   @Override
	public boolean isShowBottomTabs() {
		// TODO Auto-generated method stub
		return false;
	}
	 
	 @Override
		public void onPause() {
		 
			super.onPause();
		}
	 @Override
		public void onDestroy() {  
			super.onDestroy();
		}
	@Override
	public void setTabOnFiling() {
		 MAPVIEW_INDEX=2;
		 maxTabIndex = isShowEdit?3:2;
		
	} 
	@Override
	protected void setSelectedTab(){
		super.setSelectedTab();
		if( mTrack_bean.getStatus()==TrackStatus.RECORDING &&currentTab==MAPVIEW_INDEX)
			 redrawTrack();
	}
}
