package com.smart.gtstrvl;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import com.example.mountaintravel.base.BaseActivity;
import com.example.mountaintravel.util.MD5Util;
import com.example.mountaintravel.util.PhoneUtil;
import com.example.mountaintravel.util.StreamTools;
import com.example.mountaintravel.util.ToastUtil;
import com.example.mountaintravel.variable.Variable;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.InputType;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Findpass2Activity extends BaseActivity {
	private SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
	private String time = format.format(new Date());
	private String type = "android";
	private String key = ".5Xf_01KXd352s177Jx3wxXc074poII";
	private String sign = MD5Util.MD5(time + type + key);
	private EditText find2_editTextpassword;
	private EditText find2_editTextnewpass;
	private String name;
	private String status = "";
	private TextView find2_tverr;
	private TextView find2_tverrtwo;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_findpass2);
		initTitleBar("找回密码");
		find2_editTextpassword = (EditText) findViewById(R.id.find2_editTextpassword);
		find2_editTextnewpass = (EditText) findViewById(R.id.find2_editTextnewpass);
		find2_editTextpassword.requestFocus();
		find2_editTextpassword
				.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
		find2_editTextnewpass
				.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
		find2_tverr = (TextView) findViewById(R.id.find2_tverr);
		find2_tverrtwo = (TextView) findViewById(R.id.find2_tverrtwo);
		Intent intent = getIntent();
		name = intent.getStringExtra("name");
		find2_editTextnewpass
				.setOnFocusChangeListener(new View.OnFocusChangeListener() {

					@Override
					public void onFocusChange(View v, boolean hasFocus) {
						if (hasFocus) {// 获得焦点
							ifregister_editText1();

						} else {// 失去焦点

						}
					}

				});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.findpass2, menu);
		return true;
	}

	public void button(View v) {
		if (TextUtils.isEmpty(find2_editTextpassword.getText().toString().trim())
				|| TextUtils.isEmpty(find2_editTextnewpass.getText().toString()
						.trim())) {
			find2_tverr.setVisibility(View.VISIBLE);
			find2_tverr.setText("  密码不能为空");
			return;
		}
		if (find2_editTextpassword.getText().toString().trim().length() < 6) {
			find2_tverr.setVisibility(View.VISIBLE);
			find2_tverr.setText("  密码不能少于6位");
			return;
			// 有一个接口判断是否已注册过if
		}
		if (find2_editTextpassword.getText().toString().trim().length() > 20) {
			find2_tverr.setVisibility(View.VISIBLE);
			find2_tverr.setText("  密码长度不合法");
			return;
			// 有一个接口判断是否已注册过if
		}

		find2_tverr.setText("123");
		find2_tverr.setVisibility(View.GONE);

		if (!find2_editTextpassword.getText().toString().trim()
				.equals(find2_editTextnewpass.getText().toString().trim())) {
			find2_tverrtwo.setVisibility(View.VISIBLE);
			find2_tverrtwo.setText("  密码输入不一致");
			return;
		}
		find2_tverrtwo.setText("123");
		find2_tverrtwo.setVisibility(View.GONE);

		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				register();
			}

		}).start();
	}

	private void ifregister_editText1() {
		if (TextUtils.isEmpty(find2_editTextpassword.getText().toString().trim())) {
			find2_tverr.setVisibility(View.VISIBLE);
			find2_tverr.setText("  密码不能为空");
			return;
		}
		if (find2_editTextpassword.getText().toString().trim().length() < 6) {
			find2_tverr.setVisibility(View.VISIBLE);
			find2_tverr.setText("  密码不能少于6位");
			return;
		
		}
		if (find2_editTextpassword.getText().toString().trim().length() > 20) {
			find2_tverr.setVisibility(View.VISIBLE);
			find2_tverr.setText("  密码长度不合法");
			return;
			// 有一个接口判断是否已注册过if
		}

		find2_tverr.setText("123");
		find2_tverr.setVisibility(View.GONE);

	}

	private void register() {
		// TODO Auto-generated method stub
		String phone = name;
		String pwds = find2_editTextpassword.getText().toString().trim();
		DefaultHttpClient client = new DefaultHttpClient();
		// String uri = "http://192.168.20.80:5050/gts/terminal/login";
		String uri = "http://" + Variable.address + "/terminal/forgotpwds";
		HttpPost httpPost = new HttpPost(uri);
		try {
			List<NameValuePair> parameters = new ArrayList<NameValuePair>();
			// 往这个集合中添加你要传递的参数

			parameters.add(new BasicNameValuePair("type", type));
			parameters.add(new BasicNameValuePair("time", time));
			parameters.add(new BasicNameValuePair("sign", sign));
			parameters.add(new BasicNameValuePair("name", phone));
			parameters.add(new BasicNameValuePair("login_type", "1"));
			parameters.add(new BasicNameValuePair("pwds", pwds));
			// 创建传递参数封装 实体对象
			UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters,
					"UTF-8");// 设置传递参数的编码
			// 把实体对象存入到httpPost对象中
			httpPost.setEntity(entity);
			// 3. 调用第一步中创建好的实例的 execute 方法来执行第二步中创建好的 method 实例
			HttpResponse response = client.execute(httpPost); // HttpUriRequest的后代对象
			if (response.getStatusLine().getStatusCode() == 200) {
				InputStream is = response.getEntity().getContent();
				String result = StreamTools.streamToString(is);
				JSONObject json = new JSONObject(result);
				status = json.getString("status");
				runOnUiThread(new Runnable() {

					@Override
					public void run() {
					
						if (status.equals("9002")) {
							ToastUtil.show(Findpass2Activity.this, "找回密码成功");
							finish();
							startActivity(LoginActivity.class, R.anim.left_out,
									R.anim.right_in);
							
						}
						if (!status.equals("9002")) {
							ToastUtil.show(Findpass2Activity.this, "找回密码失败");
						}
					}
				});
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			runOnUiThread(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					ToastUtil.show(Findpass2Activity.this, "提交异常");
				}
			});
		} finally {
			// 6. 释放连接。无论执行方法是否成功，都必须释放连接
			client.getConnectionManager().shutdown();
		}
	}
}
