package net.coding.program.setting;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.widget.TextView;

import com.loopj.android.http.RequestParams;

import net.coding.program.BaseActivity;
import net.coding.program.Global;
import net.coding.program.LoginActivity_;
import net.coding.program.R;
import net.coding.program.common.CustomDialog;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.OptionsItem;
import org.androidannotations.annotations.OptionsMenu;
import org.androidannotations.annotations.ViewById;
import org.json.JSONException;
import org.json.JSONObject;

@EActivity(R.layout.activity_set_password)
@OptionsMenu(R.menu.set_password)
public class SetPasswordActivity extends BaseActivity {

    @ViewById
    TextView oldPassword;

    @ViewById
    TextView newPassword;

    @ViewById
    TextView confirmPassword;

    @AfterViews
    void init() {
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @OptionsItem(android.R.id.home)
    void back() {
        onBackPressed();
    }

    final String Url = Global.HOST + "/api/user/updatePassword";

    @OptionsItem
    void submit() {
        RequestParams params = new RequestParams();
        try {
            params.put("current_password", Global.sha1(oldPassword.getText().toString()));
            params.put("password", Global.sha1(newPassword.getText().toString()));
            params.put("confirm_password", Global.sha1(confirmPassword.getText().toString()));
            postNetwork(Url, params, "");
        } catch (Exception e) {
            showMiddleToast(e.toString());
        }
    }

    @Override
    public void parseJson(int code, JSONObject respanse, String tag, int pos, Object data) throws JSONException {
        if (code == 0) {
            showButtomToast("密码修改成功");
            popDialog();
        } else {
            showErrorMsg(code, respanse);
        }
    }

    private void popDialog() {
        AlertDialog dialog = new AlertDialog.Builder(this)
                .setMessage("修改密码后需要重新登录")
                .setNegativeButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                        startActivity(new Intent(SetPasswordActivity.this, LoginActivity_.class));
                    }
                })
                .setCancelable(false)
                .show();

        CustomDialog.dialogTitleLineColor(this, dialog);
    }

}

@@ -133,16 +133,47 @@
 Intent;%0A
+import android.text.TextUtils;%0A
 import a
@@ -1394,85 +1394,271 @@
     
-params.put(%22current_password%22, Global.sha1(oldPassword.getText().toString()))
+String oldPwd = oldPassword.getText().toString();%0A            String newPwd = newPassword.getText().toString();%0A            String confirmPwd = confirmPassword.getText().toString();%0A            if (checkPassword(oldPwd, newPwd, confirmPwd))%0A                return
 ;%0A  
@@ -1679,16 +1679,24 @@
 ms.put(%22
+current_
 password
@@ -1714,40 +1714,71 @@
 ha1(
-newPassword.getText().toString()
+oldPwd));%0A            params.put(%22password%22, Global.sha1(newPwd
 ));%0A
@@ -1844,36 +1844,10 @@
 irmP
-assword.getText().toString()
+wd
 ));%0A
@@ -1976,24 +1976,714 @@
    %7D%0A    %7D%0A%0A
+    boolean checkPassword(String oldPwd, String newPwd, String confirmPwd) %7B%0A        if (TextUtils.isEmpty(oldPwd)) %7B%0A            showMiddleToast(%22%E5%BD%93%E5%89%8D%E5%AF%86%E7%A0%81%E4%B8%8D%E8%83%BD%E4%B8%BA%E7%A9%BA%22);%0A            return true;%0A        %7D%0A        if (TextUtils.isEmpty(newPwd)) %7B%0A            showMiddleToast(%22%E6%96%B0%E5%AF%86%E7%A0%81%E4%B8%8D%E8%83%BD%E4%B8%BA%E7%A9%BA%22);%0A            return true;%0A        %7D%0A        if (TextUtils.isEmpty(confirmPwd)) %7B%0A            showMiddleToast(%22%E7%A1%AE%E8%AE%A4%E5%AF%86%E7%A0%81%E4%B8%8D%E8%83%BD%E4%B8%BA%E7%A9%BA%22);%0A            return true;%0A        %7D%0A        if (!newPwd.equals(confirmPwd)) %7B%0A            showMiddleToast(%22%E4%B8%A4%E6%AC%A1%E5%AF%86%E7%A0%81%E8%BE%93%E5%85%A5%E4%B8%8D%E4%B8%80%E8%87%B4%22);%0A            return true;%0A        %7D%0A        if (newPwd.length() %3C 6) %7B%0A            showMiddleToast(%22%E5%AF%86%E7%A0%81%E4%B8%8D%E8%83%BD%E5%B0%91%E4%BA%8E6%E4%BD%8D%22);%0A            return true;%0A        %7D%0A        return false;%0A    %7D%0A%0A
     @Overrid

add check