package com.ycgis.macall.personalcenter.v.activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.google.gson.JsonObject;
import com.rs.macall.androidx.basemodel.base.BaseViewBindActivity;
import com.rs.macall.androidx.basemodel.callback.CustomClickListener;
import com.rs.macall.androidx.basemodel.utils.DimensionConvert;
import com.rs.macall.androidx.basemodel.utils.StringUtil;
import com.rs.macall.androidx.basemodel.utils.ToastUtil;
import com.rs.macall.androidx.basemodel.utils.TypConversion;
import com.ycgis.macall.personalcenter.R;
import com.ycgis.macall.personalcenter.databinding.ActivityUpdatePasswordBinding;
import com.ycgis.macall.personalcenter.p.app.RuanseeApplication;
import com.ycgis.macall.personalcenter.p.callback.BaseRequestCallback;
import com.ycgis.macall.personalcenter.p.request.ApiModel;
import com.ycgis.macall.personalcenter.p.request.RetrofitService;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class UpdatePasswordActivity extends BaseViewBindActivity<ActivityUpdatePasswordBinding> {
    private String imei = "", imsi = "";

    @Override
    protected ActivityUpdatePasswordBinding getViewBinding() {
        return ActivityUpdatePasswordBinding.inflate(getLayoutInflater());
    }

    @Override
    protected boolean isImmersiveStatusBar() {
        return true;
    }

    @Override
    protected void init() {
        viewBinding.btnSave.setOnClickListener(customClickListener);
    }

    @Override
    protected void readInstanceState(Bundle savedInstanceState) {
        imei = getIntent().getStringExtra("imei");
        imsi = getIntent().getStringExtra("imsi");

    }

    @Override
    protected void start() {
        viewBinding.updateTitle.contentTitle.setText("修改账号密码");
        viewBinding.updateTitle.leftImage.setImageDrawable(getDrawable(R.drawable.back_cliener));
        viewBinding.updateTitle.titleleft.setOnClickListener(v -> finish());
    }

    @Override
    protected void bindViewClick(View v) {
        if (v.getId() == R.id.btn_save) {
            String pass = getEditText(viewBinding.editPassword);
            String pass1 = getEditText(viewBinding.editNewPassword1);
            String pass2 = getEditText(viewBinding.editNewPassword2);

            if (StringUtil.isNullOrEmpty(pass)) {
                shoeDialogTitle("原密码不能为空！");
                return;
            }
            if (StringUtil.isNullOrEmpty(pass1)) {
                shoeDialogTitle("请输入新密码！");
                return;
            }
            if (!isPassword(getEditText(viewBinding.editNewPassword1))) {
                shoeDialogTitle("密码格式不正确！");
                return;
            }
            if (StringUtil.isNullOrEmpty(pass2)) {
                shoeDialogTitle("请再次确认新密码！");
                return;
            }
            if (!pass1.equals(pass2)) {
//                viewBinding.editNewPassword2.setText("");
                viewBinding.editNewPassword2.setFocusable(true);
                viewBinding.editNewPassword2.setFocusableInTouchMode(true);
                shoeDialogTitle("两次密码不一样！！！");
                return;
            }
            updatePassword(pass, pass1);
        }
    }

    private void updatePassword(String ymm, String xmm) {
        Map<String, String> param = new HashMap<>();
        param.put("token", RuanseeApplication.getAppCache().getToken());
        param.put("clientType", "MOBILE");
        param.put("deviceFlags", imei);
        param.put("imsi1", imsi);
        param.put("sysCode", RuanseeApplication.getAppCache().getSysmCode());
        param.put("oldPwd", ymm);
        param.put("newPwd", xmm);
        baseShowDialog("正在提交请稍后...");
        ApiModel.request(RetrofitService.getBaseInstance().getLoginType("http://20.90.2.2/UniAuth/api/sso/v1/updatePwd", param), new BaseRequestCallback<JsonObject>() {
            @Override
            public void onRequestFailure(String msg) {
                baseDismissDialog();
                ToastUtil.centered(UpdatePasswordActivity.this, msg);
            }

            @Override
            public void onRequestSuccess(JsonObject result) {
                baseDismissDialog();
                ApiModel.uploadOperationLog("3","3", "修改了统一日志登录密码");
                try {
                    JSONObject object = new JSONObject(result.toString());
                    if (TypConversion.getJsonStr(object, "code").equals("200")) {
                        ToastUtil.centered(UpdatePasswordActivity.this, TypConversion.getJsonStr(object, "content"));
                        finish();
                        return;
                    }
                    ToastUtil.centered(UpdatePasswordActivity.this, TypConversion.getJsonStr(object, "content"));
                } catch (JSONException e) {
                    e.printStackTrace();
                    ToastUtil.centered(UpdatePasswordActivity.this, "解析失败！请联系管理员！");
                }
            }
        });
    }

    public void shoeDialogTitle(String message) {
        Dialog dialog = new AlertDialog.Builder(this)
                .setTitle("重要提示")
                .setMessage("\n\t\t\t" + message)
                .setIcon(R.drawable.icon_jg)
                .create();
        Window dialogWindow = dialog.getWindow();
        // 设置 dialog 显示的位置
        dialogWindow.setGravity(Gravity.CENTER);
        int ss = DimensionConvert.dip2px(this, 20);
        // 设置 dialog 的边距
        dialogWindow.getDecorView().setPadding(DimensionConvert.dip2px(this, 8), ss, 0, ss);
        // 设置 标题
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        // 获取 LayoutParams
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        // 设置 dialog 宽度高度
        lp.width = DimensionConvert.dip2px(this, 300);
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        dialogWindow.setAttributes(lp);
        dialog.show();
    }


    public boolean isPassword(String password) {
        if (StringUtil.isNullOrEmpty(password)) return false;
        // 大小写加数字

        String regex = "(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])[a-zA-Z0-9]{8,15}";
//        String regex = "/^[a-zA-Z\\d]+$/";
        return password.matches(regex);
    }
}