package com.stu.assistant.presenter;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.stu.assistant.activity.LoginActivity;
import com.stu.assistant.base.BaseApplion;
import com.stu.assistant.base.BasePresenter;
import com.stu.assistant.bean.UserInfoBean;
import com.stu.assistant.uitls.LogUtils;
import com.stu.assistant.uitls.SharedConfig;
import com.stu.assistant.uitls.SharedUtil;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

public class LoginPresenter extends BasePresenter {
    private LoginActivity activity;

    public LoginPresenter(LoginActivity mAct) {
        this.activity = mAct;
    }

    public void saveUser(UserInfoBean bean) {
        SharedUtil.create(BaseApplion.application).putString(SharedConfig.loginName, bean.getUsername());
        SharedUtil.create(BaseApplion.application).putString(SharedConfig.nickName, bean.getNickName());
        SharedUtil.create(BaseApplion.application).putString(SharedConfig.whToken, bean.getSessionToken());
        SharedUtil.create(BaseApplion.application).putString(SharedConfig.objectId, bean.getObjectId());
        SharedUtil.create(BaseApplion.application).putString(SharedConfig.userLevel, bean.getUserLevel());
        SharedUtil.create(BaseApplion.application).putString(SharedConfig.userType, bean.getUserType());
        SharedUtil.create(BaseApplion.application).putBoolean(SharedConfig.isLogin, true);
    }

    public boolean getIsLogin() {
        return SharedUtil.create(BaseApplion.application).getBoolean(SharedConfig.isLogin, false);
    }

    public void loginIn(String usertNameEditMsg, String usertPasswordEditMsg, OnLoginResultListener loginResultListener) {
        if (TextUtils.isEmpty(usertNameEditMsg)) {
            loginResultListener.errorMsg("请输入账号");
            return;
        }

        if (TextUtils.isEmpty(usertPasswordEditMsg)) {
            loginResultListener.errorMsg("请输入密码");
            return;
        }

        activity.showProDialog("正在登录，请稍后...");

        UserInfoBean userInfoBean = new UserInfoBean();
        userInfoBean.setUsername(usertNameEditMsg);
        userInfoBean.setPassword(usertPasswordEditMsg);
        userInfoBean.login(new SaveListener<UserInfoBean>() {
            @Override
            public void done(UserInfoBean userInfoBean1, BmobException e) {
                activity.dismissProDialog();
                if (e == null) {
                    UserInfoBean user = BmobUser.getCurrentUser(UserInfoBean.class);
                    LogUtils.i("hx","-UserInfoBean--->"+new Gson().toJson(user));
                    saveUser(user);
                    loginResultListener.isSucLogin(user);
                } else {
                    LogUtils.i("hx","-登录失败--->"+e.getMessage());
                    loginResultListener.errorMsg("用户或密码不正确");
                }
            }
        });
    }

    public interface OnLoginResultListener {
        void isSucLogin(UserInfoBean bean);

        void errorMsg(String msg);
    }
}
