package com.danding.yang800.Presenter;

import android.content.Context;
import android.content.SharedPreferences;

import com.danding.yang800.BuildConfig;
import com.danding.yang800.ChangeTakePassActivity;
import com.danding.yang800.General.HttpCallBack;
import com.danding.yang800.Util.GeneralUtil;
import com.danding.yang800.Util.MD5Util;
import com.danding.yang800.Util.ToastUtil;
import com.danding.yang800.Util.VolleyUtil;

import org.json.JSONObject;

import java.util.HashMap;

public class ChangeTakePassActivityPresenter {
    private ChangeTakePassActivity context;
    SharedPreferences sp;

    public ChangeTakePassActivityPresenter(ChangeTakePassActivity context) {
        sp = context.getSharedPreferences("variable", Context.MODE_PRIVATE);
        this.context = context;
    }


    public void updateWithdrawCashPassword(final String userId, final String oldPassword, final String newPassword, final String verifyPassword) {
        if (newPassword.equals("") || verifyPassword.equals("") || oldPassword.equals("")) {
            ToastUtil.show(context, "密码不能为空");
            return;
        }
        if (!newPassword.equals(verifyPassword)) {
            ToastUtil.show(context, "请输入相同的提现密码！");
            return;
        }
        final String oldPasswordEncode = GeneralUtil.encode(MD5Util.MD5(oldPassword) + "yang800_$_&_%_#_@");
        final String newPasswordEncode = GeneralUtil.encode(MD5Util.MD5(newPassword) + "yang800_$_&_%_#_@");
        final String verifyPasswordEncode = GeneralUtil.encode(MD5Util.MD5(verifyPassword) + "yang800_$_&_%_#_@");
        context.initDialog();
        HashMap<String, String> map = new HashMap<>();
        map.put("userId", userId);
        map.put("oldPassword", oldPasswordEncode);
        map.put("newPassword", newPasswordEncode);
        map.put("verifyPassword", verifyPasswordEncode);
        new VolleyUtil(context).request(context,  "user.setting.updateWithdrawCashPassword",true, map, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject json) {
                try {
                    context.successData();
                    context.dismissDialog();
                } catch (Exception e) {
                    e.printStackTrace();
                    onCatch();
                }

            }


            @Override
            public void onerrSuccess() {
                context.dismissDialog();
            }

            @Override
            public void onCatch() {
                context.dismissDialog();
            }

            @Override
            public void onFail() {
                context.dismissDialog();
            }
        });
    }


}
