package com.sunzee.mvp.passwordreset;

import android.util.Log;

import com.sunzee.R;
import com.sunzee.base.BasePresenter;
import com.sunzee.model.Heartbeat;
import com.sunzee.model.HttpResult;
import com.sunzee.model.domain.Name;
import com.sunzee.retrofit.ApiCallback;
import com.sunzee.utils.SharedPreferencesUtils;
import com.sunzee.utils.ToastUtil;
import com.sunzee.utils.UiUtil;

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

/**
 * 重置密码界面 presenter
 */
public class PasswordResetPresenter extends BasePresenter<PasswordResetView> {
    private int accountState = -1;

    public PasswordResetPresenter(PasswordResetView view) {
        attachView(view);
    }

    /**
     * //旧密码相同
     * if (confirmPas.equals(pass)) {
     * //新密码 重复密码
     * if (oldPas.equals(newPas)) {
     *
     * @param oldPas
     * @param newPas
     * @param confirmPas
     */

    public void reset(String oldPas, String newPas, String confirmPas) {
        if (oldPas == null || oldPas.equals("") || newPas == null && newPas.equals("") || confirmPas == null || confirmPas.equals("")) {
            ToastUtil.showToast(UiUtil.getStringRes(R.string.empty_password));
            return;
        }
        accountState = (int) SharedPreferencesUtils.getParam(Name.ACCOUNT_STATE, -1);
        if (accountState == 0) {
            //admin
            String adminPas = (String) SharedPreferencesUtils.getParam(Name.ADMIN, "-1");
            checkPas(oldPas, newPas, confirmPas, adminPas);
        } else if (accountState == 1) {
            //guest
            String guestPas = (String) SharedPreferencesUtils.getParam(Name.GUEST, "-1");
            checkPas(oldPas, newPas, confirmPas, guestPas);
        } else {

        }
    }

    private static final String TAG = "PasswordResetPresenter";

    /**
     * 更新服务器密码
     *
     * @param newPas 新密码
     * @param type   类型
     */
    private void updatePassword(String newPas, String type) {
        Map<String, String> params = new HashMap<>();
        params.put("clientId", Heartbeat.deviceId);
        params.put("pwd", newPas);
        params.put("pwdType", type);
        addSubscription(apiStores.updatePaw(params), new ApiCallback<HttpResult<String>>() {
            @Override
            public void onSuccess(HttpResult<String> model) {
                Log.d(TAG, "onSuccess: " + model);
                if (model.getData().equals("更新密码成功")) {
                    mvpView.showResult(true);
                }
            }

            @Override
            public void onFailure(String msg) {

            }

            @Override
            public void onFinish() {

            }
        });
    }

    /**
     * @param oldPas     旧
     * @param newPas     新
     * @param pass
     * @param confirmPas
     */
    private void checkPas(String oldPas, String newPas, String confirmPas, String pass) {
        //旧密码相同
        if (oldPas.equals(pass)) {
            //新密码 重复密码
            if (confirmPas.equals(newPas)) {
                if (accountState == 0) {
                    SharedPreferencesUtils.setParam(Name.ADMIN, newPas);
                    updatePassword(newPas, "1");

                } else {
                    SharedPreferencesUtils.setParam(Name.GUEST, newPas);
                    updatePassword(newPas, "0");
                }
            } else {
                mvpView.showResult(false);
                ToastUtil.showToast(UiUtil.getStringRes(R.string.mmbyz));
            }
        } else {
            mvpView.showResult(false);
            ToastUtil.showToast(UiUtil.getStringRes(R.string.oldpas));
        }
    }
}
