package com.wd.workoffice.ui.activity.user;

import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;

import com.wd.workoffice.R;
import com.wd.workoffice.app.BaseBean;
import com.wd.workoffice.app.Config;
import com.wd.workoffice.app.WorkToolBarActivity;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import flexible.xd.android_base.network.rtfhttp.Transformer;
import flexible.xd.android_base.utils.RegexUtils;
import io.reactivex.disposables.Disposable;

/**
 * 我的 - 设置 - 修改密码
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class UserModifyPwdActivity extends WorkToolBarActivity {

    @BindView(R.id.et_old_pwd)
    EditText etOldPwd;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.et_confirm_pwd)
    EditText etConfirmPwd;
    @BindView(R.id.btn_ok)
    Button btnOk;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
    }

    @Override
    protected void initData() {
    }


    @Override
    protected void initEvent() {
    }

    @Override
    protected int layoutId() {
        return R.layout.activity_modify_pwd;
    }


    private void modify(String oldPwd, String newPwd) {
        RtfUtils.getRtf().modifyPwd(oldPwd, newPwd).compose(Transformer.schedule()).subscribe(new WorkObserver<BaseBean>() {


            @Override
            public void doOnSubscribe(Disposable d) {
            }

            @Override
            public void onFail(String errorMsg) {
                hideLoading();
                toast(errorMsg);
            }

            @Override
            public void onSuccess(BaseBean data) {
                hideLoading();
                if (data.getCode() != 0) {
                    toast(data.getMessage());
                    return;
                }
                toast("修改成功");
                finish();
            }
        });
    }


    @OnClick(R.id.btn_ok)
    public void onViewClicked() {
        String oldPwd = etOldPwd.getText().toString();
        String pwd = etPwd.getText().toString();
        String confirmPwd = etConfirmPwd.getText().toString();
        if (!RegexUtils.isMatch(Config.REGEX_PASSWORD, pwd)) {
            toast("设置密码数字或字母６-16位");
            return;
        }
        if (TextUtils.equals(pwd, "123456")) {
            toast("密码不能为123456");
            return;
        }
        if (!TextUtils.equals(pwd, confirmPwd)) {
            toast("两次输入的密码不一致");
            return;
        }
        modify(oldPwd, pwd);
    }
}
