package com.android.main;
import java.util.Vector;

import android.os.Handler;
import android.util.Log;

public class DrawFFTThread extends Thread {
	private Handler mHandler;
	private Vector tempVector;
	private boolean DEBUG = true;
	private String TAG = "DrawPowerDensityThread";
	private Buffer buffer;
	private boolean STOPTAG;
	private int oldBufferIndex;//used to make sure that, the value is the latest one.
	private int tempOldBufferIndex;
	public DrawFFTThread(Handler handler, String name){
		mHandler = handler;
		//buffer = new Buffer();
		tempVector = new Vector(buffer.channels);
		STOPTAG = false;
		oldBufferIndex =0;
		TAG = name;
	}
	public void setStopTag(boolean tag){
		STOPTAG = tag;
	}
	public void run(){
		while(!STOPTAG){//for(int i=0;i<1000;i++){	
			tempVector.removeAllElements();
			if(((tempOldBufferIndex=buffer.getDataIndex())-oldBufferIndex) >1){
				//if(DEBUG)buffer.writeData(1, TAG+", old-temp>1");
				if(DEBUG)Log.e(TAG, tempOldBufferIndex-oldBufferIndex+"   >1");
				oldBufferIndex = tempOldBufferIndex;
			}
			else if(tempOldBufferIndex -oldBufferIndex ==1){
				if(DEBUG)Log.e(TAG, tempOldBufferIndex-oldBufferIndex+" ==1");
				oldBufferIndex = tempOldBufferIndex;
				tempVector = buffer.getData4();
				//if(DEBUG)buffer.writeData(1, TAG+", GOOD");
				if((tempVector.size()>1) && getMean())//check if size==4, and get the mean of Vector
					mHandler.obtainMessage(1, tempVector.clone()).sendToTarget();//fill in data
			}
			{
				try {
					//if(DEBUG)Log.e(TAG, tempOldBufferIndex-oldBufferIndex+"   ==0");
					sleep(10);
				} catch (InterruptedException e) {
						e.printStackTrace();
				}
			}
		}
	}
	public void run2(){
		while(!STOPTAG){//for(int i=0;i<1000;i++){			
			tempVector.removeAllElements();
			/*for(int i=0;i<1000;i++){
				mHandler.obtainMessage(1, i).sendToTarget();//fill in data
				try {
					sleep(500);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}*/
			
			tempVector = buffer.getData4();
			if(getMean())//check if size==4, and get the mean of Vector
				mHandler.obtainMessage(1, tempVector).sendToTarget();//fill in data
			else
				if(DEBUG)Log.e(TAG, "Size!=16");	
			if(DEBUG)Log.e(TAG, "set data to PowerDensityActivity");	
			try {
					sleep(1000);
			} catch (InterruptedException e) {
					e.printStackTrace();
			}
		}
	}
	public void run3(){
		double[]temp = new double[128];
		for(int j=0;j<128;j++){
			temp[j]	= 0.8*Math.sin(j * 2 *  Math.PI * 9 / 128)+0.2*Math.sin(j * 2 *  Math.PI * (Math.random()*50) / 128);
		}
		while(true){//for(int i=0;i<1000;i++){			
			tempVector.add(temp);
			tempVector.add(temp);
			tempVector.add(temp);
			tempVector.add(temp);
			mHandler.obtainMessage(1, tempVector).sendToTarget();//fill in data
			try {
				sleep(500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			tempVector.removeAllElements();
		}
	}
	private boolean getMean(){
		boolean tag = true;
		if(tempVector.size()!=buffer.channels){
			tag = false;
			return tag;
		}
		Channel [] element = new Channel[buffer.channels];
		int sum [] = new int [buffer.channels];
		for(int channel=0;channel<buffer.channels;channel++){
			element[channel] = new Channel(1);
			element[channel].oneSecData = (double[])tempVector.elementAt(channel);
		}
		for(int channel=0;channel<buffer.channels;channel++){
			for(int i=0;i<element[channel].oneSecData.length;i++){
				sum[channel]+=element[channel].oneSecData[i];
			}
			sum[channel] = sum[channel]/element[channel].oneSecData.length;
		}
		
		for(int channel=0;channel<buffer.channels;channel++){
			for(int i=0;i<element[channel].oneSecData.length;i++){
				element[channel].oneSecData[i]-=sum[channel];
			}
		}
		tempVector.clear();
		for(int channel=0;channel<buffer.channels;channel++){
			tempVector.add(element[channel].oneSecData);
		}
		return tag;
	}
	private boolean getMean2(){
		boolean tag = true;
		if(tempVector.size()!=4){
			tag = false;
			return tag;
		}
		double[]temp1 = (double[])tempVector.elementAt(0);
		double[]temp2 = (double[])tempVector.elementAt(1);
		double[]temp3 = (double[])tempVector.elementAt(2);
		double[]temp4 = (double[])tempVector.elementAt(3);
		int sum1 = 0;
		int sum2 = 0;
		int sum3 = 0;
		int sum4 = 0;
		for(int i=0;i<temp1.length;i++){
			sum1+=temp1[i];
			sum2+=temp2[i];
			sum3+=temp3[i];
			sum4+=temp4[i];
		}
		sum1 = sum1 / temp1.length;
		sum2 = sum2 / temp2.length;
		sum3 = sum3 / temp3.length;
		sum4 = sum4 / temp4.length;
		for(int i=0;i<temp1.length;i++){
			temp1[i]-=sum1;
			temp2[i]-=sum2;
			temp3[i]-=sum3;
			temp4[i]-=sum4;
		}
		tempVector.clear();
		tempVector.add(temp1);
		tempVector.add(temp2);
		tempVector.add(temp3);
		tempVector.add(temp4);
		return tag;
	}
}
