package nctu.brc.mindo_con;

import nctu.brc.mindo_con.bluetooth.ControlThread;
import nctu.brc.mindo_con.constant.Constant;
import nctu.brc.mindo_connect.R;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.widget.Toast;

public class Mindo_connect extends Activity {
    public static final int Channel_Data = 1;
    public static final int Data_Lost = 2;
	private BluetoothAdapter btAdapter;
    private BluetoothDevice btDevice; 
    private ControlThread btProcess;
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_mindo_connect);
		connectBT();
	}

	 @Override
	    public boolean onCreateOptionsMenu(Menu menu) {    	
	    	menu.addSubMenu(1, 1, 0, "Filter on");
	    	menu.addSubMenu(2, 2, 0, "Filter off");
	    	SubMenu Menu_1 = menu.addSubMenu(3, 4, 0, "Gain");
	    		Menu_1.add(3, 5, 0, "1");    
	    		Menu_1.add(3, 6, 0, "2");
	    		Menu_1.add(3, 7, 0, "3");
	    		Menu_1.add(3, 8, 0, "4");
	    		Menu_1.add(3, 9, 0, "6");
	    		Menu_1.add(3, 10, 0, "8");
	    		Menu_1.add(3, 11, 0, "12");
	    	
	        return super.onCreateOptionsMenu(menu); 
	    } 
	  
	    @Override
	    public boolean onOptionsItemSelected(MenuItem item) { 
	         
	        switch(item.getItemId()) {          
	            case 1: 
	          	
	            	ControlThread.filter_selected = 1;
	                
	                break; 
	            case 2: 
	          	
	            	ControlThread.filter_selected = 2;
	            	
	                break; 
	                
	            case 5: 
	              	
	            	ControlThread.changeGain_selected = 1;
	            	ControlThread.changeGain_num = 1;
	            	
	            break;
	            
	            case 6: 
	              	
	            	ControlThread.changeGain_selected = 1;
	            	ControlThread.changeGain_num = 2;
	            	
	            break;
	            
	            case 7: 
	              	
	            	ControlThread.changeGain_selected = 1;
	            	ControlThread.changeGain_num = 3;
	            	
	            break;
	            
	            case 8: 
	  	
	            	ControlThread.changeGain_selected = 1;
	            	ControlThread.changeGain_num = 4;
		
	            break;

	            case 9: 
	  	
	            	ControlThread.changeGain_selected = 1;
	            	ControlThread.changeGain_num = 6;
		
	            break;

	            case 10: 
	  	
	            	ControlThread.changeGain_selected = 1;
	            	ControlThread.changeGain_num = 8;
		
	            break;

	            case 11: 
	  	
	            	ControlThread.changeGain_selected = 1;
	            	ControlThread.changeGain_num = 12;
		
	            	break;


	            default:
	           
	        } 
	       
	       return super.onOptionsItemSelected(item); 
	    }
	
	  private boolean enableBT()
	  {
	   
	        btAdapter = BluetoothAdapter.getDefaultAdapter();
	       
	        if (btAdapter == null){
	        	Toast.makeText(this, "本機子沒有提供藍芽裝置", Toast.LENGTH_SHORT).show();
	            return false;
	        }
	       
	        if (!btAdapter.isEnabled()){
	        	Toast.makeText(this, "本機的藍芽裝置沒有開啟", Toast.LENGTH_SHORT).show();
	        	return false;       	
	        }
	 	       
	         BluetoothDevice tmpBTDevice = null;
	         tmpBTDevice  = btAdapter.getRemoteDevice("00:1A:FF:09:01:11");
	         
	         
	         if(tmpBTDevice == null){
	        	 Toast.makeText(this, "尚未與Mindo配對", Toast.LENGTH_SHORT).show();
	          		return false;
	          	}else{
	          		btDevice = tmpBTDevice;
	          	}  
	         
	        return true;        
	    }
	  
	   @Override
		public boolean onKeyDown(int keyCode, KeyEvent event) {
	    	if (keyCode == KeyEvent.KEYCODE_BACK) {
	            if(btProcess!=null && btProcess.btState == ControlThread.CONNECTED){
	            	btProcess.onExit();
	            }          	
		    		btProcess = null;
		        	btDevice = null;
		        	btAdapter = null;
		      
		            Mindo_connect.this.finish();     
		    		
	    		return true;
	    	}
	    	return false;
	    }
	  
	   public void connectBT()
	   {
	    	
	    	if(!enableBT())   		
	    		return ; 	
	       
	        btAdapter.cancelDiscovery();	    		      
	       
			btProcess = new ControlThread(mHandler, btDevice,Constant.getInstance().getResolution(),Constant.getInstance().getChannel(),Constant.getInstance().getSampleRate(),Constant.getInstance().getGain());				    
			if(btProcess.btState == ControlThread.CONNECTED){    		
	    		new Thread(btProcess).start();       		  		    
	    	}else{
	    		btProcess.onExit();
	    		btProcess = null;
	        	btDevice = null;
	        	btAdapter = null;     	
	    	}
	   }
	   
	   private  Handler mHandler = new Handler() {
	        @Override
	        public void handleMessage(Message msg) {
	            switch (msg.what) {	            
	            case Channel_Data: 	    
	            	Log.v("Mindo data", "Channel:"+msg.arg2+" data:"+(Float)msg.obj);
					//drawPanel.addData((Float)msg.obj,msg.arg2); 左邊為範例，其中每接收到一次msg.obj，代表的是一個channel的data。(msg.obj是從ControlThread.java中第92傳遞過來的)				 						 																  						 										      
	            break;	
	            
	            case Data_Lost:
	            	Log.v("Data Lost", "DataLost:"+msg.arg1);
	            break;
	           }	                        
	        }        
	    };	
}
