package BWP.MobileSale;

import BWP.MobileSale.Config.ServerConfig;
import BWP.MobileSale.Util.ActivityUtil;
import BWP.MobileSale.Util.ConfigUtil;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class ServerConfigActivity extends Activity {
	private EditText urlInput;
	private EditText pageCountInput;
	private ServerConfig config;
	

	String[] options= new String[]{"http://192.168.1.101:2000/MainSystem/",
			"http://192.168.1.2/B2/",
			"http://203.86.48.90:81/B2/","http://192.168.1.123:2000/MainSystem/","http://42.121.96.192/B2/"};
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, Menu.FIRST, 0, "设置测试地址");
		// TODO Auto-generated method stub
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()){
		case Menu.FIRST:
			new AlertDialog.Builder(this).setItems(options,new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					urlInput.setText(options[which].toString());
				}
			}).show();
			break;
		
		}
		
		return super.onOptionsItemSelected(item);
		
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_serverconfig);
	
		urlInput = ActivityUtil.Find(this,R.id.EditText_Url);
		config = new ServerConfig(getBaseContext());
		ConfigUtil.Fill(config);
		urlInput.setText(config.getServerUri());
	
	
		Button saveButton= ActivityUtil.FindButton(this,R.id.Button_OK);
		saveButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				config.setServerUri(urlInput.getText().toString());
				ConfigUtil.Save(config);
				finish();
			}
		});
		
		
		Button cancelButton = ActivityUtil.FindButton(this, R.id.Button_Cancel);
		cancelButton.setOnClickListener(new OnClickListener(){

			public void onClick(View arg0) {
				finish();
			}
				
		});
	}
	
}
