package BWP.MobileSale;

import java.math.BigDecimal;
import java.util.List;

import org.forks.jsonrpc.DFDataTable;
import org.forks.jsonrpc.ManyList;
import org.forks.jsonrpc.RpcFacade;
import org.forks.jsonrpc.RpcObject;

import BWP.MobileSale.Config.LoginUserConfig;
import BWP.MobileSale.MobileAdjustGoodsBill.MobileAdjustGoodsBillMainActivity;
import BWP.MobileSale.Util.ConfigUtil;
import BWP.MobileSale.Util.StringUtil;
import BWP.MobileSale.Util.ViewOnClickTask;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
@SuppressLint("NewApi")
public class LoginActivity extends Activity {

	private EditText usernameInput;
	private EditText passwordInput;
	private CheckBox autoSaveInput;
	private LoginUserConfig config = new LoginUserConfig(this);

	ViewOnClickTask loginTask = new ViewOnClickTask(this, "登录中...") {

		public Object call() throws Exception {
			String username = usernameInput.getText().toString();
			String password = passwordInput.getText().toString();
			config.Username = username;
			config.Password = password;
			config.SavePassword = autoSaveInput.isChecked();
			if (!config.SavePassword) {
				config.Password = "";
			}
			ConfigUtil.Save(config);
			RpcFacade.login(username, password);			
			// 放到每一个子模块中了
			// String method =
			// "/Sale/Sale/Rpcs/MobileReturnClientRpc/FeatureEnable";
			// JsonRpcResult result = RpcFacade.rpcCall(method);
			// featurEnable = result.getBoolean();
			// if (!featurEnable) {
			// RpcFacade.logout();
			// }

			return null;
		}

		@Override
		protected void successUI() {

			SaleApplication.SaveLogin(config.Username, config.Password);
			Utils.ActivityUtil.Go(LoginActivity.this, MainActivity.class);

		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
    	String strVer=android.os.Build.VERSION.RELEASE;
    	strVer=strVer.substring(0,3).trim();
    	float fv=Float.valueOf(strVer);
    	if(fv>2.3)
    	{
    		
    		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
    		StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedSqlLiteObjects().penaltyLog().penaltyDeath().build());
       	}
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		usernameInput = (EditText) findViewById(R.id.EditText_Username);
		passwordInput = (EditText) findViewById(R.id.EditText_Password);
		autoSaveInput = (CheckBox) findViewById(R.id.CheckBox_AutoSave);
		SaleApplication.ClearLogin();

		ConfigUtil.Fill(config);
		usernameInput.setText(config.Username);
		passwordInput.setText(config.Password);
		autoSaveInput.setChecked(config.SavePassword);

		if (!StringUtil.IsNullOrEmpty(config.Username)
				&& StringUtil.IsNullOrEmpty(config.Password)) {
			passwordInput.requestFocus();
		}
		Button buttonCancel = (Button) this.findViewById(R.id.Button_Cancel);
		buttonCancel.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				finish();

			}
		});

		Button buttonLogin = (Button) findViewById(R.id.Button_OK);
		buttonLogin.setOnClickListener(loginTask);
		
		Button btnSetUrl=(Button)findViewById(R.id.Button_SetUrl);
		btnSetUrl.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				ServerConfig();				
			}
		});
  		
		checkUpdate();

	}

	private void checkUpdate() {
		 Intent intent = new Intent();
		 intent.setClass(this, UpdateService.class);
		 startService(intent);
	}

	@Override
	protected void onResume() {
		super.onResume();
		checkUpdate();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, Menu.FIRST, 0, "服务器设置");
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case Menu.FIRST:
			ServerConfig();
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	private void ServerConfig() {
		Intent intent = new Intent();
		intent.setClass(this.getBaseContext(), ServerConfigActivity.class);
		startActivity(intent);

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		org.forks.ui.UIHelper.shutdowApp();

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			new AlertDialog.Builder(this)
					.setTitle("确定退出吗")
					.setPositiveButton("确定",
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									finish();
								}
							})
					.setNegativeButton("取消",
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {

									dialog.cancel();//
								}
							}).create().show();
			break;
		default:
			break;
		}
		return false;

	}
}
