package com.uhf288.scanlable;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.Toast;

import com.rfid.trans.*;
public class IvtParamActivity extends Activity implements OnClickListener{

	private Spinner spType;
	private Spinner spMem;
	private Spinner spStart;
	private Spinner spLength;
	
	private Spinner spQ;
	private Spinner spS;
	private Spinner spTime;
	private Spinner spTarget;
	private Spinner spCount;
	
	EditText txtPsd;
	CheckBox Ant1;
	CheckBox Ant2;


	LinearLayout line1;
	
	Button btSet;
	Button btGet;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_ivt_param);
	    ////////////查询类型
		String[] strType = new String[3];
		strType[0]="EPC";
		strType[1]="TID";
		strType[2]= getString(R.string.info_mix);
		spType=(Spinner)findViewById(R.id.type_spinner);  
		ArrayAdapter<String> spada_Type = new ArrayAdapter<String>(IvtParamActivity.this,  
	             android.R.layout.simple_spinner_item, strType);  
		spada_Type.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  
		spType.setAdapter(spada_Type);  
		spType.setSelection(0,false); 
		
		
		 ////////////查询区域
		String[] strMem = new String[3];
		strMem[0]="EPC";
		strMem[1]="TID";
		strMem[2]="User";
		spMem=(Spinner)findViewById(R.id.mixmem_spinner);  
		ArrayAdapter<String> spada_Mem = new ArrayAdapter<String>(IvtParamActivity.this,  
	             android.R.layout.simple_spinner_item, strMem);  
		spada_Mem.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  
		spMem.setAdapter(spada_Mem);  
		spMem.setSelection(0,false); 
	
		////////////起始地址
		spStart = (Spinner)findViewById(R.id.start_spinner);
		ArrayAdapter<CharSequence> adapter1 =  ArrayAdapter.createFromResource(this, R.array.act_addr, android.R.layout.simple_spinner_item);
		adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spStart.setAdapter(adapter1); 
		spStart.setSelection(0, true);
		
		////////////读取长度
		spLength = (Spinner)findViewById(R.id.count_spinner);
		ArrayAdapter<CharSequence> adapter2 =  ArrayAdapter.createFromResource(this, R.array.act_len, android.R.layout.simple_spinner_item);
		adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spLength.setAdapter(adapter2); 
		spLength.setSelection(5, true);

		//////////Q值
		spQ = (Spinner)findViewById(R.id.qvalue_spinner);
		ArrayAdapter<CharSequence> adapter3 =  ArrayAdapter.createFromResource(this, R.array.men_q, android.R.layout.simple_spinner_item);
		adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spQ.setAdapter(adapter3); 
		spQ.setSelection(6, true);
		/////////Session值
		spS = (Spinner)findViewById(R.id.session_spinner);
		ArrayAdapter<CharSequence> adapter4 =  ArrayAdapter.createFromResource(this, R.array.men_s, android.R.layout.simple_spinner_item);
		adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spS.setAdapter(adapter4); 
		spS.setSelection(1, true);
		////////查询时间
		spTime = (Spinner)findViewById(R.id.scantime_spinner);
		ArrayAdapter<CharSequence> adapter5 =  ArrayAdapter.createFromResource(this, R.array.men_time, android.R.layout.simple_spinner_item);
		adapter5.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spTime.setAdapter(adapter5); 
		spTime.setSelection(10, true);
		
		 ////////////Target
		String[] strTar= new String[2];
		strTar[0]="A";
		strTar[1]="B";
		spTarget=(Spinner)findViewById(R.id.target_spinner);  
		ArrayAdapter<String> spada_tar = new ArrayAdapter<String>(IvtParamActivity.this,  
	             android.R.layout.simple_spinner_item, strTar);  
		spada_Mem.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  
		spTarget.setAdapter(spada_tar);  
		spTarget.setSelection(0,false); 
		
		////////////无标签次数切换A/B
		spCount = (Spinner)findViewById(R.id.change_spinner);
		ArrayAdapter<CharSequence> adapter6 =  ArrayAdapter.createFromResource(this, R.array.act_addr, android.R.layout.simple_spinner_item);
		adapter6.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spCount.setAdapter(adapter6); 
		spCount.setSelection(8, true);
		
		txtPsd = (EditText)findViewById(R.id.et_pwd);  
		
		Ant1 = (CheckBox)findViewById(R.id.scanAnt1);  
		Ant2 = (CheckBox)findViewById(R.id.scanAnt2);


		btSet = (Button)findViewById(R.id.btSetParam);
		btGet = (Button)findViewById(R.id.btGetParam);
		btSet.setOnClickListener(this);
		btGet.setOnClickListener(this);

		line1 = (LinearLayout) findViewById(R.id.scanGroup1);
		GetParam();
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		if(arg0 == btSet)
		{
			if((txtPsd.getText()==null)||(txtPsd.getText().toString().length()!=8))
			{
				Toast.makeText(getApplication(),getString(R.string.info_psw_length_error), Toast.LENGTH_SHORT).show();
                return;
			}
			ReaderParameter param = Reader.rrlib.GetInventoryParameter();
			param.IvtType     = spType.getSelectedItemPosition();
			param.Memory      = spMem.getSelectedItemPosition()+1;
			param.Password    = txtPsd.getText().toString();
			param.TidPtr      = spStart.getSelectedItemPosition();
			param.TidLen      = spLength.getSelectedItemPosition()+1;
			param.Session     = spS.getSelectedItemPosition();
			if(param.Session==4)param.Session=255;
			else if(param.Session==5)param.Session=254;
			else if(param.Session==6)param.Session=253;
			param.QValue      = spQ.getSelectedItemPosition();
			param.ScanTime    = spTime.getSelectedItemPosition()+1;
			param.Target      = spTarget.getSelectedItemPosition();
			param.reTryCount  = spCount.getSelectedItemPosition();
			
			param.Antenna     = 0;
			if(Ant1.isChecked())param.Antenna|=0x0001;
			if(Ant2.isChecked())param.Antenna|=0x0002;

			Reader.rrlib.SetInventoryParameter(param);
			Toast.makeText(getApplication(),getString(R.string.set_success), Toast.LENGTH_SHORT).show();

		}
		else if(arg0 == btGet)
		{
			GetParam();
		}
	}

	
	private void GetParam()
	{
		ReaderParameter param = Reader.rrlib.GetInventoryParameter();
		spType.setSelection(param.IvtType, false);
		spMem.setSelection(param.Memory-1, false);
		txtPsd.setText(param.Password);
		spStart.setSelection(param.TidPtr, false);
		spLength.setSelection(param.TidLen-1, false);
		if(param.Session==255)
		{
			spS.setSelection(4, false);
		}
		else if(param.Session==254)
		{
			spS.setSelection(5, false);
		}
		else if(param.Session==253)
		{
			spS.setSelection(6, false);
		}
		else
			spS.setSelection(param.Session, false);
		spQ.setSelection(param.QValue & 0x0F, false);
		
		spTime.setSelection(param.ScanTime-1, false);
		spTarget.setSelection(param.Target, false);
		spCount.setSelection(param.reTryCount,false);
		line1.setVisibility(View.VISIBLE);

		if((param.Antenna&0x0001)==0x0001)
			Ant1.setChecked(true);
		else
			Ant1.setChecked(false);
		
		if((param.Antenna&0x0002)==0x0002)
			Ant2.setChecked(true);
		else
			Ant2.setChecked(false);

		Toast.makeText(getApplication(),getString(R.string.get_success), Toast.LENGTH_SHORT).show();
	}

}
