package com.example.client;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class EvaluateActivity extends Activity implements OnClickListener {
    
    private Timer timer1s;  
    private int timeCount = 300;
    
    private static TextView mTimeTextView;
    private static TextView mTimeMention;
    
    private static TextView mXVTextView;
    private static TextView mSDNNTextView;
    private static TextView mHRVTextView;
    private static TextView mPNN50TextView;
    private static TextView mQZTextView;
    private static TextView mVLFTextView;
    private static TextView mLFTextView;
    private static TextView mHFTextView;
    
    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    };
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.evaluate);
        
        Button back = (Button)findViewById(R.id.back_btn);
        back.setOnClickListener(this);
        
        mTimeTextView = (TextView) findViewById(R.id.labelTime);
        mTimeMention = (TextView) findViewById(R.id.timeMention);
        
        mXVTextView = (TextView) findViewById(R.id.labelXv);
        mSDNNTextView = (TextView) findViewById(R.id.labelSDNN);
        mHRVTextView = (TextView) findViewById(R.id.labelHRV);
        mPNN50TextView = (TextView) findViewById(R.id.labelPNN50);
        mQZTextView = (TextView) findViewById(R.id.labelQZ);
        mVLFTextView = (TextView) findViewById(R.id.labelVLF);       
        mLFTextView = (TextView) findViewById(R.id.labelLF);
        mHFTextView = (TextView) findViewById(R.id.labelHF);  
        
        // 定时器: 每隔1s更新一次曲线
        timer1s = new Timer();
        timer1s.schedule(new TimerTask(){
            @Override
            public void run() {         
                mHandler.post(mRunnable1s);
            }
        }, 1000, 1000);
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int id = v.getId();
		
		 if(id == R.id.back_btn){ 
			 this.finish();
		 }
	}
	
	
    private Runnable mRunnable1s = new Runnable() {
        @Override
        public void run() {
            mHandler.sendEmptyMessage(1);
            
            if (timeCount >= 0) {
            	mTimeMention.setText("距离评估结束还有" + Integer.toString(timeCount) + "秒.");
                mTimeTextView.setText(Integer.toString(timeCount--) + "s");
                
                EvaluateActivity.this.updateIndexs();
                              
                if (timeCount < 0) {
                    // 测评结束
                    new AlertDialog.Builder(EvaluateActivity.this)   
                    .setTitle("确认")  
                    .setMessage("恭喜您完成测评!")  
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface arg0, int arg1) {
                        	EvaluateActivity.this.finish();
                        }
                    })  
                    .show(); 
                }
            } 
        }
    };
    
    
    private void updateIndexs()
    {
        int randHrv = (int)(Math.random() * 40);
        int randXv = 40 + (int)(Math.random() * 30);
        int randQz = 70 + (int)(Math.random() * 30);   
        int randSdnn = (int)(Math.random() * 100);      
        int randPnn50 = (int)(Math.random() * 100);      
        int randHF = (int)(Math.random() * 100);
        int randLF = (int)(Math.random() * 100);
        int randVLF = (int)(Math.random() * 100);
        
        mXVTextView.setText("心率:" + String.valueOf(randXv));
        mSDNNTextView.setText("SDNN:" + String.valueOf(randSdnn));
        mHRVTextView.setText("HRV:" + String.valueOf(randHrv));
        mPNN50TextView.setText("PNN50:" + String.valueOf(randPnn50));
        mQZTextView.setText("情指:" + String.valueOf(randQz));
        mVLFTextView.setText("VLF:" + String.valueOf(randVLF));   
        mLFTextView.setText("LF:" + String.valueOf(randLF));
        mHFTextView.setText("HF:" + String.valueOf(randHF)); 
        
        RelaxData.getInstance().setXV( randXv );
        RelaxData.getInstance().setQZ( randQz );
        RelaxData.getInstance().setSDNN( randSdnn );
        RelaxData.getInstance().setHRV( randHrv ) ;
        RelaxData.getInstance().setPNN50( randPnn50 );      
        RelaxData.getInstance().setHF( String.valueOf(randHF) );
        RelaxData.getInstance().setLF( String.valueOf(randLF) ) ;
        RelaxData.getInstance().setVLF( String.valueOf(randVLF) );
    }
}
