package com.Agilent_34410A_Temp;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.CheckBox;

import com.agilent.android.io.AAIoException;
import com.agilent.android.io.IO;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.GraphView.GraphViewData;
import com.jjoe64.graphview.GraphViewSeries;
import com.jjoe64.graphview.LineGraphView;
import com.jjoe64.graphview.GraphViewSeries.GraphViewSeriesStyle;

public class Graf_Osciloskop extends Activity {
    private final Handler mHandler = new Handler();
    private Runnable mTimer1;
    private Runnable mTimer2;
    private GraphView graphView;
    private GraphViewSeries exampleSeries1;
    private GraphViewSeries exampleSeries2;
    private String channel_1="";
    private String channel_2="";
	private String prikaz ="";
	private String scale ="";
	private int size_array_1 = 0;
	private int size_array_2 = 0;
	private CheckBox Channel_1;
	private CheckBox Channel_2;
	boolean channel_1_aut = false;
	boolean channel_2_aut = false;
	
    private IO pripojeni = null;
    String wave_array_1[]= null;
    String wave_array_2[]= null;
    
    int i=0;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.graphs);
        
        Channel_1 = (CheckBox)findViewById(R.id.chkChannel_1);
        Channel_2 = (CheckBox)findViewById(R.id.chkChannel_2);
        
        Channel_1.setChecked(true);
         
        prikaz = ":run";
        channel_1 = Channel_1().toString();
        
        exampleSeries1 = new GraphViewSeries(new GraphViewData[] {
        		new GraphViewData(0, 0d)
                , new GraphViewData(50, 0d)
                , new GraphViewData(100, 0d)
        });
        exampleSeries2 = new GraphViewSeries(new GraphViewData[] {
        		new GraphViewData(0, 0d)
                , new GraphViewData(50, 0d)
                , new GraphViewData(100, 0d)
        });

            graphView = new LineGraphView(
                    this 
                    , "DSO-X 2012A Remote" 
            );
        graphView.addSeries(exampleSeries1);
        graphView.addSeries(exampleSeries2);
        
        
        LinearLayout layout = (LinearLayout) findViewById(R.id.graph1);
        layout.addView(graphView);
 
        ((Button) findViewById(R.id.btnGen)).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) { 
            	
            	Bundle bundle = getIntent().getExtras();
    			String ipAdresa = bundle.getString("ipAdresa");
            	
            	Intent intent = new Intent(Graf_Osciloskop.this,Gen_Osciloskop.class);
            	intent.putExtra("ipAdresa", ipAdresa);
            	startActivity(intent);
            }
        });
    }

    @Override
    protected void onPause() {
        mHandler.removeCallbacks(mTimer1);
        mHandler.removeCallbacks(mTimer2);
        super.onPause();
    }

    @Override
    protected void onResume() {                         
        super.onResume();
        mTimer1 = new Runnable() {
            @Override
            public void run() { 
            	
            	graphView.removeSeries(exampleSeries1);
            	graphView.removeSeries(exampleSeries2);
            	
            	if(Channel_1.isChecked()){
            	
            		channel_1 = Channel_1().toString();
                
            		int length_1 = channel_1.length();
            		String druhy_1 = channel_1.substring(24,length_1);
            		wave_array_1 = druhy_1.split(",");
            		size_array_1 = wave_array_1.length;
            	
            		GraphViewData[] datos_1= new GraphViewData[size_array_1];
            	
            		for (int i=0; i<size_array_1; i++){
            			datos_1[i] = new GraphViewData(i, Double.valueOf(wave_array_1[i]));
            		}
            	
            		exampleSeries1 = new GraphViewSeries("Sinus curve", new GraphViewSeriesStyle(Color.rgb(200, 50, 00), 3), datos_1);
            	          	                 
            		graphView.addSeries(exampleSeries1);
            	
            	}
  
            	if(Channel_2.isChecked()){
            		
            		channel_2 = Channel_2().toString();
            	
            		int length_2 = channel_2.length();
            		String druhy_2 = channel_2.substring(24,length_2);
            		wave_array_2 = druhy_2.split(",");
            		size_array_2 = wave_array_2.length;
            	
            		GraphViewData[] datos_2= new GraphViewData[size_array_2];
            	
            		for (int i=0; i<size_array_2; i++){
            			datos_2[i] = new GraphViewData(i, Double.valueOf(wave_array_2[i]));
            		}
            	
            		exampleSeries2 = new GraphViewSeries("Sinus curve", new GraphViewSeriesStyle(Color.rgb(90, 250, 00), 3), datos_2);
            	          	                 
            		graphView.addSeries(exampleSeries2);
            	
            	}
                mHandler.postDelayed(this, 1);
            }
        };
        mHandler.postDelayed(mTimer1, 1);
    }
    
    private String Channel_1()
	{
    	
		StringBuffer wave = new StringBuffer();
		try
		{	
			Bundle bundle = getIntent().getExtras();
	        String ipAdresa = bundle.getString("ipAdresa");
	        String points = bundle.getString("points");

			pripojeni = new IO();
			pripojeni.openWithAddress(ipAdresa, 5025);
			pripojeni.print(prikaz);
			pripojeni.print(":WAV:SOUR CHAN1");
			pripojeni.print(":WAV:FORM ASC");
			pripojeni.print(":WAV:POIN " + points);
			pripojeni.print(":WAV:DATA?");
			wave = new StringBuffer();
			pripojeni.scan(wave);
			pripojeni.print(scale);
		}
		catch(AAIoException ex)
		{
			return wave.toString();
		}
		catch (Exception ex)
		{
			return wave.toString();
		}
		finally 
		{ 
			if (pripojeni != null)
				pripojeni.close();
		}
		return wave.toString();
	}
    
    private String Channel_2()
	{
    	
		StringBuffer wave = new StringBuffer();
		try
		{	
			Bundle bundle = getIntent().getExtras();
	        String ipAdresa = bundle.getString("ipAdresa");
	        String points = bundle.getString("points");

			pripojeni = new IO();
			pripojeni.openWithAddress(ipAdresa, 5025);
			pripojeni.print(prikaz);
			pripojeni.print(":WAV:SOUR CHAN2");
			pripojeni.print(":WAV:FORM ASC");
			pripojeni.print(":WAV:POIN " + points);
			pripojeni.print(":WAV:DATA?");
			wave = new StringBuffer();
			pripojeni.scan(wave);
		}
		catch(AAIoException ex)
		{
			return wave.toString();
		}
		catch (Exception ex)
		{
			return wave.toString();
		}
		finally 
		{ 
			if (pripojeni != null)
				pripojeni.close();
		}
		return wave.toString();
	}
}