package com.activity.chaoran;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import com.chaoran.db.SqlHelpUtil;
import com.activity.chaoran.R;
import com.sys.SysData;
import com.util.DialogUtil;
import com.util.SysUtil;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

public class PdaRegActivity extends Activity {
	private EditText ip;
	private EditText port;
	private EditText itemName;
	private SqlHelpUtil db;
	private SQLiteDatabase sDatabase;
	private Handler handler = new Handler() {
		@Override
		// 当有消息发送出来的时候就执行Handler的这个方法
		public void handleMessage(Message msg) {
			if (msg.what == -1) {
				DialogUtil.builder(PdaRegActivity.this, "错误信息", "连接服务器失败,请检查网络！"
						+ msg.obj,0);
			} else if (msg.what == -2) {
				DialogUtil.builder(PdaRegActivity.this, "错误信息", "返回值为空！",0);
			} else{
				String retunVal=msg.obj.toString();
				if(retunVal.equals("-1")){
					DialogUtil.builder(PdaRegActivity.this, "提示信息", "序列号还未注册！",0);
				}else if(!retunVal.equals("Y")){
					DialogUtil.builder(PdaRegActivity.this, "提示信息", "序列号还未启用！",0);
				}else{
					DialogUtil.builder(PdaRegActivity.this, "提示信息", "操作成功！",0);
					String selectStr = "select col1 from systable where desc='pdaState'";
					Cursor select_cursor = sDatabase.rawQuery(selectStr, null);
					if (select_cursor.moveToFirst()) {
						sDatabase.execSQL("update systable   set col1='Y' where desc='pdaState'");
					} else {
						sDatabase
								.execSQL("insert into systable (col1,desc) values('Y','pdaState')");
					}
					select_cursor.close();
					SysData.isreg="Y"; 
				}
			}
		}
	};
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pda_reg);
		ip = (EditText) findViewById(R.id.ip);
		port = (EditText) findViewById(R.id.port);
		itemName = (EditText) findViewById(R.id.itemName);
		db = new SqlHelpUtil(getApplicationContext());
		sDatabase = db.getWritableDatabase();
		String selectStr = "select col1,col2,col3 from systable where desc='pdaRegServer'";
		Cursor select_cursor = sDatabase.rawQuery(selectStr, null);
		if (select_cursor.moveToFirst()) {
			ip.setText(select_cursor.getString(0));
			port.setText(select_cursor.getString(1));
			itemName.setText(select_cursor.getString(2));
		}
		select_cursor.close();
	}

	public void onsub(View v) {
		if (ip.getText().toString().trim().equals("")) {
			DialogUtil.builder(PdaRegActivity.this, "错误信息", "请填写服务器IP！",0);
			ip.requestFocus();
			return;
		}
		if (port.getText().toString().trim().equals("")) {
			DialogUtil.builder(PdaRegActivity.this, "错误信息", "请填写服务器端口！",0);
			port.requestFocus();
			return;
		}
		if (itemName.getText().toString().trim().equals("")) {
			DialogUtil.builder(PdaRegActivity.this, "错误信息", "请填写工程名！",0);
			itemName.requestFocus();
			return;
		}
		String selectStr = "select col1 from systable where desc='pdaRegServer'";
		Cursor select_cursor = sDatabase.rawQuery(selectStr, null);
		String ipValue = ip.getText().toString().trim();
		String portValue = port.getText().toString().trim();
		String itemNameValue = itemName.getText().toString().trim();
		if (select_cursor.moveToFirst()) {
			sDatabase.execSQL("update systable   set col1='" + ipValue
					+ "',col2='" + portValue + "',col3='" + itemNameValue
					+ "' where desc='pdaRegServer'");
		} else {
			sDatabase
					.execSQL("insert into systable (col1,col2,col3,desc) values('"
							+ ipValue
							+ "','"
							+ portValue
							+ "','"
							+ itemNameValue + "','pdaRegServer')");
		}
		select_cursor.close();
		SysData.url = "http://".concat(ipValue).concat(":").concat(portValue)
				.concat("/").concat(itemNameValue);
		DialogUtil.builder(PdaRegActivity.this, "提示", "保存成功,请点下面的PDA注册按钮进行注册！",0);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_pda_reg, menu);
		return true;
	}

	public void onback(View v) {
		finish();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (db != null) {
			db.close();
			sDatabase.close();
		}
	}

	public void pdaRegister(View v) throws Exception {
		String sn = SysUtil.getSn();
		if (sn == null || sn.length() < 1) {
			DialogUtil.builder(PdaRegActivity.this, "提示", "序列号为空,请联系供应商",0);
			return;
		}
		new Thread(new PdaRegValidate()).start();
	}

	public class PdaRegValidate implements Runnable {
		public void run() {
			String ipValue = ip.getText().toString().trim();
			String portValue = port.getText().toString().trim();
			String itemNameValue = itemName.getText().toString().trim();
			String url = "http://".concat(ipValue).concat(":")
					.concat(portValue).concat("/").concat(itemNameValue);
			url = url + "/webservice/ServiceInterface?wsdl";
			System.out.println(url);
			String method = "pdaZcValidate";
			SoapObject rpc = new SoapObject("", method);
			System.out.println(SysUtil.getSn());
			rpc.addProperty("sn", SysUtil.getSn());
			try {
				HttpTransportSE ht = new HttpTransportSE(url, SysData.timeout);
				ht.debug = true;
				SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
						SoapEnvelope.VER11);
				envelope.bodyOut = rpc;
				envelope.dotNet = true;
				envelope.setOutputSoapObject(rpc);
				ht.call("", envelope);
				if (envelope.getResponse() != null) {
					String s=envelope.getResponse().toString();
					Message message = new Message();
					message.obj = s;
					handler.sendMessage(message);
				}else{
					Message message = new Message();
					message.what = -2;
					handler.sendMessage(message);
				}
			} catch (Exception e) {
				e.printStackTrace();
				Message message = new Message();
				message.what = -1;
				message.obj = e.toString();
				handler.sendMessage(message);
			}
		}
	}
}
