package com.Agilent_34410A_Temp;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.Agilent_34410A_Temp.R;
import com.agilent.android.io.AAIoException;
import com.agilent.android.io.IO;

public class Gen_Osciloskop extends Activity {

	private TextView txtPrubeh;
	private IO pripojeni = null;
	private EditText etxtFrek;
	private EditText etxtAmp;
	private EditText etxtPos;
	private EditText etxtUro;
	private EditText etxtSou;
	private EditText etxtSir;
	boolean not34410A = false;
	private Button btnNastavit;
	
	private String ipAdresa ="";
	private String idn ="";
	private String prubeh ="SIN";
	private String frekvence ="";
	private String amplituda ="";
	private String posun ="";
	private String uroven ="";
	private String soumernost ="";
	private String sirka ="";
	private String prikaz ="";

	
    /** Called when the activity is first created. */
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.gen);
		
        Bundle bundle = getIntent().getExtras();
		String ipAdresa = bundle.getString("ipAdresa");
		
		btnNastavit = (Button) findViewById(R.id.btnNastavit);
        
        etxtFrek = (EditText)findViewById(R.id.etxtFrek);
        etxtAmp = (EditText)findViewById(R.id.etxtAmp);
        etxtPos = (EditText)findViewById(R.id.etxtPos);
        etxtUro = (EditText)findViewById(R.id.etxtUro);
        etxtSou = (EditText)findViewById(R.id.etxtSou);
        etxtSir = (EditText)findViewById(R.id.etxtSir);
        
        if (prubeh == "SIN"){
				etxtUro.setEnabled(false);
				etxtSou.setEnabled(false);
				etxtSir.setEnabled(false);
			}
        
		txtPrubeh = (TextView)findViewById(R.id.txtPrubeh);
		txtPrubeh.setText("Průběh: Sinusový");
		txtPrubeh.setOnClickListener(new View.OnClickListener() 
		{

			@Override
			public void onClick(View v) {
				final String[] prubeh_array = {"Sinusový","Čtvercový","Rampa","Impuls","DC","Šum"};
				AlertDialog.Builder builder = new AlertDialog.Builder(Gen_Osciloskop.this);
				builder.setTitle("Průběh");
		        builder.setItems(prubeh_array, new DialogInterface.OnClickListener() {
		           			public void onClick(DialogInterface dialog, int which) {
		           				
		           				txtPrubeh.setText("Průběh: " + prubeh_array[which]);
		           				txtPrubeh.setVisibility(1);
		           				String[] prikaz_prubeh_array = {"SIN","SQU","RAMP","PULS","DC","NOIS"};
		           				prubeh = prikaz_prubeh_array[which];
		           				
		           				etxtFrek.setEnabled(true);
	           					etxtAmp.setEnabled(true);
	           					etxtPos.setEnabled(true);
	           					etxtUro.setEnabled(true);
	           					etxtSou.setEnabled(true);
	           					etxtSir.setEnabled(true);
		           				
		           				if (prubeh == "SIN"){
		           					etxtUro.setEnabled(false);
		           					etxtSou.setEnabled(false);
		           					etxtSir.setEnabled(false);
		           				}
		           				
		           				if (prubeh == "SQU"){
		           					etxtSou.setEnabled(false);
		           					etxtSir.setEnabled(false);
          					
		           				}
		           				
		           				if (prubeh == "RAMP"){
		           					etxtUro.setEnabled(false);
		           					etxtSir.setEnabled(false);
           					
		           				}
		           				
		           				if (prubeh == "PULS"){
		           					etxtUro.setEnabled(false);
		           					etxtSou.setEnabled(false);
	           					
		           				}
		           				
		           				if (prubeh == "DC"){
		           					etxtFrek.setEnabled(false);
		           					etxtAmp.setEnabled(false);
		           					etxtUro.setEnabled(false);
		           					etxtSou.setEnabled(false);
		           					etxtSir.setEnabled(false);
		           				}
		           				
		           				if (prubeh == "NOIS"){
		           					etxtFrek.setEnabled(false);
		           					etxtUro.setEnabled(false);
		           					etxtSou.setEnabled(false);
		           					etxtSir.setEnabled(false);
		           					
		           				}
		           			}
		           		}).show();
		        
			}
			
			
		});	
		
		
		btnNastavit.setOnClickListener(new View.OnClickListener() 
		{
			@Override
			public void onClick(View v) 
			{ 
				if (prubeh == "SIN"){
   					
   					frekvence = etxtFrek.getText().toString();
   					amplituda = etxtAmp.getText().toString();
   					posun = etxtPos.getText().toString();
   					
   					uroven = "";
   					soumernost = "";
   					sirka = "";
   				}
   				
   				if (prubeh == "SQU"){
		
   					frekvence = etxtFrek.getText().toString();
   					amplituda = etxtAmp.getText().toString();
   					posun = etxtPos.getText().toString();
   					uroven = etxtUro.getText().toString();
   					
   					soumernost = "";
   					sirka = "";
   					
   				}
   				
   				if (prubeh == "RAMP"){
 			
   					frekvence = etxtFrek.getText().toString();
   					amplituda = etxtAmp.getText().toString();
   					posun = etxtPos.getText().toString();
   					soumernost = etxtSou.getText().toString();
   					
   					uroven = "";
   					sirka = "";
   					
   				}
   				
   				if (prubeh == "PULS"){
  		
   					frekvence = etxtFrek.getText().toString();
   					amplituda = etxtAmp.getText().toString();
   					posun = etxtPos.getText().toString();
   					sirka = etxtSir.getText().toString();
   					
   					uroven = "";
   					soumernost = "";
   					
   				}
   				
   				if (prubeh == "DC"){
  	
   					posun = etxtPos.getText().toString();
   					
   					frekvence = "";
   					amplituda = "";
   					uroven = "";
   					soumernost = "";
   					sirka = "";
   				}
   				
   				if (prubeh == "NOIS"){
		
   					amplituda = etxtAmp.getText().toString();
   					posun = etxtPos.getText().toString();
   					
   					frekvence = "";
   					uroven = "";
   					soumernost = "";
   					sirka = "";
   					
   				}
				
				prikaz = "*IDN?";
				idn = Nastav_Gen().toString();
			}
        });
		
    }
    

	private String Nastav_Gen()
	{
		StringBuffer data = new StringBuffer();
		try
		{
			
			Bundle bundle = getIntent().getExtras();
		    ipAdresa = bundle.getString("ipAdresa");
			
		    pripojeni = new IO();
			pripojeni.openWithAddress(ipAdresa, 5025);
			pripojeni.print(":WGEN:FUNC " + prubeh);
			pripojeni.print(":WGEN:FREQ " + frekvence);
			pripojeni.print(":WGEN:VOLT " + amplituda);
			pripojeni.print(":WGEN:VOLT:OFFS " + posun);
			pripojeni.print(":WGEN:FUNC:SQU:DCYC " + uroven);
			pripojeni.print(":WGEN:FUNC:RAMP:SYMM " + soumernost);
			pripojeni.print(":WGEN:FUNC:PULS:WIDT " + sirka);
			pripojeni.print(":AUT");
			
			pripojeni.print(prikaz);																
			data = new StringBuffer();															
			pripojeni.scan(data);

			
		}
		catch(AAIoException ex)
		{
			return data.toString();
		}
		catch (Exception ex)
		{
			return data.toString();
		}
		finally 
		{ 
			if (pripojeni != null)
				pripojeni.close();
		}
		
		return data.toString();
	}
		
}