package com.android.main;

import java.util.Vector;

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

public class DrawTimeDomainThread extends Thread {
	private Handler mHandler;
	private Vector tempVector;
	private boolean DEBUG = true;
	private String TAG = "DrawTimeDomainThread";
	private Buffer buffer;
	private boolean STOPTAG;
	private int oldBufferIndex;//used to make sure that, the value is the latest one.
	private int tempOldBufferIndex;
	public DrawTimeDomainThread(Handler handler){
		mHandler = handler;
		tempVector = new Vector(16);
		//buffer = new Buffer();
		STOPTAG = false;
		oldBufferIndex = 0;
	}
	public void setStopTag(boolean tag){
		STOPTAG = tag;
	}
	public void run(){
		/*double[]temp = new double[128];
		for(int j=0;j<128;j++){
			temp[j]	= j;
		}*/
		while(!STOPTAG){//for(int i=0;i<1000;i++){			
			if(((tempOldBufferIndex=buffer.getDataIndex())-oldBufferIndex) >1){
				//if(DEBUG)buffer.writeData(tempOldBufferIndex-oldBufferIndex, "     >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((double[])tempVector.elementAt(0));
				if(getMean())//check if size==4, and get the mean of Vector
					mHandler.obtainMessage(1, tempVector.clone()).sendToTarget();//fill in data
			}
			else{
				try {
					//if(DEBUG)Log.e(TAG, tempOldBufferIndex-oldBufferIndex+"   ==0");
					sleep(10);
				} catch (InterruptedException e) {
						e.printStackTrace();
				}
			}
		}
	}
	private boolean getMean(){
		boolean tag = true;
		if(tempVector.size()!=16){
			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);
		double[]temp5 = (double[])tempVector.elementAt(4);
		double[]temp6 = (double[])tempVector.elementAt(5);
		double[]temp7 = (double[])tempVector.elementAt(6);
		double[]temp8 = (double[])tempVector.elementAt(7);
		double[]temp9 = (double[])tempVector.elementAt(8);
		double[]temp10 = (double[])tempVector.elementAt(9);
		double[]temp11 = (double[])tempVector.elementAt(10);
		double[]temp12 = (double[])tempVector.elementAt(11);
		double[]temp13 = (double[])tempVector.elementAt(12);
		double[]temp14 = (double[])tempVector.elementAt(13);
		double[]temp15 = (double[])tempVector.elementAt(14);
		double[]temp16 = (double[])tempVector.elementAt(15);
		int sum1 = 0;
		int sum2 = 0;
		int sum3 = 0;
		int sum4 = 0;
		int sum5 = 0;
		int sum6 = 0;
		int sum7 = 0;
		int sum8 = 0;
		int sum9 = 0;
		int sum10 = 0;
		int sum11 = 0;
		int sum12 = 0;
		int sum13 = 0;
		int sum14 = 0;
		int sum15 = 0;
		int sum16 = 0;
		for(int i=0;i<temp1.length;i++){
			sum1+=temp1[i];
			sum2+=temp2[i];
			sum3+=temp3[i];
			sum4+=temp4[i];
			sum5+=temp5[i];
			sum6+=temp6[i];
			sum7+=temp7[i];
			sum8+=temp8[i];
			sum9+=temp9[i];
			sum10+=temp10[i];
			sum11+=temp11[i];
			sum12+=temp12[i];
			sum13+=temp13[i];
			sum14+=temp14[i];
			sum15+=temp15[i];
			sum16+=temp16[i];
		}
		sum1 = sum1 / temp1.length;
		sum2 = sum2 / temp2.length;
		sum3 = sum3 / temp3.length;
		sum4 = sum4 / temp4.length;
		sum5 = sum5 / temp5.length;
		sum6 = sum6 / temp6.length;
		sum7 = sum7 / temp7.length;
		sum8 = sum8 / temp8.length;
		sum9 = sum9 / temp9.length;
		sum10 = sum10 / temp10.length;
		sum11 = sum11 / temp11.length;
		sum12 = sum12 / temp12.length;
		sum13 = sum13 / temp13.length;
		sum14 = sum14 / temp14.length;
		sum15 = sum15 / temp15.length;
		sum16 = sum16 / temp16.length;
		for(int i=0;i<temp1.length;i++){
			temp1[i]-=sum1;
			temp2[i]-=sum2;
			temp3[i]-=sum3;
			temp4[i]-=sum4;
			temp5[i]-=sum5;
			temp6[i]-=sum6;
			temp7[i]-=sum7;
			temp8[i]-=sum8;
			temp9[i]-=sum9;
			temp10[i]-=sum10;
			temp11[i]-=sum11;
			temp12[i]-=sum12;
			temp13[i]-=sum13;
			temp14[i]-=sum14;
			temp15[i]-=sum15;
			temp16[i]-=sum16;
		}
		tempVector.clear();
		tempVector.add(temp1);
		tempVector.add(temp2);
		tempVector.add(temp3);
		tempVector.add(temp4);
		tempVector.add(temp5);
		tempVector.add(temp6);
		tempVector.add(temp7);
		tempVector.add(temp8);
		tempVector.add(temp9);
		tempVector.add(temp10);
		tempVector.add(temp11);
		tempVector.add(temp12);
		tempVector.add(temp13);
		tempVector.add(temp14);
		tempVector.add(temp15);
		tempVector.add(temp16);
		return tag;
	}
}
